C++ int Array not returning largest value

Hello All,

First - I am just a beginner at this (just started studying C++ this semester) so I am lost somewhat sometimes and I think I am making good headway with my first array, but, I can not get it to return any output in my console window. I'm somewhat positive in the way the code has written as I have debugged all the syntax errors. I cannot simply get the program to read the output or display an output. I was hoping you might have some suggestions. Also, could you please tell me what may need to be fixed in this array.

Problem:

Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. Also, write a program to test your function.

Code Solution:
#include <iostream>

using namespace std;
//*****************************DECLARATION ARRAY*******************************************

int lastLargestIndex(int [], int);

void main()

{
int arr[15] = {5,198,76,9,4,2,15,8,21,34,99,3,6,13,61,};
int location;
location = lastLargestIndex(arr, 15);
cout << "The last largest index is:" << location << endl;

}

//*****************************LOOPS INTO FOR LARGEST OCCURANCE*******************************************

int lastLargestIndex(int arr[], int size)

{
int lastLargestIndex = 15;
int tem = arr[0];
int i;
for(i = 0; i < size; i++)
{
if (arr[i] > tem)
{
lastLargestIndex = i;
tem = arr[i];
}
}
//*****************************RETURNS TO THE LARGEST NUMBER*******************************************
system ("pause");
return lastLargestIndex;
}

Thanks,
Neil
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
#include <iostream>
#include <cstdlib>
 
int lastLargestIndex(int [], int);
 
int main()
{
   const int N = 15;
   int arr[N] = {5,198,76,9,4,2,15,8,21,34,99,3,6,13,61,};
   int location;

   location = lastLargestIndex( arr, N );

   std::cout << "The last largest index is:" << location << std::endl;
 
   std::system ("pause");

   return 0;
}
 
int lastLargestIndex( int arr[], int size )
{
   int maxIndex = 0;

   for( int i = 0; i < size; i++ )
   {
      if ( arr[maxIndex]  <= arr[i] )
      {
         maxIndex = i;
      }
   }
   
   return maxIndex;
}
 
Last edited on
Topic archived. No new replies allowed.