• Register
1 vote
240 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.

15.3k points

Please log in or register to answer this question.

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.

94.3k points

Related questions

0 votes
1 answer 375 views
375 views
Problem: Hello Programmers! I am a novice C programmer striving hard to learn as much programming as possible in minimum time. I came across an assignment in which the programmer has to write a program that prints the second largest number in an integer array. I tried writing such a program and I ... +) { if(arr[i] > largest) { largest = arr[i]; } } printf("%d", largest); return 0; } Output: 44
asked Jan 2 Code Learner 10.5k points
0 votes
1 answer 696 views
696 views
Problem: Hi there! I have started learning java just recently and I found a problem in a programming exercise in which I have to find the second largest number in a given array using programming. I know how to find the largest number, but I am facing difficulty ... number. Can anybody here help me solve the problem? Also, provide the example program so that I can understand it better. Thanks!
asked Jan 2 Code Learner 10.5k points
1 vote
1 answer 39 views
39 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, 2020 Gavin 15.3k points
0 votes
1 answer 45 views
45 views
Problem I'm trying to find the largest number in the array in recusin java, but i am not getting the appropriate results So can anyone help me
asked Apr 6 Subhendu 4.5k points
0 votes
1 answer 328 views
328 views
Problem: Python program to find second largest number in a listGiven a list of numbers, the task is to write a Python program to find the second largest number in given list. Examples: Input: list1 = [10, 20, 4] Output: 10 Input: list2 = [70, 11, 20, 4, 100] Output: 70 Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same. Python3 filter_none
asked Jan 23 Mashhoodch 13k points
0 votes
1 answer 25 views
25 views
Problem: Please assist me in resolving this issue: Find missing number in array in java
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 16 views
16 views
Problem: Please assist me in resolving this issue: find second largest number in array in java
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 19 views
19 views
Problem: The operation must be exported or contain an intent-filter, which is why I got the error in Find second largest number in list python
asked Mar 29 tuhin1 48.9k points
1 vote
1 answer 28 views
28 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, 2020 samhaz 5k points
0 votes
1 answer 32 views
32 views
Problem How can i find largent number in array in c++
asked Mar 27 Subhendu 4.5k points