In this article array programs in c we give the one dimensional array programs with their output also two dimensional array programs with their out.

Array programs in C:

// 1. One Dimensional array

#include<stdio.h>

#include<conio.h>

#define SIZE 5

void main()

{

int i;

int marks[SIZE];

// 2. compile time initialization

marks[0]=80;

marks[1]=70;

marks[2]=90;

marks[3]=88;

marks[4]=65;

clrscr();

// trivals array- show data

printf(“\n\n Elements of Array: \n”);

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

printf(“\n %d”,marks[i]);

getch();

}

Output:

Elements of Array:

80

70

90

88

65

// 3. One Dimensional array

#include<stdio.h>

#include<conio.h>

#define SIZE 5

void main()

{

int i;

int marks[SIZE]={5,3,7,9,10};  // compile time initialization

clrscr();

// trivals array- show data

printf(“\n\n Elements of Array: \n”);

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

printf(“\n marks [%d] : %d”,i,marks[i]);

getch();

}

Output:

Elements of Array:

marks [0] : 5

marks [0] : 3

marks [0] : 7

marks [0] : 9

marks [0] : 10

/* 4. Write a C Program to show different numbers by using

            One dimensional array*/

#include<stdio.h>

#include<conio.h>

#define SIZE 5

void main()

{

int i;

int data[SIZE];

clrscr();

printf(“\n\n Enter the elements of Array: “);

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

{

scanf(“%d”,&data[i]);

}

printf(“\n\n Elements of Array: “);

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

{

printf(“\t %d”,data[i]);

}

getch();

}

Output:

Enter the elements of Array: 4

2

6

9

1

Elements of Array:    4   2   6    9    1

/* 5. Write a C Program to show sum of different numbers by using

            One dimensional array*/

#include<stdio.h>

#include<conio.h>

#define SIZE 5

void main()

{

int i,sum=0;

int data[SIZE];

clrscr();

printf(“\n\n Enter the elements of Array: “);

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

{

scanf(“%d”,&data[i]);

}

printf(“\n\n Elements of Array: “);

for(i=0;i<SIZE;i++)    //  2  1   4   6  3

{

printf(“\t %d”,data[i]);

sum=sum+data[i]; // 16

}

printf(“\n\n Sum of Array elements: %d”,sum);

getch();

}

Output:

Enter the elements of Array: 2

1

4

6

3

Sum of Array elements: 16

/* 6. Write a C Program to find largest number for the existing list

            Of given array */

#include<stdio.h>

#include<conio.h>

#define SIZE 7

void main()

{

int i,max;

int data[SIZE]={43,12,56,8,2,97,1};  // declaration & initialization

clrscr();

max=data[0];

for(i=1;i<SIZE;i++)

{

if(max<data[i])

{

max=data[i];

}

}

printf(“\n\n %d is Max number for the given series…”,max);

getch();

}

Output:

97 is Max number for the given series…

/* 7. Write a C Program to search the number for the existing list

            of given array */

#include<stdio.h>

#include<conio.h>

#define SIZE 7

void main()

{

int i,n,search=0;

int data[SIZE]={43,12,56,8,2,97,1};  // declaration & initialization

clrscr();

printf(“\n\n Enter the number: “);

scanf(“%d”,&n);   //12

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

{

if(data[i]==n)

{

printf(“\n\n %d is Found. Search is Successful…”,n);

search=1;

break;

}

}

if(search==0)

{

printf(“\n\n %d is not Found. Search is Unsuccessful…”,n);

}

getch();

}

Output:

Enter the number: 2

2 is Found. Search is Successful…”

Array programs in C:

// 8. Two dimensional array – Show Matrix

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

int matrix[2][2]={4,1,7,3};  // compile time initialization

clrscr();

printf(“\n\n First Matrix: \n\n”);

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

{

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

{

printf(“\t%d”,matrix[i][j]);

}

printf(“\n\n”);

}

getch();

}

Output:

First Matrix:

4           1

7          3

// 9. two dimensional array

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

int data[3][2]={3,7,1,9,2,5}; // compile time initialization

clrscr();

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

{

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

{

printf(“Data[%d][%d]: %d”,i,j,data[i][j]);

}

}

getch();

}

Output:

Data[0][0]: 3

Data[0][1]: 7

Data[1][0]: 1

Data[1][1]: 9

Data[2][0]: 2

Data[2][1]: 5

// 10. Two dimensional array – Show Matrix

#include<stdio.h>

#include<conio.h>

#define RSIZE 3

#define CSIZE 2

void main()

{

int i,j;

int matrix[RSIZE][CSIZE];    // run time initialization

clrscr();

printf(“\n\n Enter the elements of 1st Matrix: “);

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

{

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

{

scanf(“%d”,&matrix[i][j]);

}

}

printf(“\n\n First Matrix: \n\n”);

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

{

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

{

printf(“\t%d”,matrix[i][j]);

}

printf(“\n\n”);

}

getch();

}

Output:

Enter the elements of 1st Matrix: 2

5

8

1

3

7

First Matrix:

2          5

8          1

3          7

/* 11. Write a C Program to find addition of two Matrices */

/*

1st matrix:

1          2

3          4

2nd matrix:

5          6

7          8

Sum  of above 2 matrices:

1+5=6         2+6=8

3+7=10       4+8=12

*/

 #include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

int mat1[2][2],mat2[2][2],sum_mat[2][2];

clrscr();

printf(“\n Enter the elements of first matrix: “);

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

{

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

{

scanf(“%d”,&mat1[i][j]);

}

}

printf(“\n First Matrix: \n”);

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

{

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

{

printf(“\t %d”,mat1[i][j]);

}

printf(“\n\n”);

}

printf(“\n Enter the elements of second matrix: “);

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

{

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

{

scanf(“%d”,&mat2[i][j]);

}

}

printf(“\n Second Matrix: \n”);

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

{

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

{

printf(“\t %d”,mat2[i][j]);

}

printf(“\n\n”);

}

printf(“\n Sum of Above Two Matrices: \n”);

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

{

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

{

sum_mat[i][j]=mat1[i][j]+mat2[i][j];

}

}

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

{

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

{

printf(“\t %d”,sum_mat[i][j]);

}

printf(“\n\n”);

}

getch();

}

Output:

Enter the elements of first matrix: 1

2

3

4

First Matrix:

1         2

3        4

Enter the elements of second matrix: 5

6

7

8

Second Matrix:

5          6

7          8

Sum of Above Two Matrices:

6          8

10        12

/* 12. Write a C Program to find multiplication of two Matrices */

/*

1st matrix:

1          2

3          4

2nd matrix:

5          6

7          8

Multiplication of above 2 matrices:

(1*5)+(2*7)=19      (1*6)+(2*8)=22

(3*5)+(4*7)=43              (3*6)+(4*8)=50

*/

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,k;

int mat1[2][2],mat2[2][2],mul_mat[2][2];

clrscr();

printf(“\n Enter the elements of first matrix: “);

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

{

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

{

scanf(“%d”,&mat1[i][j]);

}

}

printf(“\n First Matrix: \n”);

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

{

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

{

printf(“\t %d”,mat1[i][j]);

}

printf(“\n\n”);

}

printf(“\n Enter the elements of second matrix: “);

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

{

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

{

scanf(“%d”,&mat2[i][j]);

}

}

printf(“\n Second Matrix: \n”);

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

{

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

{

printf(“\t %d”,mat2[i][j]);

}

printf(“\n\n”);

}

printf(“\n Multiplication of Above Two Matrices: \n”);

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

{

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

{

mul_mat[i][j]=0;

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

{

mul_mat[i][j]=mul_mat[i][j]+(mat1[i][k]*mat2[k][j]);

}

}

}

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

{

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

{

printf(“\t %d”,mul_mat[i][j]);

}

printf(“\n\n”);

}

getch();

}

Output:

Enter the elements of first matrix: 1

2

3

4

First Matrix:

1           2

3          4

Enter the elements of second matrix: 5

6

7

8

Second Matrix:

5          6

7          8

Multiplication of Above Two Matrices:

19        22

43        50

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *