• Register
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?

I’d love to hear from you guys. Please shar your ideas about this.

Thanks.

12 7 7
15,250 points

1 Answer

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.

 Please follow the code snippet below:

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[0]); 
    cout << largest_number(my_arr, i); 
    return 0; 
} 

 Keep exploring. Thanks.

13 9 6
94,240 points

Related questions

1 vote
1 answer 12 views
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.
asked Mar 29 Gavin 15.3k points
0 votes
1 answer 21 views
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.
asked Aug 8 game 4.3k points
1 vote
1 answer 16 views
16 views
Problem: Supose there are 3 numbers. num1 = 114 num2 = 88 num3 = 279 I tried to find the largest number between them using python.So I wrote the code: num1 = float(input("Enter your first number: ")) num2 = float(input("Enter your second number: ")) num3 = ... num2 > num3): ^ SyntaxError: invalid syntax Can anyone help me out to solve this problem? And how can I take the numbers input from users?
asked Jun 11 samhaz 5k points
0 votes
1 answer 5 views
5 views
I am trobuleing to find the length of array in c without using sizeof.
asked Oct 3 Daniel Anderson 4k points
0 votes
1 answer 15 views
15 views
I am new at java programing, I have an assginment about how to find largest number in array and I start coding on my own and i wrote a program given bellow : int size ; int array[] = {10, 20, 25, 63, 96, 57}; size = array.length; for(int i = 0; i<size; i++ ){ ... j]){ size = array[i]; } } System.out.println("Third largest element is:: "+array[size]) } but it is not running, what should I do now?
asked Mar 16 LizzyM 6.1k points