Thread: Logical Error

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Smile Logical Error

    The purpose of this program is to read a .data file and calculate the average of the numbers listed there. The program then gives a recommendation based on the average by way of nested if statements. The problem I am having is that for certain .data files the output average is way out of proportion to the actual average. This has happened when the program meets the "This value is the result of faulty data" condition

    Here is the code:
    insert
    Code:
    #include <stdio.h>
    int main (void)
    {
    
    	FILE *f1;
    	double a, b, c, d, e, f, g, h, i, j, k, l, m, avg;
    
    
    	/* opening the file */
    	f1 = fopen ("density1.data", "r");
    	
    	/* read data */
    	fscanf (f1, "%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m);
    	/* calculate average */
    	avg = (a+b+c+d+e+f+g+h+i+j+k+l+m)/13;
    	fclose (f1);
    	/* First if condition for invalid data */
    	if (avg < 1.2 || avg > 2.2)
    		printf ("The data collected is faulty, there is no way that is reliable data! \n");
    	else
    			/* Second if condition for valid data */
    		if (1.2 < avg < 1.5)
    				printf (" The data collected suggests the site is usable with heavy \n compaction before building \n");
    		else
    			if ( 1.5 < avg < 2.2)
    				printf (" The data collected suggests the site is safe to build upon \n");
    								
    		printf (" The average is %5.3lf \n", avg);
    		
    return (0);
    
    }



    Thank you

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Luigi1!

    This should be something you can pinpoint right where the problem is, and no time is too soon to start working on simple troubleshooting - bugs in programs, "ain't goin' away", anytime soon.

    So:

    1) Check your input code: Is it ALL what it should be - add print statements and/or step through it and watch your values with the debugger.

    2) Check your arithmetic: Are you summing up the numbers right? Are you aware that integer division will round down, to the nearest integer value, always?

    Do you need to use a float or double data type, for the average to be more accurate?

    3) Are you printing up the values with the right printf() control (format) strings?

    If the above doesn't work, post up a sample that fails, from the data file, so we can see what's happening when it encounters this data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM