#include<iostream>
usingnamespace std;
int main() {
int number;
int highest = 0;
int max_num;
int num[max_num];
cout << "this program determines which number is the highest." << endl;
cout << "please enter the max number." << endl;
cin >> max_num; // this is the list of numbers you can write.
for (int i = 1;i < max_num;i++) {
cout << "enter numbers:" << endl;
cin >> num[i]; // write the list of numbers.
}
for (int i = 1;i < max_num;i++) {
if (num[i] > highest ) {
highest = num[i];
}
}
cout << highest << endl;
system("pause");
return 0;
}
hello there fellow programmers.I just need to ask a question.My program works the way it needs to,but I just have one question....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int highest = 0; // .....
for (int i = 0;i < max_num;i++) {
if (num[i] > highest ) {
highest = num[i]
}
}
how does this work? I discovered this by accident and I don't have any idea how these lines of the code works.So I reckoned 'highest' containes every value of the array.So I did and experimented.
1 2 3 4 5 6 7 8
for (int i = 0;i < 10;i++) {
cout << largest;
}
and the output was,,,
0
0
0
0
0
0
0
0
0
so no,it just stays as '0' through the whole loop.So how on earth does it work?I hope you understand my question.
This is where the magic happens, your "test" output using the variable largest makes no sense with the code you've posted thus far.
1 2 3 4 5 6 7 8 9
int highest = 0; //So highest == 0 right now
for (int i = 1;i < max_num;i++)
{
if (num[i] > highest ) //What happens if num[i] == 2 here, we enter the if block and assign highest to 2.
//Repeat this loop until max_num, if num[i] is greater than highest again assign highest that new value
{
highest = num[i];
}
}