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;
}

Thank You,
Neil

The for loop only executes ONCE.

You've almost got it right.
Note that it should be int main() and pausing at line 35 achieves nothing.

Here's your code with some corrections.

#include <iostream>

using namespace std;

int lastLargestIndex(int [], int);

int main()
{
    int arr[15] = {5,198,76,9,4,2,15,8,21,34,99,3,198,13,61,};
    int location = lastLargestIndex(arr, 15);

    if (location != -1)
        cout << "The last largest index is: " << location << endl;

    // pause to read the output
    cout << "\npress Enter to exit...";
    cin.get();

    return 0;
}

int lastLargestIndex(int arr[], int size)
{
    if (size == 0)
        return -1;

    if (size == 1)
        return 0;

    // set variables to the first index
    int lastLargestIndex = 0;
    int tem = arr[0];

    // start the loop from the 2nd index (1)
    for (int i = 1; i < size; ++i)
    {
        if (arr[i] >= tem) // note >= so we get the last occurrence
        {
            lastLargestIndex = i;
            tem = arr[i];
        }
    }

    return lastLargestIndex;
}

Oop. Ignore my post above. I read your code too fast before running for lunch and didn't check it.

re: firstPerson's code

int lastLargestIndex(int arr[], int size)

{
    int lastLargestIndex = 0;
    int tem = arr[0];
    int i;
    for(i = 0; i < size; i++)
    {
        if (arr[i] > tem)
        {
            lastLargestIndex = i;
            tem = arr[i];
        }
    }
    return lastLargestIndex;
}

Consider if the array has duplicate entries.

int arr[15] = {5,11111,76,9,4,2,15,8,21,34,99,3,6,13,11111};

Using (arr[i] > tem) will return the last highest index being index 1, because the value of arr[14] is not greater than the value of arr[1].

That's why you need if (arr[i] >= tem)

@nullptr, nvmd didn't realize the 'last' word in 'lastLargestIndex'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.