Hey

I want to write a program that defines which is the biggest value between n numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
using namespace std;

int main ()
{
    int n,number,maximum;
    cin >> n;
    for (int cnt=1;cnt<=n;cnt++)
    {
        cin >> number;
        if (number>maximum)
            maximum=number;
    }
    cout << " " << maximum << endl;
    return 0;
}
what is your question?
Not really following your use of the for loop

Array:
1
2
3
4
5
6
7
	const int SIZE = 5;
	int num[SIZE];
	int largest = num[0];
	for (int i = 1; i < SIZE; i++){
		if (largest < num[i])
			largest = num[i];
	}


2 numbers:
1
2
3
4
5
	int num1, num2, max;
	if (num1 > num2)
		max = num1;
	else
		max = num2;


my question is:
how can I make my code work for n numbers (more than 2,a sequence of numbers) :)
your program seems to do that already.

You haven't included any cout's to make it human readable when the program runs but if you entered 3 first.

Then entered 6, 22, and 2. The ouput would be " " maximum or

22
Last edited on
Topic archived. No new replies allowed.