Friday, June 9, 2023

C language chapter 4 Array

 

ARRAY

ARRAYS

          Array is a collection of similar data type stored in continuous Memory location.

 In single variable we can store multiple values.

Syntax

          Data-type variable-name[size];

Example

          int a[10];     //declaration

Array Initialization

Syntax

data-type  variable-name[size]={value1,……..value n};

Example

int a[5]={1,2,3,4,5};

Types of arrays

ZSingle or one-dimensional array

ZMulti-dimensional or Two-dimensional array

 

 

Single or 1-D  Array

#include<stdio.h>

#include<conio.h>

main()

{

int a[5],i;

clrscr();

for(i=0;i<5;i++)

{

scanf("%d",&a[i]);

}

for(i=0;i<5;i++)

{

printf("\n %d",a[i]);

}

getch();

}

 Output


Two-Dimensional Array

Syntax

datatype variablename[rowsize][column size]={value};

        Column=j

 

0

1

 Row=i                 0

1

2

         1

3

4

          

  int a[2][2]={{1,2},{3,4}};


Program

#include<stdio.h>

#include<conio.h>

main()

{

int i,j,a[2][2];

clrscr();

printf("Enter the values \n");

for(i=0;i<2;i++)

{

for(j=0;j<2;j++)

{

scanf("%d",&a[i][j]);

}

}

printf("\n Matrix a \n");

for(i=0;i<2;i++)

{

for(j=0;j<2;j++)

{

printf("%d\t",a[i][j]);

}

printf("\n");

}

getch();

}

Output



 


0 comments:

Post a Comment