Like Tree1Likes
  • 1 Post By anduril462

Help on using a funtion

This is a discussion on Help on using a funtion within the C Programming forums, part of the General Programming Boards category; Repeat Project 3 with the modification to write a separate function to accept k, m, n (in that order) and ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Help on using a funtion

    Repeat Project 3 with the modification to write a separate function to accept k, m, n (in that order) and return the sum specified in the problem from the text. (A single, well-defined task)

    This is that original project that works...
    Code:
    #include <stdio.h>
    main()
    {
        int i,k,m,n,sum,e,ctr;
        ctr=0;
        FILE *payInFile, *payOutFile;
        payInFile=fopen("data3.txt","r");
        payOutFile=fopen("Pro3Out.txt","w");
        e=fscanf(payInFile, "%d %d %d", &k, &m, &n);
        fprintf(payOutFile,"     K      M      N    Sum\n");
        while(e==3)
        {
        sum=0;
        ctr=ctr+1;
        for(i=m;i<=n;i++)
          {
                if((i%k)==0)
                {
                          sum=sum+i;
                          i++;
                }
          }
          fprintf(payOutFile,"%6d %6d %6d %6d\n", k, m, n, sum);
          e=fscanf(payInFile,"%d %d %d", &k, &m, &n);
        }
        fprintf(payOutFile,"\n\nEnd of Program, it processed %d sets of data", ctr);
        fclose(payInFile);
        fclose(payOutFile);
        system("Pause");
    }
    This is the data it pulls in
    Code:
    13   22   054
    7   8   045
    013 023  032
    2  2   014
    5   15   59
    15   48  56
    7  248   267
    4  2  16
    19  234  735
    2   3   4
    11  456  957
    9 2 66
    7   39  0216
    3   75  106
    017  129  317
    Now this is the code Ive tried to modify using a function and I just cant seem to get it working right.

    Code:
    #include <stdio.h>
    int calcSum(int k, int m, int n);
    main()
    {
        int i,k,m,n,sum,e,ctr;
        ctr=0;
        FILE *payInFile, *payOutFile;
        payInFile=fopen("data3.txt","r");
        payOutFile=fopen("Pro4Out.txt","w");
        e=fscanf(payInFile, "%d %d %d", &k, &m, &n);
        fprintf(payOutFile,"     K      M      N    Sum\n");
        while(e==3)
        {
        sum=calcSum(k,m,n);
        ctr++;
          fprintf(payOutFile,"%6d %6d %6d %6d\n", k, m, n, sum);
          e=fscanf(payInFile,"%d %d %d", &k, &m, &n);
        }
        fprintf(payOutFile,"\n\nEnd of Program, it processed %d sets of data", ctr);
        fclose(payInFile);
        fclose(payOutFile);
        system("Pause");
    }
    
    int calcSum(int k, int m, int n)
    {
        int sum,i;
        for(i=m;i<=n;i++)
          {
                if((i%k)==0)
                {
                          sum=sum+i;
                          return sum;
                }
          }
    }
    Any help would be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,442
    You don't initialize sum in your function.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    So something like this?
    Code:
    int calcSum(int k, int m, int n)
    
    {
        int sum,i;
        for(i=m;i<=n;i++)
          {
           sum=0;                      //sum
    
                if((i%k)==0)
    
                {
                          sum=sum+i;
    
                          return sum;
                }
    
          }
    }
    I've tried this already to no avail. I know I'm missing sum=0; somewhere in this code.
    I know I'm close to but at this point I'm stumped.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You need to return sum at the end of the function, not inside a loop.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,442
    You need to initialize sum before the loop, not inside it. Right now, every time through the loop, you reset sum to zero.

    You need to think and trace through your code by hand, so you can envision what your code does as it runs. It may help to get some scratch paper to track the state of each variable after each individual instruction.
    rags_to_riches likes this.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    Thanks for your help. Finally got it and it took way longer than it should have haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A key press funtion
    By getinhigh4life in forum Game Programming
    Replies: 3
    Last Post: 08-08-2008, 10:58 PM
  2. funtion pow(x,y) problem
    By g_p in forum C Programming
    Replies: 7
    Last Post: 12-12-2006, 08:18 AM
  3. on funtion pointers
    By enggabhinandan in forum C Programming
    Replies: 11
    Last Post: 10-22-2006, 01:06 AM
  4. is this funtion ok?
    By kermit in forum C Programming
    Replies: 8
    Last Post: 08-22-2003, 05:28 PM
  5. swap funtion
    By ManicC in forum C Programming
    Replies: 9
    Last Post: 11-15-2001, 06:55 AM

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