how to write a program in C++ which receives 10 numbers from input and checks whether these numbers are in ascending order or not? [closed]
I am a newbie. And I wa开发者_如何学编程nt to do this program without using array and only with 3 variables. Looking forward for your help. I stuck up here:
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
float a,b,c;
cout << "Please Enter the numbers: " << endl;
for(float k=0; k<=9; k++)
{
cout << "Enter number " << k+1 << " : ";
cin >> c
}
}
I am not getting the part how to take the values in a & b and compare them with each other. Please let me know if I am heading towards wrong direction. looking forward for the help.
Thanks in advance.
Regard, Sam
You only need two variables, not three.
#include <iostream>
#include <limits>
using namespace std;
int main(int argc, char* argv[]){
float old = std::numeric_limits<float>::min(), current = 0.f;
for(int i = 0; i < 10; i++){
std::cin >> current;
if(current > old){
old = current;
}else{
std::cout << "Not ascending order!" << std::endl;
return 0;
}
}
std::cout << "Ascending order!" << std::endl;
return 0;
}
精彩评论