0% found this document useful (0 votes)
6 views13 pages

C Programs for Array Operations

The document contains multiple C programs that demonstrate basic operations on arrays, including storing and printing elements of a 1D array, performing matrix multiplication, addition, and subtraction on 2D arrays. Each program prompts the user for input, processes the data, and displays the results. The code uses standard input/output functions and includes basic control structures for iteration.

Uploaded by

mugloosalman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

C Programs for Array Operations

The document contains multiple C programs that demonstrate basic operations on arrays, including storing and printing elements of a 1D array, performing matrix multiplication, addition, and subtraction on 2D arrays. Each program prompts the user for input, processes the data, and displays the results. The code uses standard input/output functions and includes basic control structures for iteration.

Uploaded by

mugloosalman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//program to store and print elements from 1D array

#include<stdio.h>
#include<conio.h>
void main()
{
int num_array[10],i;
//here array size is 10
//index value of array start by 0
clrscr();
//reding data for array
for(i=0;i<10;i++)
{
printf("\n enter element for an array : ");
scanf("%d",&num_array[i]);
}
//printing array
printf("\n Entered 1D array is \n");
for(i=0;i<10;i++)
{
printf("\n %d",num_array[i]);
}
getch();
}
//Program to perform matrix multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[3][3],arr2[3][3],arr3[3][3];
int i,j,k;
clrscr();
//take input of first array
for(i=0;i<3;i++) //row
{
for(j=0;j<2;j++) //column
{
printf("\n enter element for first array : ");
scanf("%d",&arr1[i][j]);
}
}
//take input of second array
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n enter element for second array : ");
scanf("%d",&arr2[i][j]);
}
}

//Process to perfrom multiplication


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=0;
for(k=0;k<3;k++)
{
arr3[i][j] = arr3[i][j] + (arr1[i][k] * arr2[k][j]);
}
}
printf("\n");
}
clrscr();
printf("\n First Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr1[i][j]);

}
printf("\n");
}
printf("\n Second Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr2[i][j]);

}
printf("\n");
}
printf("\n Multiplication of Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr3[i][j]);

}
printf("\n");
}
getch();
}

//Program to read and print 2D array (matrix)


#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[3][3];
int i,j;
clrscr();
//take input of first array
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n enter element for first array : ");
scanf("%d",&arr1[i][j]);
}
}

printf("\n Printing an Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr1[i][j]);

}
printf("\n");
}
getch();
}

//Program to perform matrix Addition


#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[3][3],arr2[3][3],arr3[3][3];
int i,j;
clrscr();
//take input of first array
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n enter element for first array : ");
scanf("%d",&arr1[i][j]);
}
}
//take input of second array
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n enter element for second array : ");
scanf("%d",&arr2[i][j]);
}
}
//Process to perfrom addition
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j] = arr1[i][j] + arr2[i][j];
}
printf("\n");
}
clrscr();
printf("\n First Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr1[i][j]);

}
printf("\n");
}
printf("\n Second Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr2[i][j]);
}
printf("\n");
}
printf("\n Subtraction of Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr3[i][j]);

}
printf("\n");
}
getch();
}

//Program to perform matrix subtraction


#include<stdio.h>
#include<conio.h>
void main()
{
int arr1[3][3],arr2[3][3],arr3[3][3];
int i,j;
clrscr();
//take input of first array
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n enter element for first array : ");
scanf("%d",&arr1[i][j]);
}
}
//take input of second array
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\n enter element for second array : ");
scanf("%d",&arr2[i][j]);
}
}
//Process to perfrom subtraction
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j] = arr1[i][j] - arr2[i][j];
// printf("\t %d = %d - %d",arr3[i][j],arr1[i][j],arr2[i][j]);
}
printf("\n");
}
clrscr();
printf("\n First Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr1[i][j]);

}
printf("\n");
}
printf("\n Second Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr2[i][j]);

}
printf("\n");
}
printf("\n Subtraction of Array\n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t %d",arr3[i][j]);

}
printf("\n");
}
getch();
}

You might also like