forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_array_user_input.c
More file actions
40 lines (33 loc) · 1.04 KB
/
Copy pathinit_array_user_input.c
File metadata and controls
40 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*******************************************************************************
*
* Program: Initialize An Array With User Input
*
* Description: Example of how to initialize an array with user input in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=5nyMb7hJ7Xs
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
int main()
{
int length;
// ask the user for the length of the array, store it into length
printf("Length: ");
scanf("%d", &length);
// declare an array using the entered length
int array[length];
// ask the user to enter a value for array indexes from 0 ... (length-1), and
// store what is entered into the array at each index
for (int i = 0; i < length; i++)
{
printf("array[%d]=", i);
scanf("%d", &array[i]);
}
// output the array contents to verify elements were set correctly
printf("\n");
for (int i = 0; i < length; i++)
printf("array[%d] = %d\n", i, array[i]);
return 0;
}