0

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

    }
}
4
  • You can call functions within functions. That's not the problem. You shouldn't include ctime and time.h. Commented Mar 24, 2021 at 8:33
  • What is the question? :/ Commented Mar 24, 2021 at 8:34
  • 1
    The root of the problem is that your functions print their findings to the terminal instead of returning the relevant values to the caller. Commented Mar 24, 2021 at 8:34
  • findLargest is "wrong", as it mutates the array. Commented Mar 24, 2021 at 10:47

1 Answer 1

1

Ran it through compiler explorer

https://godbolt.org/z/c9ETjaq3z

Revealing this error message

<source>:69:59: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int]
            cout<< "The index of the largest element is " + i <<endl;
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
<source>:69:59: note: use array indexing to silence this warning
            cout<< "The index of the largest element is " + i <<endl;
                                                          ^
                   &                                      [  ]

This line

 cout << "The index of the largest element is " + i <<endl;

Adds i to the pointer to the character string "The index of the largest element is ". The result is that you carve of the beginning of the string instead of showing the value of i.

What you want is

 cout << "The index of the largest element is " << i <<endl;

or

 cout << "The index of the largest element is " + std::to_string(i) <<endl;

You might want to consider using std::max_element

auto largest = *std::max_element(arr, arr + size);
std::cout << "Largest... = " << largest << std::endl;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.