Hello, I working on this program for school which reads two numbers from a file and then breaks the numbers down to single integer array elements (less than 10). Then I take the two different arrays and add each element together and if the answer result in an integer higher than 10 I carry to one to the next Array element. All this works fine (although Im sure there is a better way). However my problem is that my professor wants us to shift the elements of the array to the right. For example you have an array[10] . your given the number 456. where A[0] =4, A[1] = 5, A[2] = 6,. But how do you shift it so that A[0-7] are = 0 and
A[7] =4 [8] = 5, [9] =6 so on and so forth.. Here is what I have:
Thanks I kind of understand that. But I have run into other problems.. this is problem I am working on.
Assume that your computer has the very limited capability of being able to read and write only single-integer digits and to add two integers consisting of one decimal digit each. Write a program that can read two integers up to 30 digits each, add these integers together, and display the result. Test your program using pairs of numbers of varying lengths.
So This is what I have.. but its not working.. I figure because the first for loop is adding to the first array when there is nothing there ?
The way you are reading in the number out of the file is not correct. Let's get Numbers1 and Numbers2 read in first and then you can just call the shiftIt function in the post above.
Here is your code with some debug print statements:
#include <iostream>
#include <fstream>
usingnamespace std;
#define in_file "data.txt"
int main()
{
ifstream ins;
int num=0,count =0;
int Num1[10]={0}, Num2[10]={0};
int Numbers1[10]={0}, Numbers2[10]={0}, results[10]={0};
char ch;
ins.open(in_file);
while (!ins.eof()) {
ins.get(ch);
while (ch!='\n') {
//----------------------------------------------
// Don't want to have a for loop here
// How do you know that the lenght of the
// number in the file is 10 characters long?
//----------------------------------------------
for (int i(0); i<=9; i++) {
num= int(ch)-48;
Numbers1[i] = num;
//-------------------------------------------
// Print out what you are reading in for the
// numbers in the file
//-------------------------------------------
cout << "Numbers1["<<i<<"] = " << num << endl;
ins.get(ch);
count++;
}
for (int x=0; x<= count; x++) {
for (int y=9; y>=count; y--) {
Num1[y] = Numbers1[x];
for (int v= count-1; v >= 0; v--) {
Num1[v] = 0;
}
}
}
for (int i = 0; i<10; i++) {
cout << Num1[i];
}
cout << endl;
}
cout << endl;
ins.get(ch);
for (int j(0); j<10; ++j) {
num= int(ch)-48;
Numbers2[j] = + num;
cout << "Numbers2["<<j<<"] = " << num << endl;
ins.get(ch);
}
}
cout << endl;
for (int r=0; r<10; r++) {
results[r] = Numbers2[r] + Numbers1[r] + results[r];
if ( results[r] > 9) {
results[r+1] = + 1;
results[r] = results[r] - 10;
}
else {
results[r] = Numbers2[r] + Numbers1[r];
}
cout << results[r];
}
cout << endl;
}
$ ./a.out
Numbers1[0] = 1
Numbers1[1] = 2
Numbers1[2] = 3
Numbers1[3] = 4
Numbers1[4] = 5
Numbers1[5] = -38 <--- This is were is goes wrong
Numbers1[6] = 5
Numbers1[7] = 6
Numbers1[8] = 7
Numbers1[9] = 8
0000000000
Numbers1[0] = 9 <--- Notice here you are back in the for loop
Numbers1[1] = 0 because you read in 10 characters and
Numbers1[2] = -38 came back to the top of the while loop
Numbers1[3] = -38 and ch != '\n' was true, so more data
Numbers1[4] = -38 was loaded into Numbers1. Not what
Numbers1[5] = -38 you want.
Numbers1[6] = -38
Numbers1[7] = -38
Numbers1[8] = -38
Numbers1[9] = -38
0000000000
Numbers2[0] = -38 <--- Numbers2 never has a chance to get
Numbers2[1] = -38 the correct information because the
Numbers2[2] = -38 first loop consumed all of the data in
Numbers2[3] = -38 the file. All that was left was the '\n'
Numbers2[4] = -38
Numbers2[5] = -38
Numbers2[6] = -38
Numbers2[7] = -38
Numbers2[8] = -38
Numbers2[9] = -38
-29-38-76-76-76-76-76-76-76-76
You have a for loop to read in the numbers and they go from 0 to 9 but you don't have numbers that are exactly 10 characters in length. So, you can see from the debug print outs that you read in the first 5 numbers and then you get some crazy number like -38. That -38 = 10-48. The number 10 in a newline character and that is the issue. What you should do is use the loop [icode]while (ch!='\n')[/icode] and remove the for loop so when you see the '\n' character you stop reading into Numbers1. Something like:
while (!ins.eof()) {
ins.get(ch);
// If the character read is not a newline or space or eof
// we are still reading the first number in the file
while (ch != '\n' && ch != ' ' && !ins.eof()) {
num= int(ch)-48;
Numbers1[count] = num;
cout << "Numbers1["<<count<<"] = " << num << endl;
ins.get(ch);
count++;
}
// Call the shift function above
shiftIt(Numbers1,count);
count = 0;
ins.get(ch);
// Use while loop but for array Numbers2
// ...
// Call the shift function above
shiftIt(Numbers2,count);
// Do the adding
// ....
// Print the result
Edit: Fixed Numbers1 index. Was i should have been count. And added the setting of count back to zero when done with Numbers1.
You are correct. I should have not used i as the index for the Numbers1 array. It should be count. I changed the code above to reflect the correct value. Also set count back to zero when done shifting Numbers1. I just used count because it was there.
You might not want to count but make two new variables count1 and count2. The reason is because you might want to know the number of elements in each array when you go to add them.