Your task is to write a program that would ask user to enter 10 integers. If the
ID: 3562358 • Letter: Y
Question
Your task is to write a program that would ask user to enter 10 integers. If the entered numbers are in ascending order, your program should output success. If the numbers are not ascending order (meaning there is a number out of place), your program should output failure.
Hint: Let's clarify this week's assignment. User is going to enter 10 numbers. Make sure that you are getting these numbers with a loop -- don't want to see 10 cin statements :-)
As soon as user enters a number, you will check if this number is smaller than the previous number he/she had entered. If the current number is smaller than the previous number, that means the numbers are being entered out of place (not in ascending order). Give failure message to user and exit the program right away. You don't need to wait for user to enter all 10 numbers.
If user ends up entering 10 numbers and all are good, then tell user that it is a success.
Explanation / Answer
#include<iostream>
int main()
{
int i,first,temp;
bool b = "true";
cout<<"Enter 10 integers"<<endl;
cin>>first;
for(i = 2; i <= 10; i++)
{
cin>>temp;
if(temp < first)
{
cout<<"Failure"<<endl;
exit(0);
}
else
{
first = temp;
}
}
cout<<"Success"<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.