Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help on following program using C++ language. Write a C++ function, smalles

ID: 3689196 • Letter: N

Question

Need help on following program using C++ language.

Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another 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. Write a driver to test your program.

write source code which should be commented as follows:

Analysis at the top of the file

Function analysis, preconditions, and postconditions after each function prototype

Major tasks identified as comments in main function

Comments in functions as needed

Explanation / Answer

In the example smallest is 3 and first occurance of 3 is at index 4.
In the example largest is 11 and last occurance of 11 is at index 5.

c++ code:

#include <bits/stdc++.h>
using namespace std;

int smallestIndex(int arr[],int size){
   int ind=0; //initilaize ind to 0
   int val=arr[0]; // //initilaize val to first element of array
   for(int i=1;i<size;i++){ //iterate from 1 to size-1
       if(arr[i]<val){ // if arr[i] is less than val then update ind and val.
           ind=i;
           val=arr[i];
       }
   }
   return ind;
}

int lastLargestIndex(int arr[],int size){
   int ind=0; //initilaize ind to 0
   int val=arr[0]; //initilaize val to first element of array
   for(int i=1;i<size;i++){ //iterate from 1 to size-1
       if(arr[i]>=val){ // if arr[i] is less than val then update ind and val.
           ind=i;
           val=arr[i];
       }
   }
   return ind;
}

int main(){
   int arr[10]={11, 5, 7, 5, 3, 11, 10, 8, 5, 3};
   cout<<smallestIndex(arr,10)<<endl;
   cout<<lastLargestIndex(arr,10)<<endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote