Bob
Bob

Reputation: 63

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

Upvotes: 0

Views: 53

Answers (3)

Sujan Poudel
Sujan Poudel

Reputation: 853

Let's use a loop from which runs from i=0 to i=2 because you have to add 3 numbers in array.

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.

Upvotes: 0

user6816543
user6816543

Reputation: 11

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.

Upvotes: 1

Gerhard Stein
Gerhard Stein

Reputation: 1563

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

Upvotes: 0

Related Questions