Designing a program to find max and average of a set using C++

Discussion in 'Programmer's Corner' started by sausages, Dec 8, 2008.

  1. sausages

    Thread Starter Member

    Mar 8, 2007
    23
    0
    I've been given an assignment in Uni to create a programme in C++ that can find the maximum and average of a set of values , inputted by the user. Now I've never done any C++ before so everythings a little new to me. I'm also at home today so I don't have visual c++ to test if everything's working properly. This is my code:
    As you can see there's probably a fair few errors in there but I just can't see them yet. The line ( for (count = 1; count <= set; count ++) ) is causing me a small bit of grief. Will the programme run everything in the count 1 loop and then increase the count by 1, or will it increase the count first. This would mean that the line "if ( count = 1 ); cout << " You have entered your 1st number. "; would never be seen.

    Thanks for any help , I need as much as I can get
     
  2. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    This is probably not the most efficient way I've seen to do that, but it look to me like it would work fine.

    I see absolutely no errors, so can you tell me what issues you're having with that specific for loop? Give what you want it to do, what it's outputting, and what, if any, errors you're receiving.

    As for your second question, count++ would add one AFTER the end of the for loop. You see how the ++ is written after the variable, that's called post-incrementation. If the ++ was written before the variable (like ++count) then it would happen BEFORE the for loop starts. That is called pre-incrementation.

    Does that make sense? In your case, count++ is indeed what you want and the if(count = 1) WILL be entered the first time.

    But the best way to tell if this works is to test it. Even if you don't have visual c++ I'm fairly certain there are some free (maybe not as user-friendly) c++ compilers you can download to test this out.

    I don't know one off the top of my head, but perhaps look it up or someone else might know. Good luck,

    -blazed
     
  3. sausages

    Thread Starter Member

    Mar 8, 2007
    23
    0
    This is the code I've done today,

    #include "stdafx.h"

    #include <iostream.h>
    void main(void)
    {
    int set = 0;
    double one;
    cout << "Type the amount of numbers you would like to enter: ";
    cin >> set;
    if (set == 1)
    {
    cout << " Please enter one number. ";
    cin >> one;
    cout << " Your average number is " << one << "."<< endl;
    cout << " Your maximum number must be " << one << ", you've only entered one! " << endl;
    }
    else if ( set != 1 )
    cout << " Please enter " << set << " numbers. ";

    double number = 0;
    int count = 1;
    double max = 0;
    double total = 0;
    int good = 0;
    cin >> number;
    for (count = 0; count <= set; count ++ )
    {
    total = total + number;
    if ( number > max )
    {
    max = number;
    cout << " Your new maximum number is " << max << "." << endl;
    }
    }


    cout << " You have finished entering your set. "<< endl;
    cout << " Your maximum inputted value = " << max << "."<< endl;
    cout << " Your average value was " << total/set << "." << endl;

    }


    break;
    }

    Say I want a set of 2, when I enter my first number it just rushes on down through the loop and prints my finishing cout statements i.e. the last 3.I know the problems in the count loop but I think I'm just being stupid :D
     
  4. sausages

    Thread Starter Member

    Mar 8, 2007
    23
    0
    wohoo I sorted out the problem. Of course I had to put the cin << number in the loop .. doh!
     
  5. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    Glad it's working.

    -blazed
     
Loading...