# Programming in C, creating an Array user gets to input

## Homework Statement

Alright, so for my engineering programming class I have an assignment where I have to create a program with an array, and then do things with the array, such as create a search function for it, and list how many numbers fall within a certain range, ect, ect.

So I actually just need some help getting started on this particular assignment. I need to create an Array where the user inputs 20 numbers from the keyboard. Unfortunately all the examples we are given, the arrays have values already assigned to them. I have no idea how to let a user create the data for the area to be displayed.

## Homework Equations

For example

int main()
{
/* use initializer list to initialize array n */
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
int i; /* counter */

printf( "%s%13s\n", "Element", "Value" );

/* output contents of array in tabular format */
for ( i = 0; i < 10; i++ ) {
printf( "%7d%13d\n", i, n[ i ] );
} /* end for */
getch();
return 0; /* indicates successful termination */

All the values are given, and I know how to do that just fine. I have no idea how to let a user input the values for the array though.

## The Attempt at a Solution

I tried some different things with scanf("%s", n); where "n" is the name of the array, but all the results always ended up wonky, giving me wild values once displayed that were definitely not anything I input.

How do I make an array where the values are put in by the user? The rest of the assignment I think I have a handle on, but every example involving arrays from our book, the values in the array are decided in the program itself. Thanks in advance.

Related Engineering and Comp Sci Homework Help News on Phys.org
Mark44
Mentor
When you post code, it's considered good manners to put [ code ] and [ /code ] tags around your code (omit the spaces, though). This preserves your indentation, making your code easier to read.

I have done this for your code.

## Homework Statement

Alright, so for my engineering programming class I have an assignment where I have to create a program with an array, and then do things with the array, such as create a search function for it, and list how many numbers fall within a certain range, ect, ect.

So I actually just need some help getting started on this particular assignment. I need to create an Array where the user inputs 20 numbers from the keyboard. Unfortunately all the examples we are given, the arrays have values already assigned to them. I have no idea how to let a user create the data for the area to be displayed.

## Homework Equations

For example
Code:
int main()
{
/* use initializer list to initialize array n */
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
int i; /* counter */

printf( "%s%13s\n", "Element", "Value" );

/* output contents of array in tabular format */
for ( i = 0; i < 10; i++ ) {
printf( "%7d%13d\n", i, n[ i ] );
} /* end for */
getch();
return 0; /* indicates successful termination */
All the values are given, and I know how to do that just fine. I have no idea how to let a user input the values for the array though.

## The Attempt at a Solution

I tried some different things with scanf("%s", n); where "n" is the name of the array, but all the results always ended up wonky, giving me wild values once displayed that were definitely not anything I input.
You are not using the right conversion specifier - the "%s" part. That's the specifier you use to read in a string. Your array is of type int, so the conversion specifier should be "%d".

Also, and this is very important, the input argument to scanf has to an expression that represents an address. In fact, n does represent an address, but to input values to an array you're going to have to have a loop that iterates through the array. Having a 2nd argument of n will mean that all of the values you type in go to the same location in memory - not good.
How do I make an array where the values are put in by the user? The rest of the assignment I think I have a handle on, but every example involving arrays from our book, the values in the array are decided in the program itself. Thanks in advance.

Declare the array, but don't include an initializer list. The array values will get set in a loop, something like this.

Code:
int list[5];  // Uninitialized array
int i;

for (i = 0; i < 5; i++)
{
printf("Enter a value: ");
scanf("%d", &list[i]);
...
}

Thank you very much kind sir. Sorry for the breach of etiquette. I fairly new here, and I haven't used the homework forum much.

Your solution worked like a charm though, and now I can get all of the other parts taken care of.