write a c program to intialize one dimensional array and access the elements and print the output in the display
Answers
Answer:
#include<stdio.h>
int main()
{
int arr[5], i;
for(i = 0; i < 5; i++)
{
printf("Enter a[%d]: ", i);
scanf("%d", &arr[i]);
}
printf("\nPrinting elements of the array: \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
// signal to operating system program ran fine
return 0;
}
-------------------------------------
output:
Enter a[0]: 11
Enter a[1]: 22
Enter a[2]: 34
Enter a[3]: 4
Enter a[4]: 34
Printing elements of the array:
11 22 34 4 34
Explanation:
u can initialize an array in c by first declaring the data type eg: int or float or char...., followed by the array name of ur choice
eg:
int my_array[100]
here 100 is the size of the array
u can get the individual elements by accessing the array the same way u declared it
eg:
my_array[0] will give u the first element as indexing starts with 0
if u try to use an invalid index the compiler will give u a "garbage' value
hope this helps