+ 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
3 Answers
+ 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
+ 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;
}
+ 2
Thanks)