Display highest number

I am new to programming and im stuck on a part of my program where i have to display the highest number out of 5 numbers.

cout << "Enter num 1" << endl;
cin >> num1;

cout << "Enter num 2" << endl;
cin >> num2

........This is an example of how it is setup but with 5 numbers instead of 2.
I dont know where to start in displaying the highest number.
You need to either store them all in an array then iterate over the array to find the highest.
but an easier (and very unelegant and lazy ) way would be to do something like this:

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()
{
	// define a int with a very low value
	int highestNumber = 0;
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	cout << "Enter num 1" << endl;
	cin >> num1;

	if (num1 > highestNumber)
	{
		highestNumber = num1;
	}

	cout << "Enter num 2" << endl;
	cin >> num2;

	if (num2 > highestNumber)
	{
		highestNumber = num2;
	}

	cout << "Enter num 3" << endl;
	cin >> num3;

	if (num3 > highestNumber)
	{
		highestNumber = num3;
	}

	cout << "Highest number: " << highestNumber << endl;

	return 0;
}

How large is the largest number before any numbers have been given?
You could answer: "There is no such value", but that is not practical here. Therefore, lets set the value to something.

What would be a good value? Obviously every number given by the user must be larger or equal to that initial guess. So small, very small.

Ok, now we have a variable that holds the highest value.

Lets read one number from the user. That number is either higher than highest, or not. In the former case the highest has to be updated.

Repeat that previous step as many times as you need.

Now show the value that the 'highest' has.
Thank you! Ill take the lazy way, it works fine for my program.
use a for loop, lazier way.
Here, Watch, try it and learn mr Marquise! ;)

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// DisplayHighestNumber.cpp 

#include <iostream>

using namespace std;

//Function Declarations
void BubbleSort(int numbers[]);

const int SIZE = 6;

int main()
{
	int numbers[SIZE];
	int i = 0;
	int numOfElements = 0;
	int input;

	cout << "Enter number: " << endl;

	while (i < SIZE - 1)
	{
		cin >> input;
		cout << "Enter number: " << endl;
		numOfElements++;
		numbers[i] = input;
		i++;
	}

	for (int i = 0; i < numOfElements; i++)
	{
		cout << numbers[i] << endl;
	}

	BubbleSort(numbers);
	cout << "\nAfter sort:" << endl;
	for (int i = 0; i < numOfElements; ++i)
	{
		cout << numbers[i] << endl;
	}
	return 0;
}

//Function Definitions

void BubbleSort(int numbers[])
{
	// The outer loop, going through all list
    for(int i = 0; i < 5; i++)
	{
		//Inner loop, going through element by element
		int nrLeft = 5 - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (numbers[j] < numbers[j+1]) // Compare the elements
			{
				swap(numbers[j], numbers[j+1]); 
			}
		}
	}
}
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// DisplayHighestNumber.cpp 

#include <iostream>

using namespace std;

const int SIZE = 5;

int main()
{
	int numbers[SIZE];
	int i = 0;
	int numOfElements = 0;
	int input;

	cout << "Enter number: " << endl;

	while (i < SIZE)
	{
		cin >> input;
		cout << "Enter number: " << endl;
		numOfElements++;
		numbers[i] = input;
		i++;
	}

	for (int i = 0; i < numOfElements; i++)
	{
		cout << numbers[i] << endl;
	}

	for(int i = 0; i < numOfElements; i++)
	{
		for(int j = 0; j < numOfElements; j++)
		{
			if(numbers[j]<numbers[j+1])
			{
				int hold=numbers[j];
				numbers[j]=numbers[j+1];
				numbers[j+1]=hold; 
			}
		} 
	} 
	
	cout<<"Sorted Array is: "<<endl;
 
	for(int i = 0; i < numOfElements; i++)
	{
		cout << "Numbers: " << numbers[i] << endl; 
	}

	return 0;
}




Topic archived. No new replies allowed.