help! reading from file and computing average

This is a discussion on help! reading from file and computing average within the C Programming forums, part of the General Programming Boards category; hello i am about a month into a computer programming 1 class at a university...i need to write a program ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    help! reading from file and computing average

    hello i am about a month into a computer programming 1 class at a university...i need to write a program that gets data from a file and computes the total number,average and max and min....i've started this program, but now im stuck. im not sure how the loop goes or if i even have the beginning part right...here is my code if anyone has any suggestions or hints i would really appreciate it. thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int temp, ;
       int tempcount = 0;
       FILE *tempsIn;
       
       tempsIn = fopen("c:\\documents and settings\\desktop\\temps.txt", "r");
       
       printf("Temperature Analyzer v1.1\n");
       printf("\n");
       printf("What file to analyze?");
       
       if( tempsIn == NULL ) {
          printf("File could not be opened ... exiting.\n");
          system("pause");
          exit(EXIT_FAILURE);      // quit program
       }
       while(fscanf(tempsIn, "%2d%2lf", &temp);
          if ( 
       
       
       
       system("pause");
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're going to use two format specifiers in your fscanf statement, you need two variables to put the information into.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    For average, add them all up after reading them, with say sum += temp, while incrementing a count to divide the sum by the count.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    so i've worked on things a little more but i still can't get it to print average
    here is my code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    int main()
    {
       char filename[100];
       int tempcount = 0;
       double nextVal;
       double total = 0.0;
       FILE *tempsIn;
       
       tempsIn = fopen("c:\\documents and settings\\christina kestler\\my documents\\temps.txt", "r");
       
       printf("Temperature Analyzer v1.1\n");
       printf("\n");
       printf("What file to analyze?");
       scanf("%lf", "c:\\documents and settings\\christina kestler\\my documents\\temps.txt");
       
       if(tempsIn == NULL ) {
          printf("File could not be opened ... exiting.\n");
          system("pause");
          exit(EXIT_FAILURE);      // quit program
       }
       while(fscanf(tempsIn, "%2.0lf", &nextVal) != EOF) {
          tempcount++;
          total += nextVal;
       }
       
       fclose(tempsIn);
       if (tempcount == 0)
          printf("No values in file.\n");
       else
          printf("Average of %lf values read is %.3lf\n", tempcount, total);
       system("pause");
       return 0;
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In order to print the average, you must first compute the average.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would say
    Code:
    total/=tempcount;
    will work but you have some other problems in this code -- I don't think you should use formatting in your fscanf input. Just use %lf. You can always format the output.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    is it valid to say

    Code:
       while(fscanf(tempsIn, "%2.0lf", &nextVal) != EOF)
          tempcount++;
          (total += nextVal) / total;
    for computation of the average?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cakestler View Post
    is it valid to say

    Code:
       while(fscanf(tempsIn, "%2.0lf", &nextVal) != EOF)
          tempcount++;
          (total += nextVal) / total;
    for computation of the average?
    No. (Even if you managed to get both statements inside the while loop. Still no.) Presumably division is not such a stranger to you?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    no i am not a stranger to division i am a math major, i am a stranger to c programming, i suck i know, but i've reread my book and notes about 10 times now and im still not getting it

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you still think that
    Code:
    fscanf(tempsIn, "%2.0lf", &nextVal
    is going to work, then just leave it that way. Otherwise lose the 2.0

    You will want to wait until after the loop before you compute the average. "x/=y" is shorthand for "x=x/y"

    I would also observe that if you are formatting your numbers this way, they are small enough to fit into a normal float and don't need to be double.
    Last edited by MK27; 02-12-2009 at 03:04 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cakestler View Post
    no i am not a stranger to division i am a math major, i am a stranger to c programming, i suck i know, but i've reread my book and notes about 10 times now and im still not getting it
    So why do you think that "sum / n" (which is what you want) is somehow the same as thing as (sum / sum), if not sum / sum / sum / sum ... / sum (divided n times, once each time through the loop)?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Code:
    while(fscanf(tempsIn, "%2.0lf", &nextVal) != EOF) {
          tempcount++;
          total += nextVal;
    }
    The total already have the total of all the values ( assuming grades ). Since total already have the sum of all the grades then after that you just have to divide the sum of all the grades and divide it to how many grades were retrieved.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    ok so i have changed a few things and have my program running and working almost perfectly. i am haveing 2 problems. there are 30 temperatures in my file, yet my program says there are 31, also my average is off by about a .5, i thought that it might have something to do with the count being off, but even computing the average with the incorrect count, the average is still off by a little. here is my code if anyone has any suggestions or hints. thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
      char inFileName[160];
      int x;
      int tempcount = 0;
      int max = 0;
      int min = 10000;
      double sum = 0;
      FILE *tempsIn;
      
       printf("Temperature Analyzer v1.1\n");
       printf("\n");
       printf("What file to analyze?\n");
       scanf("%s",&inFileName);
      
       tempsIn = fopen("c:\\documents and settings\\christina kestler\\desktop\\temps.txt", "r");
     
       if (inFileName == NULL) {
       printf("File could not be opened.\n");
       system("pause");
       exit(EXIT_FAILURE);    
      }
      
      while(!feof(tempsIn))
      {
        fscanf(tempsIn,"%d",&x);
        tempcount++;
        sum += x;
        
        if(x > max)
        max = x;
        
        if(x < min)
        min = x;
      }
      
      double avg = sum / tempcount;
      
      printf("# of daily temps: %d\n", tempcount);
      printf("Average daily temp: is %0.5lf\n", avg);
      printf("Highest temp: %d\n", max);
      printf("Lowest temp: %d\n", min);
      
      system("PAUSE");	
      return 0;
    }

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So after you read the last temperature, feof is still false, so your loop won't stop.

  15. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    do i need a
    Code:
    fclose(tempsIn);
    ???
    if so that isn't working.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  4. a file problem !!
    By girlzone in forum C++ Programming
    Replies: 17
    Last Post: 04-15-2003, 07:55 PM
  5. Counting # of elements in array or file.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 04:33 AM

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