C++ ONLY Need help with this program. Below is the assignment, program, output,
ID: 3675304 • Letter: C
Question
C++ ONLY
Need help with this program. Below is the assignment, program, output, and expected output, help me get the expected output please!
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.
#include <iostream>
#include <limits.h>
using namespace std;
void printArray(const int x[], int sizeX);
int lastLargestIndex(const int x[], int sizeX);
int main() {
int x[] = {6,3,6,4,6,4,6,4,7,4,7,8,4,7,4,2,2,6,8,5,7};
int n = sizeof(x)/sizeof(int);
printArray(x, n);
cout << "Last largest index: " << lastLargestIndex(x,n) << endl;
return 0;
}
void printArray(const int x[], int sizeX){
for(int i=0;i<sizeX;i++){
cout << x[i] << " ";
}
cout << endl;
}
int lastLargestIndex(const int x[], int sizeX){
int index = -1;
int max = -1*INT_MAX;
for(int i=0;i<sizeX;i++){
if(max < x[i]){
max = x[i];
index = i;
}
}
return index;
}
Unit test
0/1
Testing with array of size 15
Your output
56 34 67 54 23 87 66 92 15 32 55 54 88 92 30
Test feedback
List elements: The position of the last occurrence of the largest element in list is: 7 The largest element in list is: 92 Test Failed...
Unit test
0/1
Testing with array of size 25
Your output
56 34 67 54 23 87 66 92 15 32 55 54 88 92 30 85 102 8 102 105 24 105 18 93 98
Test feedback
List elements: The position of the last occurrence of the largest element in list is: 19 The largest element in list is: 105 Test Failed...
Explanation / Answer
#include <iostream>
#include <limits.h>
using namespace std;
void printArray(const int x[], int sizeX);
int lastLargestIndex(const int x[], int sizeX);
int main() {
int x[] = {6,3,6,4,6,4,6,4,7,4,7,8,4,7,4,2,2,6,8,5,7};
int n = sizeof(x)/sizeof(int);
printArray(x, n);
cout << "Last largest index: " << lastLargestIndex(x,n) << endl;
return 0;
}
void printArray(const int x[], int sizeX){
for(int i=0;i<sizeX;i++){
cout << x[i] << " ";
}
cout << endl;
}
int lastLargestIndex(const int x[], int sizeX){
int index = -1;
int max = -1*INT_MAX;
for(int i=0;i<sizeX;i++){
if(max <= x[i]){
max = x[i];
index = i;
}
}
return index;
}
Unit test
0/1
Testing with array of size 15
Your output
Unit test
0/1
Testing with array of size 25
Your output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.