Made with ScholarlyMake flashcards like these from your own notes
Question:Why do we need arrays?
Answer:Arrays are used to store a collection of elements of the same data type. It enables easy manipulation and management of data without having to use individual variables.
Question:What is an array?
Answer:An array is a data structure that stores a fixed-size sequential collection of elements of the same type. It allows the manipulation and management of a collection of elements as a single entity.
Question:How do we declare an array?
Answer:To declare an array, we need to specify the data type of its elements, a variable name and its size. For 1-dimensional arrays, we use int A[5]; and for 2-dimensional arrays, we use int A[3][5];.
Question:How do we initialize an array in C?
Answer:There are two ways to initialize an array in C: (1) by specifying array size during declaration and giving values to each element, like int A[5] = {1,2,3,4,5}; and (2) by omitting the array size and simply listing the values to be stored, like int A[] = {1,2,3,4,5};.
Question:How do we access elements in an array with a loop?
Answer:To access elements in a 1-dimensional array with a loop, we can use a for loop and specify the index of each element using the array name and the loop variable, like printf("Element %d in the array is %d \n", i, A[i]);. To access elements in a 2-dimensional array, we use a nested loop, like for(int i=0;i<2;i++){for(int j=0;j<3;j++){printf("\n A[%d][%d]:%d", i, j, A[i][j]);}}.