C++ exercise

We introduce n numbers. The 2 highest ones need to be shown.
e.g.: n=4, numbers: 2, 10, 4, 8. the numbers displayed: 10, 8.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

int main()
{
	int arr[1000];
	int temp, n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> arr[i];
	}
	cout << endl;
	cout << "***************Without sort***************" << endl;
	cout << endl;
	for (int i = 0; i < n; i++)
	{
		cout << arr[i] << endl;
	}
	for (int i = 0; i < n - 1; i++)
	{
		for (int j = 0; j < n - 1; j++)
		{
			if (arr[j] < arr[j + 1]) {
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	cout << endl;
	cout << "****************With  sort****************" << endl;
	cout << endl;
	for (int i = 0; i < n; i++)
	{
		cout << arr[i] << endl;
	}
	cout << endl << "***************Two large number***************" << endl << arr[0] << endl << arr[1];
	return 0;
}


OR

But here a single number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main(){
	int a,temp = 0;
	int arr[1000];
	cin >> a;
	for (int i = 0; i < a; ++i)
	{
		cin >> arr[i];
		if (arr[i] > temp)
		{
			temp = arr[i];
		}
	}
	cout << temp;
	return 0;
}
Topic archived. No new replies allowed.