Print number of times an input was given, and take the average of the inputs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vileoxidation
    New Member
    • Apr 2008
    • 2

    Print number of times an input was given, and take the average of the inputs

    Hello, and thanks for any help in advance!

    Basically, as the title says, I am looking for a way to print the number of times the user gave the program an input, and then also print the average of all the inputs.

    So far, I have initialized three variables as follows:

    Code:
    int counter=0;
    int sum=0;
    float average=0;
    Do I need to use a loop to do this? I have also written the expressions for the three variables:

    Code:
    sum = sum + size;
    counter = counter + 1;
    average = sum/counter;
    I have tried printing the values of counter and average, and I get a negative counter value and a nonense average value.

    So, in summary, do I need a loop to print the number of inputs and their average? Also, using a loop or not, how do I get the second three lines of code to work correctly?

    Thank you in advance for any help!!
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Yes you need to declare size and since you are keeping track of sum, count and average you need to control with a loop. You also need to input something with cin>>.At the moment the three variable = 0 and you are also dividing by 0

    Comment

    • vileoxidation
      New Member
      • Apr 2008
      • 2

      #3
      I already have declared size, and have an input already set for size, and the program, which prints squares, works perfectly except for this part. I just didn't want to post too much code.

      But how do I actually set up the loop using them? That is the problem I am having.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        You could do something like this:
        [CODE=cpp]
        cout<<"Enter an integer";
        for(int i=0;i<20;i++)
        {cin>>integer;
        numberIntegersE ntered+=1;
        sum+=integer;
        averageSum=sum / numberIntegersE ntered;
        cout<<"The average of the integers entered is: "<<averageSum<< endl;
        //if this was a function you could add:
        return numberIntegersE ntered;//and display this value
        [/CODE]

        Comment

        Working...