Thread: simple program, simple error? HELP!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    simple program, simple error? HELP!

    Hello all. I am a beginning programmer, taking my first programming class in college this semester. I have a program that

    it's supposed to:
    first: read the data from an outside file, data1.dat and find the average of all the entries in column 3 when the number in column1=1, or ignore the row if column1=0

    or

    find the maximum value of column 3 if column2=1
    find the minimum value of column 3 if column2=0


    it has some problem in the code, and when asked, my professor could not determine what it is. I'm kind of at a loss on this one.
    the program I wrote is as follows:

    Code:
    /* Homework 6 */
    
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "data1.dat"
    
    int main()
    
    {
    
    /* declare variables */
    
    int points=0;
    int c1, c2;
    double c3, ave, max, min, sum=0.0;
    int flag0=0, flag1=0;
    FILE *list;
    
    /* open file */
    
    list = fopen(FILENAME, "r");
    
    /* read file */
    
    while ((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)
    
     {
    
     /* compute the average, minimum and maximum */
    
     if (c1 == 1)
      {
      sum += c3;
      points++;
    
    
     if (c2 == 0)
       {
      if (flag0 == 0)
    
       flag0 = 1;
       min = c3;
    
        if (c3 < min) min = c3;
       }
    
     if (c2 == 1)
       {
      if (flag1 == 0)
    
       flag1 = 1;
       max = c3;
    
        if (c3 > max) max = c3;
       }
      }
     }
    
    /* compute ave */
    
    ave=sum/points;
    
    /* print a summary */
    
      printf("average value: %7.2f \n", ave);
      printf("maximum reading: %7.2f \n", max);
      printf("minimum reading: %7.2f \n", min);
    
    fclose(list);
    
    return 0;
    
    }
    any help is GREATLY appreciated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your problem is due to bad indentation

    Code:
    /* Homework 6 */
    
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "data1.dat"
    
    int main()
    
    {
    
    	/* declare variables */
    
    	int points=0;
    	int c1, c2;
    	double c3, ave, max, min, sum=0.0;
    	int flag0=0, flag1=0;
    	FILE *list;
    
    	/* open file */
    
    	list = fopen(FILENAME, "r");
    
    	/* read file */
    
    	while ((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)
    
    	{
    
    		/* compute the average, minimum and maximum */
    
    		if (c1 == 1)
    		{
    			sum += c3;
    			points++;
    
    
    			if (c2 == 0)
    			{
    				if (flag0 == 0)
    
    					flag0 = 1;
    				min = c3;
    
    				if (c3 < min) min = c3;
    			}
    
    			if (c2 == 1)
    			{
    				if (flag1 == 0)
    
    					flag1 = 1;
    				max = c3;
    
    				if (c3 > max) max = c3;
    			}
    		}
    	}
    
    	/* compute ave */
    
    	ave=sum/points;
    
    	/* print a summary */
    
    	printf("average value: %7.2f \n", ave);
    	printf("maximum reading: %7.2f \n", max);
    	printf("minimum reading: %7.2f \n", min);
    
    	fclose(list);
    
    	return 0;
    
    }
    do you see some problems now?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I have been working on this all evening, but still can't get the file to read in correctly. I know you guys probably hate beginners like myself taking up space on these bords, but I'm stumped.


    Code:
    /* Homework 6 */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "data1.dat"
    
    int main()
    
    {
    
    	/* declare variables */
    
    	int points=0;
    	int c1, c2;
    	double c3, ave, max, min, sum=0.0;
    	int flag0=0, flag1=0;
    	FILE *list;
    
    	/* open file */
    
    	list = fopen(FILENAME, "r");
    
    		if(list == NULL)
    		{
    		printf("error reading file \n");
    		}
    			else
    			{
    
    			/* read file */
    
    			while(!feof(list))
    			{
    			fscanf(list, "%i, %i, %lf", &c1, &c2, &c3);
    
    
    			/* compute the average, minimum and maximum */
    
    				if (c1 == 1)
    				{
    				printf("%i, %i, %lf \n", c1, c1, c3);
    	 			sum += c3;
    				points++;
    	  			printf("sum: %lf \n", sum);
    	  			printf("points: %i \n", points);
    
    	 				if (c2 == 0)
    					{
    	  					if (flag0 == 0)
    						{
    	   					flag0 = 1;
    	   					min = c3;
    	    					}
    	    						if (c3 < min) min = c3;
    	   				}
    
    	 				if (c2 == 1)
    					{
    	  					if (flag1 == 0)
    	   					{
    	   					flag1 = 1;
    	   					max = c3;
    	    					}
    							if (c3 > max) max = c3;
    					}
    				}
    			}
    		}
    
    			/* compute ave */
    
    			if(points == 0)
    			{
    			printf("error, cannot divide by zero \n");
    			}
    				else
    				{
    				ave=sum/points;
    
    				/* print a summary */
    
    				  printf("average value: %7.2f \n", ave);
    				  printf("maximum reading: %7.2f \n", max);
    				  printf("minimum reading: %7.2f \n", min);
    
    	   			}
    
    fclose(list);
    
    return 0;
    
    }
    I also tried while((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) == 3)
    and while((fscanf(list, "%i, %i, %lf", &c1, &c2, &c3)) !=EOF)

    with similar results. my conclusion is the file is not being read in correctly, I think It would return correct numbers if I could get it to read the file.

    input file consists of:

    data1.dat:
    1 0 3.4
    0 0 1.3
    1 1 3.1
    1 0 0.4
    1 1 7.9
    1 1 4.2
    1 0 -2.1

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. your indentation once gain suxxes - havn't you seen what problems it could make? (in VS Ctrl+A, Alt+F8)
    2. your format does not match your data - there is no , between numbers
    3. there is warning about using notinitialized max,min vars - to fix them initialize these vars
    4. do not use feof to control loop - there is a FAQ which explin why

    Code:
    /* Homework 6 */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "data1.dat"
    
    int main()
    
    {
    
    	/* declare variables */
    
    	int points=0;
    	int c1, c2;
    	double c3, ave, max = -1, min = -1, sum=0.0;
    	int flag0=0, flag1=0;
    	FILE *list;
    
    	/* open file */
    
    	list = fopen(FILENAME, "r");
    
    	if(list == NULL)
    	{
    		printf("error reading file \n");
    	}
    	else
    	{
    
    		/* read file */
    
    		while(fscanf(list, "%i %i %lf", &c1, &c2, &c3) == 3)
    		{
    
    
    			/* compute the average, minimum and maximum */
    
    			if (c1 == 1)
    			{
    				printf("%i, %i, %lf \n", c1, c1, c3);
    				sum += c3;
    				points++;
    				printf("sum: %lf \n", sum);
    				printf("points: %i \n", points);
    
    				if (c2 == 0)
    				{
    					if (flag0 == 0)
    					{
    						flag0 = 1;
    						min = c3;
    					}
    					if (c3 < min) min = c3;
    				}
    
    				if (c2 == 1)
    				{
    					if (flag1 == 0)
    					{
    						flag1 = 1;
    						max = c3;
    					}
    					if (c3 > max) max = c3;
    				}
    			}
    		}
    	}
    
    	/* compute ave */
    
    	if(points == 0)
    	{
    		printf("error, cannot divide by zero \n");
    	}
    	else
    	{
    		ave=sum/points;
    
    		/* print a summary */
    
    		printf("average value: %7.2f \n", ave);
    		printf("maximum reading: %7.2f \n", max);
    		printf("minimum reading: %7.2f \n", min);
    
    	}
    
    	fclose(list);
    
    	return 0;
    
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Thank you, vart. it was as simple as two misplaced commas.

    Thanks also for a lesson on indenting. My professor doesn't indent at all, he pretty much just threw us to the wolves. It makes it way easier to read with the tab indentations.
    My prof actually told us *not* to use tab, because it wouldn't work in the compiler. I guess I'll never trust him on stuff again, because obviously it worked.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. I need help on a formula for this simple program.
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-28-2002, 10:01 PM