I have an array generated with random integers from 0 to 9, I have a function that does that. I also have a function that determines what the largest element from the array is. I need to write another function that determine the index of the largest variable. I believe the problem is that I cannot call the function within the function but it could be something else.
The code I have is below:
#include <iostream>
#include <ctime>
#include <time.h>
using namespace std;
void initialize(int arr[], int size);
void print(int arr[], int size);
void findLargest(int arr[], int size);
void largestIndex(int arr[], int size);
int main(){
const int SIZE = 10;
int myList[SIZE];
initialize(myList, SIZE);
print(myList, SIZE);
findLargest(myList, SIZE);
largestIndex(myList, SIZE);
return 0;
}
void initialize(int arr[], int size){
srand(time(0));
for(int i = 0; i < size; i++){
arr[i] = (rand() % 10);
}
}
void print(int arr[], int size){
for(int j = 0; j < size; j++){
cout<<arr[j]<< endl;
}
}
void findLargest(int arr[], int size){
for(int i = 0; i < size; i++){
if(arr[0] < arr[i])
arr[0] = arr[i];
}
cout << "The largest element in the array is " << arr[0]<< endl;;
}
void largestIndex(int arr[], int size){
for(int i = 0; i < size; i++){
if(arr[i] == 9){
cout<< "The index of the largest element is " + i <<endl;
}
}
}
ctimeandtime.h.findLargestis "wrong", as it mutates the array.