Thread: Averaging two different numbers/loop?

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    3

    Averaging two different numbers/loop?

    Alright so I have a homework question that I'm a little confused about. I'm pretty new to C/programming in general, so this is probably actually really simple...
    I just don't understand how you would have it count how many negatives and positives there are based on the user's input?
    If anyone could assist me in any way that would be super great!

    Here's the problem and the code it provides:

    Modify the program so that is displays the average of the positive and negative numbers. (Hint: be careful not to count the number 0 as a negative number.)

    Code:
    #include <stdio.h>
    #define MAXNUMS 5
    int main()
    {
    int i;
    float number;
    float postotal = 0.0f;
    float negtotal = 0.0f;
    
    for (i =1; i <= MAXNUMS; i++)
    {
        printf("Enter a number (positive or negative) : ");
        scanf("%f", &number);
        if (number > 0)
          postotal += number;
        else 
          negtotal += number;
    }
    printf("\nThe positive total is %f", postotal);
    printf("\nThe negative total is %f\n", negtotal);
    
    
    return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If I were to give you these numbers:
    -7, 2, -2, -5, 5, 4, 5, -6, 0, 4

    Just by hand, how would you work out what the average of the negative and positive numbers without including 0.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I am guessing zero is NOT supposed to count as a negative or positive number from what the OP posted; but, it might be counted as a positive number by many people.
    I would suggest asking if it is supposed to be figured in the positive number average.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    I understand how to average, I just don't understand how to get the amount of negatives/positives the user inputs. If I just divide by the total of 5 that's given in the code already, that wouldn't give me the correct average since there aren't always going to be 5 negatives or 5 positives... my question is how do get that specific number of how many negatives/positives they enter so I can divide by it and get the correct average.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    my question is how do get that specific number of how many negatives/positives they enter so I can divide by it and get the correct average.
    Are you sure you're supposed to find separate averages for positive and negative, and not the average of all values entered?

    If so, then just create some variables to act as counters.

    Hint: You're already executing separate bits code depending on whether the input is positive or negative. Perhaps these bits of code could be expanded upon.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Xeydrian View Post
    I understand how to average, I just don't understand how to get the amount of negatives/positives the user inputs.
    For this you normally use a counter. Here is some pseudocode

    Code:
    nitems = 0
    while there is more input:
        get an input item
        ++nitems
    printf "The user input %d items", nitems

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    I think that's what I needed. I created a few new variables to count that and one to calculate the average itself. My program works properly now! It wasn't averaging correctly when I was messing around with it earlier.

    Thanks for the responses everyone and taking the time to try to help me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Averaging numbers using a sentinal to stop the loop
    By jesterisdead in forum C Programming
    Replies: 9
    Last Post: 04-13-2014, 02:05 AM
  2. Averaging numbers in a while loop?
    By Chinnie15 in forum C Programming
    Replies: 3
    Last Post: 12-01-2011, 10:13 PM
  3. Array problem averaging numbers
    By Thedon in forum C Programming
    Replies: 6
    Last Post: 10-27-2011, 11:27 AM
  4. averaging numbers?
    By rock4christ in forum C Programming
    Replies: 12
    Last Post: 09-09-2006, 06:38 PM
  5. averaging random numbers
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-27-2001, 03:48 PM