Welcome Guest.

# c++ enter n numbers from the keyboard, the output indicates the maximum number of the first few

Asked by: Matthew 187 views IT September 9, 2018

c++ enter n numbers from the keyboard, the output indicates the maximum number of the first few

1. +7Votes  #include <iostream>
using namespace std;
int main()
{
int n,a,m,mp;
Cin >> n;
cin >> a;
m=a;
mp=0;
For(int i=1; i<n; i++)
cin>>a;
if (a>m)
m=a;
mp=i;
}
}
printf("max value=%d postion=%d\n",m,mp +1);
return 0;
}

Answer: Upstairs did not consider the case where the input is negative

Answer: In addition, If it is your OJ program, pay attention to the data range (may use floating point)
Also note the input and output formats

2. +8Votes  #include <iostream>

using namespace std;

int main()

{

int n;

while(true)

{

cout << " Please enter the number n:";

int number = -1;

int maxid = 0;

int curmax= -1;

for(int i = 1; i <= n; i++ )

{

cout << "Please enter the " < < i << "Number: ";

cin >> number;

if(number>curmax)

{

curmax = number;

&nbs p;               maxid = i;

}

}

cout << " The biggest is the number " << maxid << " " << endl;

}

return 0;

}

Answer: cout << “Please enter a number n:”; there is one less cin&gt behind this sentence;>n;

3. +4Votes  Converts the input number into a string array, determines the number of loops based on the length (number of digits) of the string, and stores the maximum and minimum numbers with two variables, each time Take a bit and compare it with the previous one to find the function used
itoa Convert the integer number to a string
atoi Convert the string to an integer number

4. +6Votes  #include<iostream> using namespace std; int main(){     int a,min_val,max_val;     cin>>a;     min_val=a;     max_val=a;     for (int i=2;i<=10;i++){         cin>>a;         if (a> Max_val)             max_val=a;         if (a<min_val)             min_val=a; &Nbsp;   }     cout<<min_val<<" "<<max_val<<endl;     return 0; }

This is the first method to manually implement the minimum and maximum functions.

12345678910111213141516171819202122232425262728293031   #include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){     vector<int> v;     int a;     for (int i=1;i<=10;i++){         cin>>a;         v.push_back(a);  ;   }     int min_val=*min_element(v.begin(),v.end());     int max_val=*max_element(v.begin(),v.end());     cout<<min_val<<" "<<max_val<<endl;     return 0; }

This is the second method, Use the vector container in C++, and the existing min_element and max_element functions.