Introduction Data type Variable-Constant Keyword-Identifier Operations Input&Output If-else Switch loops String Array Functions Recursion-Function Mat-Function Pointers File handling Read & write Error handling Read & write error handling


Array

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..

Types of array

One-dimensional array

Two-dimensional array

Multi-dimensional array

One-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

Example
  #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]);
                        
  }              
                        

OutPut
Elements of the fixed-size array: 2 3 2 1 5

Accessing data

Accessing specific element using the index number.

Its start with zero. like [0] means accessing first element in the array, [1] accesing second element


printf("%d", array_variable_name[index_number]);
Example
 #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;
        }
                        
                        

OutPut
  First element in the array : 23 
  Second element in the array :21

Modifing / changing the array inside values.
Example
 #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;
                 }

OutPut
After change the element : 15

Two-dimensional array

The 2-D array know as matrices, becouas its represented collection of rows and colums.

Here first dimensional represented as rows, second one colums.


int prices[][]={{21,23,9,15},{11,12,13,14}}
Example
 #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;
      }
        
OutPut
  1 2 3   
  4 5 6   
  7 8 9   
            
Accessing data

Here we can acceses specific data in specific row & columns


Example
 #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);
    }
        

OutPut
Element at (2nd row, 1st column) is : 2

Try to change / modifie the data in 2-d array.


Open kannada !





@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.