Need Help Writing a Sum of Digits program

This is a discussion on Need Help Writing a Sum of Digits program within the C Programming forums, part of the General Programming Boards category; Hey all. I need to use input and output files, and create a code such that the input value digit ...

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    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. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    you can start with writing the program that will read one file and write its content as is into another
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    yeah vart, I'm gonna see if I can do that first.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    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. #6
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    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);
    }
    else puts("FILE NOT FOUND");
    return 0;
    
    }
    It compiles just fine, but when I run it, I get "segmentation fault". Please help!
    Last edited by toadkiwi; 03-12-2008 at 10:50 PM.

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    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.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    I'm not sure what you mean.

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
    fscanf(ifp, "&#37;d", &i);
    That should take care of the seg fault.
    Last edited by NeonBlack; 03-12-2008 at 11:18 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Ack! Nice catch. Thanks.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you use gcc, add -Wall -pedantic to the command line, AND fix the warnings you get.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on Sum of Digits Entered
    By SkinnyK in forum C Programming
    Replies: 5
    Last Post: 05-14-2008, 05:16 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Writing a checklist program for MTGO?
    By CrYpTiC in forum Game Programming
    Replies: 2
    Last Post: 04-08-2005, 09:46 PM
  4. Help writing calculator program (newbie)
    By Danny 2 in forum C Programming
    Replies: 3
    Last Post: 07-25-2002, 06:44 AM

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