The array is defined as the collection of similar type of data items stored at contiguous memory locations in single variable
Which stores the primitive type of data such as int, char, double, etc..
One-dimensional array
Two-dimensional array
Multi-dimensional array
One-dimensional array stores the many values of data in a single variable
// declaring with fixed size data_type variable_name[size_of element]={values} or // declaring without fixed size data_type variable_name[]={values} int prices[]={23,90,52}
Note :-
we cannot store extar values after declaring fixed size of array
#include <stdio.h> main() { // Create a fixed-size array of size 5 int fixedArray[5] = {2, 3, 2, 1, 5}; // Print the elements of the array printf("Elements of the fixed-size array: %d %d %d %d %d\n", fixedArray[0], fixedArray[1], fixedArray[2], fixedArray[3], fixedArray[4]); }
Accessing specific element using the index number.
Its start with zero. like [0] means accessing first element in the array, [1] accesing second element
#include <stdio.h> int main() { // Create a fixed-size array of size 5 int price[5] = {23,21,2, 9, 5}; // Print the elements of the array // accesing first element printf("First element in the array : %d\n",price[0]); // accesing second element printf("Second element in the array :%d",price[2]); return 0; }
First element in the array : 23 Second element in the array :21
#include <stdio.h> int main() { // Create a fixed-size array of size 5 int price[5] = {23,21,2, 9, 5}; price[0]=15; // Print the elements of the array // Here first element 23 change into 15 printf("After change the element : %d\n",price[0]); return 0; }
The 2-D array know as matrices, becouas its represented collection of rows and colums.
Here first dimensional represented as rows, second one colums.
#include <stdio.h> int main() { // Declare and initialize a 2D array int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Access and print elements of the 2D array for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } return 0; }
1 2 3 4 5 6 7 8 9
Here we can acceses specific data in specific row & columns
#include <stdio.h> int main() { // Declare and initialize a 2D array int prices[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; //Here we accesing the 2 row 1 column data int element = prices[1][0]; printf("Element at (2nd row, 1st column) is : %d\n",element); }
Try to change / modifie the data in 2-d array.
Padma
@2022, Padma Education.
We cannot warrant full correctness of all content, we try to give our best. While using Padma education website, you agree to have read and accepted our terms and Conditions , privacy policy.