Unable to print decimal part of the value. Can someone help?

This is a discussion on Unable to print decimal part of the value. Can someone help? within the C Programming forums, part of the General Programming Boards category; Hello, I recently created a program that basically gets all the test scores from an input file and prints out ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Unable to print decimal part of the value. Can someone help?

    Hello,
    I recently created a program that basically gets all the test scores from an input file and prints out the average of those scores.

    Now I am trying to add on to that program by creating a function that figures out the standard deviation and prints it out on the screen.

    I was successfully able to do this. The only problem I ran into is printing out the exact value of the standard deviation rounded to two decimal places. Right now, if the standard deviation is 6.48 its going to print out 6.00. If my standard deviation is 14.22 its going to print out 14.00....


    Can someone please help??





    Code:
    #include <stdio.h>
    #include <math.h>
    
    int Average(FILE *ifp, int num);
    float Standard(FILE *ifp, float num2, float themean);
    
    int main () {
    	
    	FILE *ifp;
    	ifp = fopen("test.txt", "r");
      
       int numScores, mean;
    
    
    	fscanf(ifp, "%d", &numScores);
    	printf("Your average for all the tests you took is %d.\n", Average(ifp, numScores));
       
       mean = Average(ifp, numScores);
       
       printf("The standard deviation is %.2f.\n", Standard(ifp, numScores, mean));
    	
    getch();
    return 0;
    }
    
    int Average(FILE *ifp, int num){
    
    	int i,z, average, sum=0;
    	int testScores [200];
    
    	for(i=0; i < num; i++){
    		fscanf(ifp, "%d", &testScores[i]);
    	}
       
       for(z=0; z < num; z++) {
    		sum=sum+testScores[z];
    	}
    
       average = sum /num;
       
       return average;
       
    
    }
    
    float Standard(FILE *ifp, float num2, float themean){
    	
    
    	int i=0, sum=0, z=0, result;
    	int MyScores[200];
    
    	for(i=0; i < num2; i++) {
    		fscanf(ifp, "%f", &MyScores[i]);
    		MyScores[i]=MyScores[i]-themean;
    	if(MyScores[i] < 0)
    		MyScores[i]=MyScores[i]*(-1);
    	MyScores[i]=MyScores[i]*MyScores[i];
    	}
    	
    	for(z=0; z < num2; z++){
    		sum = sum + MyScores[z];
    	}
    	result = sqrt(sum/(num2-1));
       
       return result;
    
    }
    My Txt doc.

    10
    88
    65
    72
    92
    100
    57
    70
    88
    90
    65

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
    float Standard(FILE *ifp, float num2, float themean){
    	int i=0, sum=0, z=0, result;
    ...
    	result = sqrt(sum/(num2-1));
       
       return result;
    
    }
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by matthayzon89 View Post
    Hello,
    I recently created a program that basically gets all the test scores from an input file and prints out the average of those scores.

    Now I am trying to add on to that program by creating a function that figures out the standard deviation and prints it out on the screen.

    I was successfully able to do this. The only problem I ran into is printing out the exact value of the standard deviation rounded to two decimal places. Right now, if the standard deviation is 6.48 its going to print out 6.00. If my standard deviation is 14.22 its going to print out 14.00....


    Can someone please help??
    Take a real close look at your variable declarations...
    At least one of them isn't right for the new application.

    Hint: Integers are whole numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return decimal number back caller function
    By andy09 in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 09:10 AM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 04:52 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  4. HOw Do I Print To A Specific part of the Screen
    By spyderwb in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2002, 11:25 AM

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