Hi guys! Thanks for the tips, I did it quite alright with your suggestions!
I ended up reading the numbers and coping them in a bi-dimensional array. There I looped summing, storing only (sum/10) in the sum vector and carrying (sum%10) over the next sum. Felt kinda dumb afterwards, my previous attempt was really lame :|. Meh, ain't gonna make the same mistake next time anyway!
Here's some code:
Code:
/*Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
*SEE FILE ATTACHED** */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char *argv[]) {
int i, x=0, y=0, sum=0, carry=0, result[500];
char num[100][50], row[60];
FILE *fp;
//File opening
fp=fopen("problem13.txt", "r");
if (fp==NULL) {
printf("Error: could not open file problem13.txt.\nPress any key to exit.\n");
getch();
return (1);
}
while(fgets(row, 60, fp) != NULL) {
for(i=0; i<=50; i++) {
num[y][i] = row[i];
}
y++;
}
memset(result, 0, sizeof(result));
sum = carry = 0;
for(i=49; i>=0; i--) { //i points to column
sum += carry;
for(y=0; y<100; y++) { //y points to row
sum += num[y][i]-48;
}
carry = sum/10;
result[x] = sum%10;
x++;
sum = 0;
}
fclose(fp);
printf("\nPress any key to continue.\n");
getch();
return (0);
}
I found the debugger quite useful with this program - I'm starting to understand the potential of that tool. I actually used it to get the solution, I was too lazy to write the for loop required to read the first ten digits, so I just read the values of my variables from the debugger and it worked out quite alright!
edit: lol I just realized that code is a real mess.