简体   繁体   English

程序怎么不告诉我数字是升序还是降序?

[英]how come the program doesn't tell me if the numbers are in ascending or descending order?

all the rest of the code works, but coming to the last point, that of the order of numbers, the code either tells me that it is in increasing form or it is neither.所有其余的代码都可以工作,但到了最后一点,即数字顺序,代码要么告诉我它是递增形式,要么两者都不是。 Could you help me understand why please?你能帮我理解为什么吗?

#include <stdlib.h>
#include <stdio.h>

int main(){
int n;
int number;
int positive, negatives, null;
int even, odd;
int cont;
int rest;
int previous_number;
int increasing;
int decreasing;

increasing=1;
decreasing=1;
positive=0;
negative=0;
null=0;
even=0;
odd=0;

printf("insert a number\n");
scanf("%d",&n);

for(cont=1;cont<=n;cont++){
printf("insert a number\n");
scanf("%d",&number);

here the code works by checking if the number is positive, negative or null这里的代码通过检查数字是正数、负数还是空来工作

if(number>0){
  positive++;
}else if(number<0){
  negatives++;
}else{
  null++;
}

here also seeing if it is even or odd rest=number%2;这里也看看它是偶数还是奇数 rest=number%2;

if(rest==0){
  even=even+1;
}else{
  odd=odd+1;
}

and here the code doesn't work在这里代码不起作用

if(number>previous_number){
  decreasing=0;
}else{
    if(number<previous_number){
  increasing=0;
}
  else{
    increasing=0;
    decreasing=0;
 }
 previous_number=number;
}

  

printf("the positive numbers are: %d \n",positive);
printf("the negative numbers are: %d \n",negative);
printf("the null numbers are: %d \n",null);
printf("the even numbers are: %d \n",even);
printf("the odd numbers are: %d \n",odd);
if (increasing==1){
  printf("the numbers are arranged in ascending order\n");
}else if(decrescente==1){
  printf("the numbers are arranged in descending order\n");
}else{
  printf("the numbers are arranged neither in increasing nor in decreasing order\n");
}
system("pause");
return 0;
}

You have a variable called "negative" and one called "negatives".你有一个叫做“负”的变量和一个叫做“负数”的变量。 I think this is the source of the issue.我认为这是问题的根源。 Also one called decreasing and another called decrescente which I think are supposed to be the same.还有一个叫做decrecente,另一个叫做decrecente,我认为应该是一样的。

As a tip: a lot of the variables which are ints would be better as booleans.提示:很多整数变量作为布尔值会更好。 You are using the ++ operator to set a int to 1 by which you mean true which is unintuitive.您正在使用 ++ 运算符将 int 设置为 1,您的意思是不直观的 true。

You don't have a value for previous_number in the first iteration.您在第一次迭代中没有previous_number的值。 I suggest you to change this part:我建议你改变这部分:

 for(cont=1;cont<=n;cont++){
     printf("insert a number\n");
     scanf("%d",&number);
     ...
     previous_number=number;
 }

to this:对此:

 scanf("%d",&number); //read a first time before the loop
 for(cont=2;cont<=n;cont++){ //start the loop one number further so it will read one less
     previous_number=number; //start by saving the old number then reading the new one after.
     
     //do the rest as you did before.
     printf("insert a number\n");
     scanf("%d",&number);
     ...
     
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM