Can't manipulate variable!

This is a discussion on Can't manipulate variable! within the C Programming forums, part of the General Programming Boards category; So here I am writing this code and I am having problems with something basic, but I think I am ...

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

    Can't manipulate variable!

    So here I am writing this code and I am having problems with something basic, but I think I am overlooking something simple. Reading a text file I had to come up with arithmetic sum and root-mean-square average. the arithmetic sum is fine but the root-square-mean isn't correct because after the first while loop i can't change it.

    Code:
    Code:
    -4.1 -2.2 -0.5  1.2  3.3  4.6  5.1  2.1  0.2 -3.6 -4.1  0.2
     0.5  2.2  4.1 -0.2 -1.2 -3.3 -4.6 -5.0 -2.2 -1.1  0.8	3.2
    -0.1 -4.8
    And here is the Code

    Code:

    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>
    
    int main()
    {
    FILE 		*inp;
    double	value, 	
    			mean,
    			root_mean_square,
    			sum=0,
    			max_height;
    int 		numValue=0,
    			count;
    		
    
    inp = fopen("surface.txt", "r");		
    
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    	{
    		  sum += fabs(value);
    		  numValue++;
    		  
    	}
    
    mean = sum / numValue;
    
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    	{
    		  sum += pow( fabs(value), 2 );
    		  numValue++;
    		  
    	}
    
    root_mean_square = sqrt( sum / numValue);
    
    
    
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    	{
    		double max=-10000, min=10000;
    
    				
    				if (value >= max)
    						max = value;
    				else 
    						min = value;
    							
    		;
    			max_height = max - min
    	}
    		
    
    	
    printf( "Arithmetic mean value %.2lf\n", mean);
    printf( "Root-mean-square average %.2lf\n", root_mean_square);
    printf( "Maximum roughness height %.2lf\n", max_height);					
    
    
    
    
    
    
    
    return (0);
    }
    i am getting
    Code:
    64.500000
    Arithmetic mean value 2.48
    Root-mean-square average 1.58
    Maximum roughness height 1889828851749420549890302903237191694835027000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00
    No matter what I do to

    Code:
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    	
    			sum =+ pow ( (fabs(value)), 5);
    I can't manipulate the sum , it stays at 64.5000000 from the first calculation in

    Code:
    while ( ( fscanf ( inp,"%lf", &value ) ) == 1 )
    	{
    		  
    		  sum += fabs(value);
    		  numValue++;
    		  
    	}
    
    mean = sum / numValue;

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    At the end of your first WHILE loop, you have fscanf()'ed all the way through the file. You leave the loop because you hit end of file.

    In your second (and subsequent) WHILE loops, you are still sitting at EOF.

    edit: I would do everything in one pass.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    How many times do you set numValue = 0?
    How many loops to you increment it in?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

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