Clarification on previous answere of different User (Write a program that lets t
ID: 3553335 • Letter: C
Question
Clarification on previous answere of different User (Write a program that lets the user enter 10 values into an array. The program should then display the largest and smallest values stored in the array) What if the 10 is a max number of integers not always 10 entered.
Where does the for(int i=0; i<10; i++) come from. you Declared nums[10], int max; and int mean. What does i++ do, I assume (int i=0; i<10; Why 0 instead of 1 is this not support to be the position of the value entered. How would you calcaulate the median.
THIS WAS THE ANSWER PROVIDED
#include<iostream>
using namespace std;
int main()
{
int nums[10];
int max;
int min;
cout<<"please enter 10 numbers"<<endl;
for(int i=0; i<10; i++)
cin>>nums[i];
max=nums[0];
for(int i=1; i<10; i++)
if(nums[i] > max)
max = nums[i];
min=nums[0];
for(int i=1; i<10; i++)
if(nums[i] < min)
min = nums[i];
cout<<"max = " <<max<<endl;
cout<<"min = "<<min<<endl;
}
Explanation / Answer
Where does the for(int i=0; i<10; i++) come from.
for- is for loop where i is a temporary variable to store the counting from 0 to 10
you Declared nums[10], int max; and int mean.
nums[10] is array for storing 10 numbers
max is variable for storing the maximum number.
mean is variable for storing the mean of 10 numbers.
What does i++ do, I assume (int i=0; i<10; Why 0 instead of 1 is this not support to be the position of the value entered.
i++ increases the counter by 1. It starts from 0 then 1,2,3....
It has been started from 0 because "less than" is used for condition.
It can be started from i = 1, then "less than equal" is used for condition. i<=10
How would you calcaulate the median.
for median is the middle no. of an array.
For odd number of list it is (n+1)/2.
For even number of list it is (n/2) +1 and (n/2)