简体   繁体   English

C程序,用户输入到单个数组中

[英]C Program, User input into a single array

I'm trying to use three integers that are inputted by a user in a single line into an array using CodeBlocks. 我正在尝试使用用户在一行中使用CodeBlocks在数组中输入的三个整数。 But i don't really know how to go about this. 但是我真的不知道该怎么做。 Can anyone help me? 谁能帮我?

Thanks! 谢谢!

main()
{

    int arr[3];
    int onenum;
    int twonum;
    int threenum;
    printf("Enter an Input: ");
    scanf("%d %d %d",&onenum,&twonum,&threenum);
    printf("Your input is: %d %d %d \n",onenum,twonum,threenum); 
    int arr [onenum, twonum, threenum];

    return 0;
}

Use this 用这个

int i;
int arr[3];
printf("Enter numbers");
for(i=0;i<3;i++){
    scanf("%d",&arr[i]);
}

This will store the 3 numbers entered by user in array arr. 这会将用户输入的3个数字存储在数组arr中。

Like this: 像这样:

void main() 
{

int arr[3];
int i;

printf("Enter an Input: ");

for(i = 0; i < 3; i++) 
{
    scanf("%d", &arr[i]);
}


printf("Your input is: ");
for(i = 0; i < 3; i++) 
{
    printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Let's use a loop from which runs from i=0 to i=2 because you have to add 3 numbers in array. 让我们使用一个从i = 0到i = 2的循环,因为您必须在数组中添加3个数字。

void main()
{

    int arry[3],i=0;
    printf("enter numbers");
    for(i=0;i<3;i++)
    {
        scanf("%d",&arry[i]); 
    }
    for(i=0;i<3;i++)
    {
        printf("%d \n",arry[i]); 
    }

}

Array is a collection of data. 数组是数据的集合。 For beginning you can think a array with different index as a different variable of same data type means arry[0] and arry[1] in this example as different variables of same data type integer.It will help you for beginning with array but keep in mind that arry is the only one variable the index inside capital bracket tells the variable where to look. 首先,您可以将具有不同索引的数组视为相同数据类型的不同变量,在此示例中将arry [0]和arry [1]视为相同数据类型整数的不同变量。它将帮助您以array开头,但保持请记住arry是唯一一个变量,大括号内的索引告诉该变量在哪里寻找。

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

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