Hello. I am using C and I want to create a row vector and display it. I mean the user will enter each number for the row vector and the program will show it. I am new to C and I am not sure how to do it. Here is my code:
I am getting some other numbers that I don't enter. Also, can anyone please explain what are these numbers and why they appear? Thank you
# include <stdio.h>
main()
{
int a [5];
printf("Please enter the row vector\n");
scanf("%d%d%d%d%d", &a,&a,&a,&a,&a);
int a[5];
printf("Please enter the row vector\n");
for (int i = 0; i < 5; i++)
{
scanf("%d", &a[i]); // use a for-loop to input 5 numbers
}
for (int i = 0; i < 5; i++)
{
printf("Your vector is %d\n", a[i]); // use a for-loop to output the row
}
I typed the code below. I am getting segmentation fault as soon as I insert the first input and press enter. The first input means that it goes to a[0], right? Why am I getting this if this array already defined in the for loop? I am using c, not c++ if it makes any difference.
main ()
{
int a[5];
int i;
printf("Please enter the row vector\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &a[i]);
}
for (i = 0; i < 5; i++)
{
printf("Your vector is %d\n", a[i]);
}
return 0;
I think the problem was lack of & sign before a[i] in the last printf.
No. The printf statement is correct. The format descriptor is %d, so printf is expecting an int argument.
@op - main must have type int.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.