Programming in C, creating an Array user gets to input

Click For Summary
SUMMARY

The discussion centers on creating a C program that allows users to input values into an array. The user initially struggled with using the wrong format specifier in the scanf function, attempting to read integers with %s instead of %d. The correct approach involves declaring an uninitialized array and using a loop to populate it with user input, as demonstrated in the provided code snippet. This method ensures that each input value is stored in the appropriate index of the array.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with arrays in C
  • Knowledge of input/output functions in C, specifically scanf and printf
  • Basic control structures in C, such as loops
NEXT STEPS
  • Learn about dynamic memory allocation in C using malloc and free
  • Explore advanced array manipulation techniques, such as sorting and searching algorithms
  • Study error handling in C, particularly with user input
  • Investigate the use of functions to modularize code for array operations
USEFUL FOR

Students in engineering programming courses, novice C programmers, and anyone looking to improve their skills in handling user input and arrays in C.

Vagabond7
Messages
50
Reaction score
11

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.
 
Physics news on Phys.org
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.
Vagabond7 said:

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.
Vagabond7 said:
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.
 

Similar threads

Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
7
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 21 ·
Replies
21
Views
4K