Thread: simple c code for min, max and mean

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    7

    simple c code for min, max and mean

    Hello,

    I am trying to get output of the min max and mean (to 2 decimal place) of an arbitrary number of floating point inputs, each separated by a newline, from the keyboard. The range of inputs is restricted from -100000 to 100000.

    Ex. Input: 7
    5.6
    6

    Output:

    5.60 7.00 6.20

    My code below is allowing me to input as many values as I want but not able to get any output. I apologize for the formatting and would appreciate insight on how to format code properly here for posts.

    #include<stdio.h>
    #include<math.h>

    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    int main(void)
    {
    	int r = 0;//for scanf value
    	float i = 0;//for input
    
    
    	float min = 100001;
    	float max = -100001;
    	int count = 1;
    	float total = 0;
    	float mean = 0;
    
    
    	do
    	{
    		r = scanf("%f", &i);
    		if (r == EOF)//when user ctrl-d EOFs, print output
    			break;
    		else if (r == 0)//if there is an input not of type float, skip it and go to next
    			continue;
    		
    		if (i < min)
    			i = min;
    		if (i > max)
    			i = max;
    		total = total + i;//running total
    		mean = total / count;
    		count++;
    		
    	}while(r != EOF);
    
    
    	printf("%.2f %.2f %.2f\n", min, max, mean);//print 2 decimal places
    
    
    	return 0;
    }
    Last edited by nyck66; 05-19-2017 at 10:39 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,552
    Look at this snippet:
    Code:
            if (i < min)
                i = min;
            if (i > max)
                i = max;
    It looks like you have your assignment backwards. Don't you want to assign the value to min or max not to i?

    Jim

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Maybe you mean
    min = i;
    max = i;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    7
    Here is my edited version (using a for loop now). However I still cannot get the output I want.

    Code:
    #include<stdio.h>
    #include<math.h>
    
    
    int main(void)
    {
            int r = 0;//for scanf value
    
    
            float min = 100001;
            float max = -100001;
    
    
            float total = 0;
            float mean = 0;
    
    
            for (int count = 1; r != EOF; count++)
            {
                    float i = 0;//for input
                    r = scanf("%f", &i);
                    if (r == EOF)//when user ctrl-d EOFs, exit loop
                            break;
    
    
                    if (i < min)
                            min = i;
                    if (i > max)
                            max = i;
                    total = total + i;//running total
                    mean = total/count;
    
    
            }
            printf("%.2f %.2f %.2f\n", min, max, mean);//print 2 decimal places
    
    
            return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,552
    However I still cannot get the output I want.
    With a given input, what output do you desire, and what is your program returning?


    Jim

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    7
    @ Jim, I want to input any number of floating point values, separated by newline, and the output the smallest value seen, the largest value seen and the arithmetic mean on a single line to two decimal places, separated by a space.

    Example Input:
    7

    5.6
    6

    Output:

    5.60 7.00 6.20

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    7
    I am sorry but my post did not get cleared by moderators.

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    7
    My code actually worked after revision.
    However, now I have a new question regarding how to ignore new lines in input using scanf?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Code Help
    By xbs22x in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2011, 03:45 AM
  2. Some Simple Code
    By YoelWarda in forum C++ Programming
    Replies: 6
    Last Post: 02-21-2010, 01:49 PM
  3. help with simple code
    By bwcal1999 in forum C++ Programming
    Replies: 1
    Last Post: 07-27-2004, 01:00 PM
  4. help with simple code (i think!)
    By JimJamJovi in forum C Programming
    Replies: 2
    Last Post: 01-10-2002, 10:53 AM

Tags for this Thread