functions

This is a discussion on functions within the C Programming forums, part of the General Programming Boards category; I am trying to calculate 3 things from a data file using functions , I think something is wrong in ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    functions

    I am trying to calculate 3 things from a data file using functions , I think something is wrong in main() because I not returning the right results

    What do you guys think?

    Code:
    /* Christopher R Smith
    COMP1200 - Fall 2008
    Assignment 6
    Program: Average Value Program
    
    -------------------------------------------------------------------------------------------------------------
    Taking surface measurements from a text file, compute and display the Arithmetic Mean Value, the
    Root Mean Square Average, and the Max Roughness Height
    
    --------------------------------------------------------------------------------------------------------------
    */
    
    #include <stdio.h>
    #include <math.h>
    
    double rMeanValue ( double sumA, double value);
    double rSqAve (double sumS, double value);
    double rMaxHeight (double max, double min);
    
    int main()
    {
    
    FILE 		*inp;
    
    double	value, 	
    			mean,
    			root_mean_square,
    			max_height=0,
    			max,
    			min,
    			sumA=0,
    			sumB=0;
    
    
    inp = fopen("surface.txt", "r");
    
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )		
    		mean = rMeanValue( sumA, value);
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )	
    		root_mean_square = rSqAve ( sumB, value);
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    		max_height = rMaxHeight (max, min);
    	
    
    fclose(inp);		
    
    printf( "Arithmetic mean value    = %.2lf\n", mean);
    printf( "Root-mean-square average = %.2lf\n", root_mean_square);
    printf( "Maximum roughness height = %.1lf\n", max_height);	
    
    return(0);
    
    }
    
    
    
    /////////////////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////////////////////
    
    
    double rMeanValue ( double sum, double value)
    	{
    		double 	numValue1,
    					 mean;	
    		
    		
    				sum += fabs(value);
    				numValue1++;
    			
    			
    		mean = sum / numValue1;
    		
    		return mean;
    		
    	}
    
    /////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////
    double rSqAve (double sum, double value)
    	{
    		double 	numValue2,
    					root_mean_square;
    		
    						sum =sum + pow(value,2);
    		 				numValue2++;		  
     					  			
    	
    		root_mean_square = sqrt( (sum / numValue2) );
    	
    		return root_mean_square;	
    	}
    	
    
    /////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////
    
    double rMaxHeight (double max, double min)
    	{
    	
    		double 	value,
    					Max_Height;
    						
    		if (value >= max)
    			max = value;
    		if (value <= min)
    			min = value;
    				
    		Max_Height = max-min;
    
    		return Max_Height;		
    
    	}

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    Cool hi

    sorry to tell u,"max"and "min" are not assign !

    so call that compare function has no meaning!

    may be u need to read max and min from file inp?

    that's all.

    PS:u should tell what's wrong before submit ,so guys will be happy to tell u what's problem was happened.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by smithc2005 View Post
    Code:
    inp = fopen("surface.txt", "r");
    
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )		
    		mean = rMeanValue( sumA, value);
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )	
    		root_mean_square = rSqAve ( sumB, value);
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    		max_height = rMaxHeight (max, min);
    	
    
    fclose(inp);
    That doesn't do what you think it does. This will march through the entire file on the first while loop. Then the other two while loops will never be executed, because you're already through the file. You should do all three function calls in the same first while loop.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

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