# How to find the largest number in an array c++

1 vote
12 views

## Problem:

I am trying to finding the largest number in an array c++

Every time I am trying my code is printing all the values of the array instead of the largest one. I might be doing something wrong inside my iteration. Do you have any idea to do it more easily and efficiently?

Thanks.

1 vote

## Solution:

You may use a function called max_element() but there is no way to skip the iteration. Without an iteration, you can’t check all the values of an array. So, iteration is a must to do such things. Every time your program prints the values of the given array because you set the wrong condition inside the iteration.

``````using namespace std;

int largest_number(int my_arr[], int i)
{
return *max_element(my_arr, arr+i);
}

int main()
{
int my_arr[] = {10, 32, 45, 90, 88};
int i = sizeof(my_arr)/sizeof(my_arr);
cout << largest_number(my_arr, i);
return 0;
} ``````

Keep exploring. Thanks.

## Related questions

1 vote
12 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
21 views
Is there a way to find how many values an array has? Detecting whether or not I have reached the end of an array would also work.
1 vote