What is the correct way to initialize a 1D array named grades with values 850, 900, 780, 920, and 880 in C?
Question
What is the correct way to initialize a 1D array named grades
with values 850
, 900
, 780
, 920
, and 880
in C?
Solution
To initialize a 1D array named grades with values 850, 900, 780, 920, and 880 in C, you can follow these steps:
-
Declare an integer array named grades with a size of 5. This can be done by writing:
int grades[5];
-
Assign the desired values to each element of the array. You can do this by using the assignment operator (=) and specifying the values in curly braces {}:
grades[0] = 850; grades[1] = 900; grades[2] = 780; grades[3] = 920; grades[4] = 880;
Alternatively, you can initialize the array with the values directly in the declaration statement:
int grades[] = {850, 900, 780, 920, 880};
After following these steps, the array named grades will be initialized with the values 850, 900, 780, 920, and 880.
Similar Questions
What is the correct way to declare and initialize a one-dimensional array in C?
Which of the given options correctly initializes a 1D array with the first three natural numbers?
Which of the following is the correct way to initialize all the elements of array with 0?
Explain different ways in which you can declare and initialize a single dimensionalarray.
How do you initialize an array in C?a.int arr[3] = (1,2,3);b.int arr(3) = {1,2,3};c.int arr[3] = {1,2,3};d.int arr(3) = (1,2,3);
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.