First I will give you the code, then I wil explain.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int i=0;
int input=0;
int lowest = 0;
int highest = 0;
int total = 0;
cout << "How many numbers do you want to input?";
cin >> input;
if ((input < 1) || (input > 100)) {
cout << "You must use a number between 1 and 100." << endl;
return 1;
}
int *numbers = new int[input];
for (i=0; i < input; i++) {
cout << "Please enter entry " << i+1 << "." << endl;
cin >> numbers[i];
}
lowest = numbers[0];
for (i=0; i < input; i++) {
total += numbers[i];
if (numbers[i] > highest) {
highest=numbers[i];
}
if (numbers [i] < lowest) {
lowest=numbers[i];
}
}
cout << "Average: " << total/input << "." << endl;
cout << "lowest: " << lowest << " highest: " << highest << "." << endl;
delete []numbers;
return 0;
}
</iostream>
Now: here is my explaination:
int i=0;
int input=0;
int lowest = 0;
int highest = 0;
int total = 0;
As the code says, this just declares the variables. It is good to have your declarations at the top, because they are easier to track down.
cout << "How many numbers do you want to input?";
cin >> input;
if ((input < 1) || (input > 100)) {
cout << "You must use a number between 1 and 100." << endl;
return 1;
}
So, we get the input from the user, then do some error checking. This is something you'll want to get in the habbit of doing; when getting input from the user, check on some bounds. If not, you'll get some clever hacker that says, "I wonder what happens if I supply -1!
int *numbers = new int[input];
I know you haven't covered this, but this is one of the better ways to do it. Another way would be to add everything up, then do your calculations as your getting input; Iwill provide the code for this, as well.
Basically, an array is just a set of variables, which you will get to eventually, but it's a way to hold multiple variables of the same type, so you could do something like:
int tttBoard [3][3];
One thing you'll have to get used to that we use is starting at a 0 index, rather than one.
for (i=0; i < input; i++) {
cout << "Please enter entry " << i+1 << "." << endl;
cin >> numbers[i];
}
This is one of the loops you were asking about, so let me explain:
A for loop will do something in order until the condition is met, so lets split it apart.
for (i=0...
This basically sets your counter (i) to 0.
i < input
The loop will keep going while this condition is true. In otherwords, while i is less than input. When I is equal to input, the loop will finish.
i++
Fore each time the loop is ran, (otherwise known as an iteration), I is incremented.
So, to sum up: The loop starts I at zero, the n runs until I is less than input (which is the max amount of numbers the user requested). So, this works because as we stated earlier, arrays run off of a zero index.
lowest = numbers[0];
for (i=0; i < input; i++) {
total += numbers[i];
if (numbers[i] > highest) {
highest=numbers[i];
}
if (numbers [i] < lowest) {
lowest=numbers[i];
}
}
Again, we just run the loop over, with an if statement.
An if statement looks something like this:
if (condition)
{
do something
}
So, if the condition is met, for example:
if (numbers[i] < lowest)
which basically checks to see if the number is lower than numbers[i], and if so evaluates to true, we execute the code within the braces.
The rest of the code just deletes the array from the heap, prints the results, and returns 0.
I hope this helped. These are concepts that are not easily explained over a forum, but rather with a good book. I personally recommend thinking in C++, by Bruce eckel. It's a free download, just google the title. Skip chapter one (which just basically talks about OOP and will confuse you until later), and dig in.