Lab help

I need to write a C++ program, with a function called lastLargestIndex that takes as parameters an int array and its size and returns the index of the last ocurrence of the largest element in the array. Also, write a function to test your code.
Start by writing some code. Nobody here will do your work for you.

Start by creating an array and a function lastLargestIndex that finds the last element in the array
closed account (Dy7SLyTq)
well make a counter, have a for loop step through the array, with a variable holding the largest so far. at each iteration, test to see if the current value is still bigger than the current value of the array
Something like this would work, you'd have to learn from this and write your own function to test your code

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
#include <iostream>
#include <algorithm>
using namespace std;

void lastLargestIndex(int foo[], int size){
	int largest = 0;
	int index = 0;
	for (int i=0; i <= size; i++) {
		if ( foo[i] > foo[i-1] ) {
			largest = foo[i];
			index = i;
		}
	}
	cout << "Last occurance of highest number was" << largest << " at index: " << index << endl;
}

int main () {
	int SIZE = 50;
	int* foo = new int[SIZE];
	for (int i=0; i <= SIZE; i++) {
		foo[i] = i;
	}
	lastLargestIndex(foo, SIZE);
}


@sh129951: Unless you get lucky, that is...
Last edited on
closed account (Dy7SLyTq)
well i didnt want to write code for him cause its homework
Its YoUr LuCky DaY. I'm just trying to stay frosty for my interview in 4 days. Sorry! I'll just do the programs in private from now on and post what I found for the user.
Last edited on
...write a C++ program...
1
2
3
4
int main()
{
    return 0;
}

...with a function called lastLargestIndex that takes as parameters an int array and its size and returns the index...
1
2
3
4
5
6
7
8
9
10
int lastLargestIndex(int arrayOfThings[], int sizeOfArrayOfThings)
{
    int theCorrectIndex;
    //figure out correct index
    return theCorrectIndex;
}
int main()
{
    return 0;
}

...write a function to test...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int lastLargestIndex(int arrayOfThings[], int sizeOfArrayOfThings)
{
    int theCorrectIndex;
    //figure out correct index
    return theCorrectIndex;
}
void testMyCode()
{
   //write up a test that exercises lastLargestIndex
   //perhaps instead of void, return some sort of thing to indicate a test result
}
int main()
{
    return 0;
}


All of that code falls out word for word from the description (and it compiles!). Use this approach on this and future assignments to get some traction. Then, when you hit a snag, you'll have some code to show the forum and get better answers. (This start I've given is pretty generous, and I expect to get some backlash from other posters. And I'll probably agree with them!)
Last edited on
Topic archived. No new replies allowed.