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

Thread Starter

sausages

Joined Mar 8, 2007
23
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:
#include <iostream.h>
void main(void)
int set = 0;
{
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 " << double one << ".";
cout << " Your maximum number must be " << double one << ", you've only entered one! " endl;
break
}
else if ( set != 1 );
cout << " Please enter " << set << " numbers. ";
else
cout << "\a";

double number = 0;
int count = 0;
double max = 0;
double total = 0;
cin >> number
for (count = 1; count <= set; count ++)

{
while (! good);
{
if (number >= 0 );
good = 1
else if ( number <= 0 );
good = 1
else cout << "\a";
}
if ( number <= max );
max = max;
else;
{
max = number;
count << " Your new maximum number is " << max << ".";
}
total = total + number;
if ( count == 1 );
cout << " You have entered your 1st number. ";
else if ( count == 2 );
cout << " You have entered your 2nd number. ";
else if ( count == 3 );
cout << " You have entered your 3rd number. ";
else
cout << " You have entered your << count << "th number. ";
}
cout << " You have finished entering your set. ";
cout << " Your maximum inputted value = " << max << ".";
cout << " Your average value was " << total/set << "." endl;

break;
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
 

blazedaces

Joined Jul 24, 2008
130
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
 

Thread Starter

sausages

Joined Mar 8, 2007
23
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
 
Top