0

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;
}
2
  • 1
    Could you please indent this? Commented Sep 10, 2016 at 11:07
  • 1
    for(int i = 0; i < 3; ++i) scanf("%d", &arr[i]); Commented Sep 10, 2016 at 11:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.