Using Arrays to Determine the Greatest Value

I am trying to determine the greatest value among the 25 values inputted from the user. However, the code seems to only output 24 rather than the index of the highest value. Any suggestions? (also, I deleted the post thinking that I found the problem, but I did not). Help would be much appreciated.

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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <ctime>

using namespace std; 

int secondary_figures_highest(int input[], int j);

int primary_figures_highest(int input[])
{
	int highest_index; 
	for(int j = 0; j < 25; j++)
	{
		highest_index = secondary_figures_highest(input, j);
	}
	return highest_index; 
}

int secondary_figures_highest(int input[], int j)
{
	int index_of_highest_value = 0; 
	for(int i = 0; i < 25; i++)
	{
		if(input[i] <= input[j])
		{
			index_of_highest_value = j; 
		}
	}
	return index_of_highest_value; 
}

int main() 
{	
	int input[24]; 
	int highest_index; 
	for(int i = 0; i < 25; i++)
	{
		cin >> input[i]; 
	}
	cout << (primary_figures_highest(input)) << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>

int main() 
{	
	int arr[5]; /////  5 values arr[0] to ar[4]; never use arr[5]
	for(int i = 0; i <5; i++)
	{
		std::cin >> arr[i]; 
	}
	std::sort(arr,arr+5); // <algorithm> library sort, ascending
	
	std::cout << "greatest value = " << arr[4];
	
	return 0;
}
I don't know why you need two functions to accomplish this, and the names of those functions don't provide much in the way of illumination.

On line 36 you define an array of 24 int, but you're trying to store and access 25 values. That is a problem.
Topic archived. No new replies allowed.