Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
void main()
{   char * number=new char[5];
    char * bnumber=new char[5];
	number="12345";
	bnumber="67890";
	int carry=0;
	char * sum=new char[strlen(number)+2];
	int t=0;
	for(int j=strlen(number)-1;j>=0;j--)
	{
		sum[t]=(number[j]-48)+(bnumber[j]-48)+carry;
		if(j==0)
		{
			sum[t]=(number[j]-48)+(bnumber[j]-48)+carry;
			break;
		}
		if((sum[t]-48)>=10)
		{   
			carry=(sum[t]-48)/10;
			sum[t]=(sum[t]-48)%10;
			t++;
		}
	}
cout<<sum;

	_getch();
}

C++

Posted
Comments
[no name] 28-Nov-15 3:44am    
i was told to use string array instead of int array and i was supposed to add two large integers as i was making a class of BigInt in visual c++.
Suvendu Shekhar Giri 28-Nov-15 3:54am    
I haven't read your code thoroughly but I believe you are missing type casting of your variables prior to addition. In case this information may help.

Start off by simplifying things: create a function which accepts a char parameter and returns an int.
Make the function check that the char is numeric: '0'..'9' only (return a negative value if it isn't) and convert it to an integer value - that's pretty trivial when you do it in isolation.

Test the function: pass it all the valid characters, and make sure it returns the right integer value for each of them. Check it does return a "bad character" value for everything else.

Write another which converts an integer to a character. Test it.

Then rewrite your code to use that function: Allocate an output array that holds the right number of characters (ie. the longest of the two inputs plus one: 99 + 99 = 198, so the addition of two n-digit numbers will always be at most n + 1 digits long, plus one for the terminating null)

Then loop through the inputs starting from the lowest digit in each - the least significant - and use the method you wrote to convert them to integers. Add them, plus any carry from the previous sum and check the value. If it's greater than 9, set the carry to one, and subtract 10 from the integer. others=wise, set the carry to zero. Use your second function to convert the integer back to a character and add it to your output array.

Test it.

You'll need to add code to cope with different length numbers and so forth but get the main code working first.

It sound complicated, but it's exactly the same process you were taught years ago to add two numbers together! :laugh:
 
Share this answer
 
You are also putting your sum values at the wrong end of the array, by using the variable t as the index, but counting from zero. And rather than calculating the values in each statement it would be clearer if you converted each character to its integer value for all the calculations; something like:
C++
int num1 = number[j] - '0';
int num2 = bnumber[j] - '0';
int total = num1 + num2 + carry;
sum[j+1] = total % 10 + '0';
carry = total / 10;

You can then easily step through the code with your debugger to see exactly what happens.
 
Share this answer
 
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

Advice:
- prepare a few trees showing different cases.
- Run your program on debugger with theses examples trees.

You should find pretty quickly what is wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900