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;
}
#include <iostream>
#include <cstdlib>
int lastLargestIndex(int [], int);
int main()
{
constint 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;
}