Counting

This is a discussion on Counting within the C Programming forums, part of the General Programming Boards category; I am trying to figure out how to count loops to do an average of numbers input. It is a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    19

    Counting

    I am trying to figure out how to count loops to do an average of numbers input. It is a lab for a college class so I will accept only hints if neccessary. I can use a "while" statement to accept all the inputs the user enters but I cannot seem to figure out how to count how many the user inputs so I can divide by that to get the average of the inputs. Would appreciate any help I can get. Thank you.

  2. #2
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    Quote Originally Posted by rculley1970
    I am trying to figure out how to count loops to do an average of numbers input. It is a lab for a college class so I will accept only hints if neccessary. I can use a "while" statement to accept all the inputs the user enters but I cannot seem to figure out how to count how many the user inputs so I can divide by that to get the average of the inputs. Would appreciate any help I can get. Thank you.

    Do you have any code so far? If so, post it. While it's for class, we can't 'give' you the answer, like you stated above, but yes we can hint to it.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
    int count = 0;
    int total = 0;
    while(1) {
        /* get input from user */
        /* add input value to total */
        if ( /* user entered a value to indicate no more inputs */ )
            break;
        count++;
    }
    /* output the calculated average */

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    Here is the code that i came up with to input some numbers and then try to average them. The only problem i really have right now is how to count the loops so i can divide it as an average.
    Code:
    #include <stdio.h>
    main()
    {
    	float n, answer, average = 0, loop = 0, sum = 0.0;
    		printf("This program averages grades.\n");
    	printf("Please enter the grades: \n\n\n");
    	scanf("%f", &n);
    	
    	
    	while (n >= 0)
    	{
    	sum += n;
    	scanf("%f", &n);
    	}
    	printf("%.2f \n", n);
    	printf("the sum is:  %.2f \n", sum);
    	average = sum / n;
    	printf("average is: %.2f \n", average);
    	return 0;
    }

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Add an int count variable like I suggested, and put a count++ in your loop to increment it each time they enter a number. Divide sum by count instead of n. (dividing by n in your case is pointless, because it's the last value entered).

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    thank you, let me work on it.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Hmm...I'm going to try to say this without making it too obvious so that you can figure out what I mean yourself, but if you need more help figuring out what I mean let me know.

    There's a flaw in your programing that has to do with the order that things are done in. Remember that your program will execute starting at the top and going down line by line until it hits that return at the end of the program.

    Also, for my own curiosity, I don't understand the need to have that line just after the while loop, in which you print the last number that was obtained...it seems to me like it would make more sense to print each number as it is obtained, so the output of the code is all them numbers that were entered, and then the sum and then the average. If this is the case then you should reconsider where you put that printf statement.

    Hope this helps, ask if you need more help.
    -Crazed

    EDIT: just to let you know I purposly didn't answer the question you posted because the answer posted above is probably the easiest way to do it

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    Thank you CWR, the hint helped and i figured it out. here is the code:
    Code:
    #include <stdio.h>
    main()
    {
    	float n, average = 0, count = 0, sum = 0.0;
    		printf("This program averages grades.\n");
    	printf("Please enter the grades: \n\n\n");
    	scanf("%f", &n);
    	while (n >= 0)
    	{
    		++count;
    	sum += n;
    	scanf("%f", &n);
    	}
    	printf("%.2f \n", count);
    	printf("the sum is:  %.2f \n", sum);
    	average = sum / count;
    	printf("average is: %.2f \n", average);
    	return 0;
    }
    Did this change take into your consideration CrazedBrit? I took the loop int out.

    I am just trying to take in any number of int or float and display the sum and average of them. This worked so far. too late at night to keep working on it so goodnight and thank you for your help everyone.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Ah, you're right. As usual I wasn't thinking clearly Excellent job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting up(and down) using recursive function. Please help.
    By MarkSquall in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2008, 04:26 AM
  2. How to implement reference counting with 'Smart_ptr'?
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 06-10-2007, 05:28 AM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-04-2005, 11:46 PM
  5. Counting using Lib Cstring
    By niroopan in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 04:51 PM

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