To Add Arrays

This is a discussion on To Add Arrays within the C++ Programming forums, part of the General Programming Boards category; Hi. I am working on a stubs only miniature program for school concerning adding integers much too large to be ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    56

    To Add Arrays

    Hi. I am working on a stubs only miniature program for school concerning adding integers much too large to be stored as integers. What I have so far seems to work great up until I add 10 integers to 10 integers. Here is what I have so far, and kindly thank anyone who has time to give me a bit of advice. I apologize for the lack of documentation in this program - I haven't really gotten that far yet.

    Code:
    const int MAXCHARS = 80;
    
    char numbers[MAXCHARS];
    int result[MAXCHARS];
    char num1[MAXCHARS];
    char num2[MAXCHARS];
    
    char op;
    int splitPoint;
    int opFinder = 0;
    int count1 = 0;
    int count2 = 0;
    int x;
    
    int ar1Length;
    int ar2Length;
    int maxLength;
    int temp1;
    int temp2;
    int tempResult;
    int carry1 = 0;
    int carry2;
    
    int main()
    {
    
    	cout << "This program adds or multiplies numbers" << endl
    		<< "much too large to be stored as integers." << endl
    		<< "Please refer to the example to enter your problem." << endl << endl
    		<< "(EXAMPLE) 100 + 10" << endl << endl
    		<< "Calculate: ";
    
    	cin.getline(numbers, MAXCHARS, '\n');
    
    	for(opFinder = 0; opFinder < MAXCHARS; opFinder++)
    	{
    		if((numbers[opFinder] == '+') || (numbers[opFinder] == '*'))
    		{
    			splitPoint = opFinder;
    			op = numbers[opFinder];
    		}
    	}
    	while(isdigit(numbers[count1]))
    	{
    		num1[count1] = numbers[count1];
    		numbers[count1] = '\0';
    		num1[count1+1] = '\0';
    		cout << num1[count1] << endl;
    		count1++;
    	}
    	
    	ar1Length = count1-1;
    	num1[count1] = '\0';
    
    	count1 = 0;
    	count2 = splitPoint + 1;
    
    	while(numbers[count2] != '\0')
    	{
    		if(numbers[count2] == ' ')
    		{
    			count2++;
    		}
    		num2[count1] = numbers[count2];
    	
    		numbers[count2] = '\0';
    		num2[count1 + 1] = '\0';
    
    		count1++;
    		count2++;
    	}
    	ar2Length = count1-1;
    
    	num2[count1] = '\0';
    
    	if(ar1Length > ar2Length)
    	{maxLength = ar1Length;}
    	else
    	{maxLength = ar2Length;}
    
    	cout << "You have entered: " << endl
    		<< setw(maxLength+2) << setfill(' ') << num1 << endl
    		<< op << setw(maxLength+1) << setfill(' ') << num2 << endl;
    
    	for(x = 0; x <= maxLength+2; x++)
    	{cout << "-";}
    	cout << endl;
    
    	x = 0;
    
    	while((num1[x] != '\0') || (num2[x] != '\0'))
    	{
    		if((num1[x] == '\0') && (num2[x] == '\0'))
    		{break;}
    		else if(num1[x] == '\0')
    		{temp1 = 0;}
    		else if(num2[x] == '\0')
    		{temp2 = 0;}
    		else
    		{
    			temp1 = atoi(&num1[x]);
    			temp2 = atoi(&num2[x]);
    		}
    		
    		if((temp1+temp2) > 9)
    		{
    			tempResult = (temp1 + temp2) &#37; 10;
    			carry1 = (temp1 + temp2) / 10;
    
    		}
    
    		tempResult = temp1 + temp2 + carry2;
    		result[x] = tempResult;
    		num1[x] = '\0';
    		num2[x] = '\0';
    		result[x+1] = '\0';
    		cout << result[x] << endl;
    
    		temp1 = 0;
    		temp2 = 0;
    
    		carry2 = carry1;
    	}
    	return 0;
    }
    Last edited by marQade; 02-15-2008 at 03:55 PM. Reason: removed a dummy variable declaration

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Hopefully you have some header files, such as iostream for cout and cctype for isdigit(), and whatever atoi() is in -- cstdlib, I think. I never use it, because of its nonexistent error checking. I suggest you look at strtol() at some point in the future for better error checking.

    I'm also wondering if you ought to be incrementing x in your last while loop there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> ar1Length = count1-1;
    >> ar2Length = count1-1;
    Are you sure those are right? I don't think you need the -1's there.

    What if the user inputs two spaces in a row after the plus sign?

    >> while((num1[x] != '\0') || (num2[x] != '\0'))
    You only set the character after the last digit in num1 and num2 to null. So what if you're adding 123456 + 12? Then the while might not finish. If you want to make that the control, you should ensure that all values of x will be null after the number in the arrays.

    >> if((num1[x] == '\0') && (num2[x] == '\0'))
    That's unnecessary given the while condition right above it.

    >> else if(num1[x] == '\0')
    >> else if(num2[x] == '\0')
    If either of these two run, then temp2 or temp1 will not get set for that iteration.

    >> temp1 = atoi(&num1[x]);
    >> temp2 = atoi(&num2[x]);
    Why the & there? I assume you're trying to make atoi happy since it expects a null terminated array, but then you'll get the whole number in the array, not a single digit. You're just trying to convert a digit, so use this: temp1 = num1[x] - '0';

    >> tempResult = temp1 + temp2 + carry2;
    Why do you overwrite the value of tempResult that you just calculated in the if above it?

    Are you sure this working with any input? Try to look at some of those issues (and the x increment dwks pointed out), verify that they really are problems, and see if you can come up with solutions for them.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    . . . and iomanip, for setfill().

    You don't really need to make those variables global, since you only have one function. Although, again since you only have one function, it probably doesn't make too much difference.

    And since Elysia's going to say it, I'll say it first: your indentation is a little hard to read, where you have both curly braces on one line.

    I don't think you need quite so many variables. You should be able to use just one carry variable, for instance.

    Code:
    	while(isdigit(numbers[count1]))
    	{
    		num1[count1] = numbers[count1];
    		numbers[count1] = '\0';
    		num1[count1+1] = '\0';
    		cout << num1[count1] << endl;
    		count1++;
    	}
    	
    	ar1Length = count1-1;  // what if count1 is zero?
    	num1[count1] = '\0';
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    It would definitely be easier to read if you broke it into several functions which each do one specific task.
    Switching to STL strings & vectors would probably make things easier too.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Thank you for all your comments. I've fixed up some of the stuff from my stubs program, and got it working okay as far as that. Here is the code for the stubs only with NO error-trapping and NO provision for arrays of different lengths. I tried to convert this into a three part program, with a header file, definition file, and main function, adding some complexity, but I'm having some trouble adding arrays of different sizes. I've attached the three files for your perusal. Thanks again.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cctype>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
    
    const int MAXCHARS = 80;
    
    char numbers[MAXCHARS];
    int result[MAXCHARS];
    char num1[MAXCHARS];
    char num2[MAXCHARS];
    
    char op;
    int splitPoint;
    int opFinder = 0;
    int count1 = 0;
    int count2 = 0;
    int x;
    
    int ar1Length;
    int ar2Length;
    int maxLength;
    int temp1;
    int temp2;
    int tempResult;
    int carry = 0;
    
    	cout << "This program adds or multiplies numbers" << endl
    		<< "much too large to be stored as integers." << endl
    		<< "Please refer to the example to enter your problem." << endl << endl
    		<< "(EXAMPLE) 100 + 10" << endl << endl
    		<< "Calculate: ";
    
    	cin.getline(numbers, MAXCHARS, '\n');
    
    	for(opFinder = 0; opFinder < MAXCHARS; opFinder++)
    	{
    		if((numbers[opFinder] == '+') || (numbers[opFinder] == '*'))
    		{
    			splitPoint = opFinder;
    			op = numbers[opFinder];
    		}
    	}
    	while(isdigit(numbers[count1]))
    	{
    		num1[count1] = numbers[count1];
    		numbers[count1] = '\0';
    		num1[count1+1] = '\0';
    		cout << num1[count1] << endl;
    		count1++;
    	}
    	
    	ar1Length = count1;
    	num1[count1] = '\0';
    
    	count1 = 0;
    	count2 = splitPoint + 1;
    
    	while(numbers[count2] != '\0')
    	{
    		if(numbers[count2] == ' ')
    		{
    			count2++;
    		}
    		num2[count1] = numbers[count2];
    	
    		numbers[count2] = '\0';
    		num2[count1 + 1] = '\0';
    
    		count1++;
    		count2++;
    	}
    	ar2Length = count1-1;
    
    	num2[count1] = '\0';
    
    	if(ar1Length > ar2Length)
    	{maxLength = ar1Length;}
    	else
    	{maxLength = ar2Length;}
    
    	cout << "You have entered: " << endl
    		<< setw(maxLength+2) << setfill(' ') << num1 << endl
    		<< op << setw(maxLength+1) << setfill(' ') << num2 << endl;
    
    	for(x = 0; x <= maxLength+2; x++)
    	{cout << "-";}
    	cout << endl;
    
    	x = 0;
    
    	while(x <= maxLength+2)
    	{
    		if(num1[x] == '\0')
    		{
    			temp1 = 0;
    		}
    		else if(num2[x] == '\0')
    		{
    			temp2 = 0;
    		}
    		else
    		{
    			temp1 = (num1[x] - '0');
    			temp2 = (num2[x] - '0');
    		}
    
    		tempResult = temp1 + temp2 + carry;
    		
    		if((tempResult) > 9)
    		{
    			tempResult = (temp1 + temp2 + carry) % 10;
    			carry = (temp1 + temp2) / 10;
    		}
    		else
    		{
    			carry = 0;
    		}
    
    		result[x] = tempResult;
    
    		num1[x] = '\0';
    		num2[x] = '\0';
    		result[x+1] = '\0';
    
    		temp1 = 0;
    		temp2 = 0;
    
    		x++;
    	}
    
    	cout << " ";
    
    	for(count2 = maxLength; count2 >= 0; count2--)
    	{
    		cout << result[count2];
    	}
    	cout << endl;
    
    
    
    	return 0;
    }
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    I may have to add the arrays backwards...hmm...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> if(num1[x] == '\0')
    >> else if(num2[x] == '\0')
    This is probably still a problem. If the sizes differ then inside the if or else if you are only setting one of temp1 or temp2, but you aren't setting the other.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Right under my nose!!
    Here I was playing with my count variables. I'll give this a try and get back to you - thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. VC++6.0 Add member function Wizard
    By 7stud in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2003, 07:48 PM
  3. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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