In this tutorial, we are going to explain the types of arrays in c programming. In c programming, arrays are classified into two types but if we use both arrays in programming then we can say it is a multi-dimensional array. Types of arrays in programming are as follows.

  1. one-dimensional array/Single Dimensional Array
  2. Two-dimensional array/Multi-Dimensional Array

Types of Array in c programming

One dimensional array

The one-dimensional array also called single dimensional array is the type of array. Accessing its elements involves the single subscript which can either represent a row or column index.
As an example consider the C declaration int marks10];
In the above-given example, the array is contained with the  10 elements of any value available to the int data type. In the  C, the array element scale is from 0 to 9 indexes because the array always starts from 0 indexes.

Rules For Declaring One Dimensional Array

  • An array variable must be declared before being used in a program.
  • The declaration must have a data type(int, float, char, double, etc.), variable name, and subscript.
  • The subscript represents the size of the array. If the size is declared as 10, programmers can store 10 elements.
  • An array index always starts from 0. For example, if an array variable is declared as s[10], then it ranges from 0 to 9.
  • Each array element is stored in a separate memory location.

One-dimensional Array Declaration in C

You can use the following syntax to declare a 1-D array in the c programming language; as shown below:

Syntax:
datatype arrayName [ size ] ;

Example
int students [10] ;

The above declaration of 1-D array reserves 10 continuous memory locations of 2 bytes each with the name students and tells the compiler to allow only integer values into those memory locations.

Initialization of One-Dimensional Array

Following general syntax is used for declaring and initializing a one-dimensional array.

datatype arrayName [ size ] = {value1, value2, value3 ...} ;

Example Code for Initialization

int students [6] = { 89, 90, 76, 78, 98, 86 } ;

Accessing Elements of Single-Dimensional Array

In a c programming language, to access the elements of a one-dimensional array we use an array name. The following syntax is used to access the value from the array.

arrayName [indexValue]

Example Code

RollNO [2] = 99 ;
In the above example, the third element of the 'student' array is assigned the value '99'.

Program1 – One Dimensional Array in C

#include<stdio.h> 
int main() 
{ 
    int n;                                                                    //variable declation
    int student[5] = {10,20,30,40,50};                        //declaring and Initializing array in C 
       
    student[0] = 10;                                                        /*  initialized array
    student[1] = 20;
    student[2] = 30; 
    student[3] = 40;
    student[4] = 50; */
    for (n=0;n<5;n++) 
    { 
        // Accessing each array through for loop
        printf("marks of student[%d] is %d \n", i, student[i]); 
    } 
}

Output of the above program

Value of arr[0] is 10
Value of arr[1] is 20
Value of arr[2] is 30
Value of arr[3] is 40
Value of arr[4] is 50

Program 2 To Access Access Large and 2nd Large No from the array 

#include <stdio.h>
#include <stdlib.h>
int main()
{
 
int i,A[50],size;
int large1=0, Large2=0;
printf("Plz decide the size of array: ");
scanf("%d",&size);
for(i=0; i<size; i++){
     
    printf("\n Enter 1st no ");
    printf("%d :",(i+1));
     
    scanf("%d",&A[i]);
 
    if(large1<A[i]){
      Large2=large;
      large=A[i];
    }
    else if(Large2<A[i]){
    Large2=A[i];
    }
}
 
printf("\n\n");
printf("Large No is %d",large);
printf("\n The second largest number is %d",Large2);
 
return 0;
}

Two-dimensional array in C programming

When an array is created with more than one dimension is called a multi-dimensional array. A two-dimensional array is also called a multi-dimensional array. The two-dimensional array in C programming is also known as a matrix or table which contains rows and columns. A matrix can be represented as a table of rows and columns. Before we discuss two Dimensional arrays. The example of the two-dimensional array is as follows.
include<stdio.h>
include<conio.h>
void main( )
{
int marks[4][5];
int i,j;
for (i=o;i<5;i++) {
for (j=0;j<5;j++) {
printf (” Enter the value for marks [%d][%d] :”,i,j);
scanf (“%d”, &marks[i][j]);
}
}

Advertisements
Advertisements
Advertisements