简体   繁体   English

如何将用户输入的整数存储在数组中

[英]How to store user input of round numbers in an Array

I have a problem to solve which is.我有一个问题要解决。 I have to make a program which will take 2 numbers from the sequence of numbers and compare them if it's '<' '>' or '=' and the list of numbers needs to end with number 0. So basically I have a sequence of numbers {5, 7, 8, 4, 3, 3, 0} and the program have to check by couples 5,7 (it is 5 < 7) then it will go to 8, 4 (it is 8 > 4) then 3, 3 so it is 3 = 3. And the 0 is basically as the exit.我必须制作一个程序,它将从数字序列中取出 2 个数字并比较它们是否为“<”“>”或“=”并且数字列表需要以数字 0 结尾。所以基本上我有一个序列数字 {5, 7, 8, 4, 3, 3, 0} 并且程序必须检查 5,7 对(它是 5 < 7)然后它将 go 到 8, 4(它是 8 > 4)然后3, 3 所以它是 3 = 3。而 0 基本上就是出口。

So far I wrote down the comparison of the numbers but now I only have a program which takes 2 inputs from the user and compares them.到目前为止,我写下了数字的比较,但现在我只有一个程序,它从用户那里获取 2 个输入并进行比较。 But I kinda need to specify for the user let's say to enter 11 numbers which will end with 0 (as 0 will not be counted to the comparison) and store those numbers in an array and then let the program to compare the 2 numbers after each other (in a sequence) with <, > or =.但是我有点需要为用户指定让我们说输入 11 个以 0 结尾的数字(因为 0 不会被计入比较)并将这些数字存储在一个数组中然后让程序在每个之后比较 2 个数字其他(按顺序)用 <、> 或 =。

Thanks in advance guys.提前谢谢大家。 I'm kinda new to C and these arrays and specially malloc, calloc, realloc are really complicated for me.我对 C 有点陌生,这些 arrays 和特别是 malloc、calloc、realloc 对我来说真的很复杂。

So far my code looks like this:到目前为止,我的代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define post 10
int main(){

    int a, b;


    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);

    if (a > b)
    {
        printf("%d > %d\n", a, b);
    }
    else if(a < b)
    {
        printf("%d < %d\n", a, b);
    }
    else
    {
        printf("%d = %d\n", a, b);
    }


    system("pause");
    return 0;
}

You can create large array and then input how many number you want to compare.您可以创建大数组,然后输入要比较的数字的数量。 Then you can use for to walk through the numbers in the array and compare them.然后您可以使用for遍历数组中的数字并比较它们。

Here is exemple:这是例子:

#include <stdio.h>

#define SIZE 100

int main()
{
    int arr[SIZE] = {0};

    printf("Enter number: ");
    
    int number;
    int counter = 0;   
    
    while (1)
    {
        if (scanf("%d", &number) != 1)
        {
            printf("Invalid number!");
            return 1;
        }
        arr[counter] = number;
        if(number == 0) {break;}
        counter++;
    }

    for (int i = 0; i < counter; i+=2)
    {
        if (arr[i] > arr[i + 1])
        {
            printf("%d > %d\n", arr[i], arr[i + 1]);
        }
        else if(arr[i] < arr[i + 1])
        {
            printf("%d < %d\n", arr[i], arr[i + 1]);
        }
        else
        {
            printf("%d = %d\n", arr[i], arr[i + 1]);
        }      
    }
    
    return 0;
}

Output: Output:

Enter number: 1 2 3 4 5 5 0
1 < 2
3 < 4
5 = 5

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

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