
Help!
I have an assignment due soon and I have no idea where to start and what to do. This is the assignment as written by my teacher:
Write a program that reads two positive integers that are 20 or fewer digits in length and then ouputs their sum. Your program will read the digits as values of type char so that the number 1234 is read in as the four characters '1', '2', '3', '4'. After they are read into the program that characters are changed to values of type int. The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elments in the array ater the data is read. Perform the addition by implementing hte usual pencilandpaper algorithm. The result of the addition is stored in an array the same size as the input arrays and the result is written to the screen. If the result of the addition is an iteger with more than the maximum number of digits, the program should print a message saying it has encountered "integer overflow".
My teacher did not teach us much about arrays and I am so lost. any help on any oft he points in this problem would be really reaqlly apreciated!

> Perform the addition by implementing hte usual pencilandpaper algorithm.
So do you know how to add two numbers on paper or not?
Code:
0 1 2 3 4 < array indices
++++++
  1  2  3  4  < number digits
++++++
   5  6  7 
++++++
     
++++++
You need 3 arrays, each 20 chars long


>> Any reason you didn't continue those threads?
Just for the note, this isn't the issue here.

I'm totally stuck. It's like my brain is frozen or something, probablly has do due with the fact that I'm sick. I can't even think how to read in the character arrays and then count how many digits there are in that array.

Were you confused by my previous suggestions? I know they are somewhat cryptic but it is important that you try to read thoroughly and ask for clarification when necessary.
Read in the entire number into the character array, not one character at a time. For example, doing this once (not in a loop) is all you need to do to read in the first 20 digit number: Code:
cin >> FirstNumber;
Do the same for the second number. Then, perhaps use strlen or a loop to find out how long the two character arrays are. That will tell you where to start lining up the two numbers as in Salem's picture. Get that working first, and then move on to further things.

This should help you get started:
Code:
int maximum_array_size = 260; // You shouldn't have a number bigger than this.
char array[maximum_array_size]; // Your array
int array_size = 0; // The size of your array, not yet figured out.
memset(array, 0, maximum_array_size); // Wipe the array clean.
cin << array; // Get the user input.
// Find the size of the array:
while(array_size < maximum_array_size && array[array_size] != 0)
{
array_size++;
}
// There, now we have the amount of characters in the array in array_size.
// And an example on accessing the array to begin with is also above.
I may have my operators pointing the wrong way, it's been a while sence I've coded outside windows. :p

Okay got this so far:
Code:
#include <iostream>
using namespace std;
const int Size = 20;
int main ()
{
char FirstNumber [Size];
char SecondNumber [Size];
int Length;
int Length2;
cout<<"Enter the First Number : ";
cin>>FirstNumber;
cout<<"Enter the Second Number : ";
cin>>SecondNumber;
Length = strlen (FirstNumber);
Length2 = strl (SecondNumber);
return 0;
}
Now how can I convert the characters into integers?

You can't have a number 20 characters long in an int type, the number would be WAY to big.
You will have to do the math manualy.


That's a great start. Does it compile? Make sure it compiles and runs before moving on.
Now, you already had the conversion of digits to integers part correct before, you just have to do it in the right order.
So you have to figure out how to loop from the end of the frst number and the end of the second number (like adding of 4 and 7 in Salem's picture. The problem is that the actual picture is like this: Code:
FirstNumber:
0 1 2 3 4 < array indices
++++++
 1  2  3  4 \0  < number digits
++++++
SecondNumber:
0 1 2 3 4 < array indices
++++++
 5  6  7 \0  ? 
++++++
You have to add FirstNumber[3] to SecondNumber[2], and move to the left through bopth arrays. In this example, you know that you start with 3 and 2 because you subtract 1 from strlen and strlen(FirstNumber) is 4 and strlen(SecondNumber) is 3.

I tried to do what I had before for converting the charcters thsi si what I put:
Code:
#include <iostream>
using namespace std;
const int Size = 20;
int main ()
{
char FirstNumber [Size];
char SecondNumber [Size];
int Length;
int Length2;
cout<<"Enter the First Number : ";
cin>>FirstNumber;
cout<<"Enter the Second Number : ";
cin>>SecondNumber;
Length = strlen (FirstNumber);
Length2 = strlen (SecondNumber);
int Num1 [Size] = FirstNumber [Size]  '0';
int Num2 [Size] = SecondNumber [Size]  '0';
return 0;
}
But I get an error saying : error C2440: 'initializing' : cannot convert from 'int' to 'int [20]'

okay I got the error to stop. This is what I have now:
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];
cout<<"Enter the First Number : ";
cin>>FirstNumber;
cout<<"Enter the Second Number : ";
cin>>SecondNumber;
Length = strlen (FirstNumber);
Length2 = strlen (SecondNumber);
Num1 [Length] = FirstNumber [Size]  '0';
Num2 [Length2] = SecondNumber [Size]  '0';
cout<<Num1 [Length  1];
return 0;
}
But I know this isn't working because when it returns Num1 [Length  1] as some trash number and not the actual number

I always save Homework assignments posted here, to make em myself later on :)
(self study :D )

Ok, I realized that you can do this two ways, you can convert the character arrays into integer arrays in reverse first, and then add, or you can convert directly first, then do the add in reverse. Code:
FirstNumber:
0 1 2 3 4 < array indices
++++++
 1  2  3  4 \0  < number digits
++++++
SecondNumber:
0 1 2 3 4 < array indices
++++++
 5  6  7 \0  ? 
++++++
OR: Code:
FirstNumber:
0 1 2 3 4 < array indices
++++++
 1  2  3  4 \0  < number digits
++++++
SecondNumber:
0 1 2 3 4 < array indices
++++++
 0  5  6  7 \0  ? 
++++++
Doing it the first way makes the conversion to integer array easier. You just make two loops, one for each number. The loops should just loop through each character (from 0 to length1) and convert that character to an integer with the  '0' trick. Then adding is a little trickier because you have to lineup the end digits.
The second way is a little harder to move the character into digits, but it will make the adding easier.
If you go with the first way, try to make the loops. Remember you are looping through each character in the character array, then subtracting '0' and assigning the result to the same spot in the integer array.
If you go with the second way, you have to identify which number is longer, and then use that as the last digit in both integer arrays. Then just loop backwards through the character arrays assigning the appropriate value. You should work this out on paper before trying to code it.