+ 1

Find minimum

https://code.sololearn.com/cB4C7ZA46hHy/?ref=app Given a sequence of numbers.Find minimum element from the given sequence. Input data. First line - n number,total amount of data in array.(1<=N=<1000) Then all aray is inputed,all numbers are between -1000 and 1000. Output data. The minimum number from the sequence. Sample input: 7 1 4 2 5 2 5 3 Sample output: 1

1st Oct 2021, 7:26 AM
Le.7
3 Réponses
+ 3
You did a few things wrong here. First of all, you need to receive input from user with array size before you fill your array. And therefore you should only loop in range 0-(size-1) In addition you should start the loop with i=0 , as arrays start from 0 rather than 1. And lastly, you shouldn't print min every time you find a new one. You should just print it once at the end. Here is a fixed version of your code: https://code.sololearn.com/cX7En98vdniU/?ref=app
1st Oct 2021, 8:05 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
#include <iostream> using namespace std; int main() { int arr[1000] = {}; for(int i = 0; i < 1000; i++){ cin >> arr[i]; } int min = arr[0]; for (int n : arr){ if(min > n && 1 <= n && n <= 1000){ min = n; } } cout << min << endl; return 0; }
1st Oct 2021, 8:45 AM
Asanda Ndimande
Asanda Ndimande - avatar
+ 2
Thanks)
1st Oct 2021, 10:10 AM
Le.7