开发者

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]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜