Addition array

This is a discussion on Addition array within the C++ Programming forums, part of the General Programming Boards category; In this program that I have written so far I am attempting to add two very large integers together using ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    Addition array

    In this program that I have written so far I am attempting to add two very large integers together using arrays:

    Code:
    #include <iostream> 
    using namespace std;
    
    const int Size = 20;
    int main ()
    {
    
    	char FirstNumber [Size];
    	char SecondNumber [Size];
    	int Length;
    	int Length2;
    	int Num1 [Size];
    	int Num2 [Size];
    	int Answer [Size];
    	
    	//getting numbers into character array
    	cout<<"Enter the First Number : ";
    	cin>>FirstNumber;
    
    	cout<<"Enter the Second Number : ";
    	cin>>SecondNumber;
    	
    	//finding length of the arrays
    	Length = strlen (FirstNumber);
    	Length2 = strlen (SecondNumber);
    
    	
    	//bringing numbers in reverse so that they can be added
    	for (int a = Length - 1; a >= 0; a--)
    	{
    		Num1 [a] = FirstNumber [a] - int ('0');//changing char to int
    			for (int a = Length2 -1; a >= 0; a--)
    			{
    				Num2 [a] = SecondNumber [a] - int ('0');
    			}
    			
    			Answer [a] = Num1 [a] + Num2 [a];//getting sum of two numbers in the arrays
    
    			if (Answer [a] > 10)//if answer of the two numbers is greter than ten, minus 10 and add ten to the next number
    			{
    				Answer [a] = Answer [a] - 10;
    				Answer [a--] = Answer [a--] + 1;
    			}
    			
    			
    			cout<< Answer [a];
    
    	}
    	
    	
    
    
    
    
    	return 0;
    }
    Everything works right untill the numbers add up to be bigger then 10 and then I am trying to figure out how to carry the one to the next number. Any suggestions?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Why do you reverse num2 every time ?

    Create two functions to
    - reverse a number string (which you call twice)
    - add two number strings, and store the result in a third
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    I am suppose to add each number in the array together one by one. If the two numbers add up to greater then ten I know I have to minus ten for that particular index place but how do I add one on to the next number?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    How do you add two numbers on paper?
    Use a 'carry', and add that on the next iteration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    I know that, but how do I do that in my program and get it to work? I tried to in my program above but that doesn't work.

  6. #6
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Without looking at the horrible, and probably wrong structure of your code (why are you even using an INNER for loop, with the SAME variable than the outer loop), the line that produces the biggest mistake is the following:

    Code:
    Answer [a--] = Answer [a--] + 1;
    That lines decrements a two times. Additionally, the number you want to increment is stored at Answer[a+1] and not a-1.

    The code is pretty hard to code in reverse, since the length of the two input strings is variable, so do as Salem suggested, reverse the two strings and add them from the start to the end and finally reverse the result once again.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    You know I am a beginner and I'm sorry I'm not as superior as some people here think they are but even if I wern't there is no reason to go insulting peoples work, whether it's wrong or not. If you are not going to offer help without being derogatory then you don't need to post at all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 12:50 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21