Programming in C, creating an Array user gets to input

In summary, you need to create an array that will be filled by the user, and you need to use a loop to input the values.
  • #1
Vagabond7
50
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
  • #2
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]);
    ...
}
 
  • #3
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.
 

1. How do I create an array in C?

To create an array in C, you first need to declare the array by specifying the data type of the elements and the size of the array. For example, to create an array of 10 integers, you would use the syntax: int myArray[10];

2. How do I allow the user to input values into the array?

To allow the user to input values into the array, you can use a for loop to iterate through the array and use the scanf() function to store the user's input into each element of the array. It is important to check for errors and validate the user's input before storing it in the array.

3. How do I access and modify specific elements in the array?

To access and modify elements in the array, you can use the index notation. For example, to access the third element in the array, you would use myArray[2] since arrays in C are zero-indexed. To modify the element, you can simply assign a new value to it using the assignment operator (=).

4. Can I create an array of a user-defined data type?

Yes, you can create an array of a user-defined data type in C. This can be done by declaring the array using the struct keyword, followed by the name of the data type and the size of the array. For example, to create an array of 5 students, you would use the syntax: struct student myArray[5];

5. How do I free the memory allocated for the array?

In C, you can use the free() function to free the memory allocated for the array. This is especially important if you dynamically allocated memory for the array using functions like malloc(). Failing to free the memory can lead to memory leaks and cause performance issues in your program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top