# Thread: Need Help Writing a Sum of Digits program

1. ## Need Help Writing a Sum of Digits program

Hey all. I need to use input and output files, and create a code such that the input value digit sums are added and then output to a file. Here is the problem as given to me:

The file "original.txt" contains integer numbers in them. You are required to create another file, named "encoded.txt", from the original file.
Write a program "encoding.c" that reads every integer from the "original.txt" file and writes the integer and the sum of the digits of the
integer to the "encoded.txt" file.
The "encoded.txt" contains twice the number of integers compared to the "original.txt" file.

The contents of a sample "original.txt" and "encoded.txt" files are shown below.

original.txt file contents
1234
3527
9882
83421
23421

Corresponding encoded.txt file
1234 10
3527 17
9882 27
83421 18
23421 12
If anyone can help me out, get started it would be greatly appreciated. Our "hint" if you wanna call it that, was to use pointers and files...

2. you can start with writing the program that will read one file and write its content as is into another

3. So what's your thoughts? I would start by writing something that can read the input and produce the output WITHOUT the sum. That is a starting point. Then work on summing things up. This is something called "stepwise refinement", which is a useful technique for coming up with a solution to just about any problem - define the problem in several portions, and solve each portion.

--
Mats

4. yeah vart, I'm gonna see if I can do that first.

5. Also, how would you like to store your chars that are read in...as strings or separate chars in an array perhaps? Though strings would not sum very well for you, lol. Then from this array you can run it through a loop to sum up each element and then pipe the array contents back plus the summed up variable to your encoded.c file.

As many have suggested. The key points is to review the requirements that are needed and break it down to the simplest of parts. I remember back in my early days of coding exercises which actually was very little coding at all, was to break down all the steps that are needed to wake up in the morning and to brush your teeth. You would not believe how many pieces that can be broking down into!

With these snipits of tasks can you really begin to create psedo code, logistics, and then actually begin writing code.

6. Ok some reading and TA help later, I've got:

Code:
```#include <stdio.h>
#define INFILE "original.txt"
#define OUTFILE "encoded.txt"

int main(void)
{
int i,sum;
FILE *ifp,*ofp;
if ((ifp=fopen(INFILE,"r"))!=NULL) {
ofp=fopen(OUTFILE,"w");
while (!feof(ifp))
{
fscanf(ifp,"&#37;d",i);
sum=0;
fprintf(ofp,"%d ",i);
while (i)
{
sum+=i%10;
i/=10;
}
fprintf(ofp,"%d\n",sum);
}
fclose(ifp);
fclose(ofp);
}
return 0;

}```

7. I'm pretty sure that you need to pass the destination to the scanf family by reference.

I would comment on the rest of your program, but I can't read it.

8. I'm not sure what you mean.

9. Code:
`fscanf(ifp, "&#37;d", &i);`
That should take care of the seg fault.

10. Ack! Nice catch. Thanks.

11. If you use gcc, add -Wall -pedantic to the command line, AND fix the warnings you get.

--
Mats