0% found this document useful (0 votes)
4 views17 pages

2-D Array Programs

The document contains various C programming exercises focused on 2-D arrays and matrices, including tasks such as calculating the sum of elements, finding transposes, and identifying the highest and lowest elements. Each exercise includes code snippets that demonstrate how to implement the required functionality. The document serves as a practical guide for learning about arrays and strings in programming.

Uploaded by

aayushjha534
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)
4 views17 pages

2-D Array Programs

The document contains various C programming exercises focused on 2-D arrays and matrices, including tasks such as calculating the sum of elements, finding transposes, and identifying the highest and lowest elements. Each exercise includes code snippets that demonstrate how to implement the required functionality. The document serves as a practical guide for learning about arrays and strings in programming.

Uploaded by

aayushjha534
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

CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

----2-D ARRAY PROGRAM—


1. WAP to find sum of all elements in m*n matrix //find sum of all elements
#include<stdio.h> for(i=0; i<m; i++){
#include<conio.h> for(j=0; j<n; j++){
int main(){ sum=sum+a[i][j];
int a[100][100], m, n, i, j, sum=0; }
printf("Enter the size of row of matrix:"); }
scanf("%d", &m); printf(“\nThe sum of elements is:%d”, sum);
printf("Enter the size of column of matrix:"); getch();
scanf("%d", &n); return 0;
}
//input matrix
for(i=0;i<m;i++){
for(j=0; j<n; j++){
printf("Enter a[%d][%d] element:", i, j);
scanf("%d", &a[i][j]);
}
}
//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t", a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

2. WAP to find transpose m*n matrix (transpose of matrix is //Calculating transpose


obtained by exchanging row and columns). for(i=0; i<m; i++){
#include<stdio.h> for(j=0; j<n; j++){
#include<conio.h> b[j][i]=a[i][j];
void main(){ }
int a[100][100], b[100][100] ,m, n, i, j; }
printf("Enter the size of row of matrix:"); //outputing transpose (row=n and column=m)
scanf("%d",&m); printf("\nThe Transpose of %d*%d matrix is:\n",m, n);
printf("Enter the size of column of matrix:"); for(i=0; i<n; i++){
scanf("%d",&n); for(j=0; j<m; j++){
//input matrix printf("%d\t",b[i][j]);
for(i=0;i<m;i++){ }
for(j=0; j<n; j++){ printf(“\n”);
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]); getch();
} }
}
//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

3. WAP to find sum of only even elements in m*n matrix


#include<stdio.h> //find sum of only even elements
#include<conio.h> for(i=0; i<m; i++){
int main(){ for(j=0; j<n; j++){
int a[100][100],m, n, i, j, sum=0; if(a[i][j]%2==0){
printf("Enter the size of row of matrix:"); sum=sum+a[i][j];
scanf("%d",&m); }
printf("Enter the size of column of matrix:"); }
scanf("%d",&n); }
printf(“\nThe sum even elements is:%d”, sum);
//input matrix
for(i=0;i<m;i++){ getch();
for(j=0; j<n; j++){ return 0;
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]);
}
}

//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

4. WAP to find sum of odd and even elements in m*n //find sum of even and odd elements
matrix. for(i=0; i<m; i++){
#include<stdio.h> for(j=0; j<n; j++){
#include<conio.h> if(a[i][j]%2==0){
int main(){ esum=esum+a[i][j];
int a[100][100],m, n, i, j, esum=0, osum=0; }
printf("Enter the size of row of matrix:"); else{
scanf("%d",&m); osum=osum+a[i][j];
printf("Enter the size of column of matrix:"); }
scanf("%d",&n);
//input matrix }
for(i=0;i<m;i++){ }
for(j=0; j<n; j++){ printf(“\nThe sum odd elements is:%d”, osum);
printf("Enter a[%d][%d] element:", i, j); printf(“\nThe sum even elements is:%d”, esum);
scanf("%d",&a[i][j]);
} getch();
} return 0;
//output matrix }
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

5. WAP to find sum of main diagonal (also called trace of //find sum of main diagonal elements
matrix) of n*n square matrix for(i=0; i<n; i++){
#include<stdio.h> for(j=0; j<n; j++){
#include<conio.h> if(i==j){
int main(){ sum=sum+a[i][j];
int a[100][100], n, i, j, sum=0; }
printf("Enter the size of square of matrix:"); }
scanf("%d",&n); }
printf(“\nThe sum main diagonal elements is:%d”, sum);
//input matrix
for(i=0;i<n;i++){ getch();
for(j=0; j<n; j++){ return 0;
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]);
}
}

//output matrix
printf("\nThe %d*%d matrix is:\n",n, n);
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

6. WAP to find sum of right diagonal of n*n square matrix //find sum of right diagonal elements
#include<stdio.h> for(i=0; i<n; i++){
#include<conio.h> for(j=0; j<n; j++){
int main(){ if((i+j)==(n-1)){
int a[100][100],m, n, i, j, sum=0; sum=sum+a[i][j];
printf("Enter the size of square of matrix:"); }
scanf("%d",&n); }
}
//input matrix printf(“\nThe sum of right diagonal elements is:%d”, sum);
for(i=0;i<n;i++){ getch();
for(j=0; j<n; j++){ return 0;
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]);
}
}

//output matrix
printf("\nThe %d*%d matrix is:\n",n, n);
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

7. WAP to find highest element of m*n matrix. //finding highest element


#include<stdio.h> max=a[0][0];
#include<conio.h> for(i=0; i<m; i++){
int main(){ for(j=0; j<n; j++){
int a[100][100],m, n, i, j, max; if(a[i][j]>max){
printf("Enter the size of row of matrix:"); max=a[i][j];
scanf("%d",&m); }
printf("Enter the size of column of matrix:"); }
scanf("%d",&n); }
printf(“\nThe Highest elements is:%d”, max);
//input matrix
for(i=0;i<m;i++){ getch();
for(j=0; j<n; j++){ return 0;
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]);
}
}

//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

8. WAP to find highest and lowest element of m*n matrix. //finding highest element
#include<stdio.h> low=a[0][0];
#include<conio.h> high=a[0][0];
int main(){ for(i=0; i<m; i++){
int a[100][100],m, n, i, j, low,high; for(j=0; j<n; j++){
printf("Enter the size of row of matrix:"); if(a[i][j]<low){
scanf("%d",&m); low=a[i][j];
printf("Enter the size of column of matrix:"); }
scanf("%d",&n); if(a[i][j]>high){
high=a[i][j];
//input matrix }
for(i=0;i<m;i++){
for(j=0; j<n; j++){ }
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]); printf(“\nThe lowest elements is:%d”, low);
} printf(“\nThe highest elements is:%d”, high);
}
getch();
//output matrix return 0;
printf("\nThe %d*%d matrix is:\n",m, n); }
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

9. WAP to find norm of m*n square matrix. (The norm is //finding norm of matrix element
defined as the square root of the sum of squares of all for(i=0; i<m; i++){
elements in the matrix) for(j=0; j<n; j++){
#include<stdio.h> sum = sum+(a[i][j]*a[i][j]);
#include<conio.h> }
#include<math.h> }
int main(){ norm = sqrt(sum);
int a[100][100],m, n, i, j, sum; printf(“\nThe norm of matrix is:%f”, norm);
double norm;
printf("Enter the size of row of matrx:"); getch();
scanf("%d",&m); return 0;
printf("Enter the size of column of matrx:"); }
scanf("%d",&n);
//input matrix
for(i=0;i<m;i++){
for(j=0; j<n; j++){
printf("Enter a[%d][%d] element:", i, j);
scanf("%d",&a[i][j]);
}
}
//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

10. WAP to find sum of individual rows of m*n matrix. //finding sum of individual rows
#include<stdio.h> for(i=0; i<m; i++){
#include<conio.h> sum=0;
int main(){ for(j=0; j<n; j++){
int a[100][100],m, n, i, j, sum; sum = sum+a[i][j];
printf("Enter the size of row of matrx:"); }
scanf("%d",&m); printf(“\nSum of %d Row :%d”,i , sum);
printf("Enter the size of column of matrx:"); }
scanf("%d",&n); getch();
return 0;
//input matrix }
for(i=0;i<m;i++){
for(j=0; j<n; j++){
printf("Enter a[%d][%d] element:", i, j);
scanf("%d",&a[i][j]);
}
}

//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

11. WAP to find sum of individual columns of m*n matrix. //finding sum of individual columns
#include<stdio.h> for(i=0; i<m; i++){
#include<conio.h> sum=0;
int main(){ for(j=0; j<n; j++){
int a[100][100],m, n, i, j, sum; sum = sum+a[j][i];
printf("Enter the size of row of matrx:"); }
scanf("%d",&m); printf(“\nSum of %d Column :%d”,j , sum);
printf("Enter the size of column of matrx:"); }
scanf("%d",&n); getch();
return 0;
//input matrix }
for(i=0;i<m;i++){
for(j=0; j<n; j++){
printf("Enter a[%d][%d] element:", i, j);
scanf("%d",&a[i][j]);
}
}

//output matrix
printf("\nThe %d*%d matrix is:\n",m, n);
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

12. WAP to add two n*n square matrix (Two matrices may //output First matrix
be added or subtracted only if they have the same dimension; printf("\nThe first %d*%d matrix is:\n",n, n);
that is, they must have the same number of rows and for(i=0; i<n; i++){
columns.) for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
#include<stdio.h> }
#include<conio.h> printf(“\n”);
int main(){ }
int a[100][100], b[100][100] , c[100][100], n, i, j;
printf("Enter the size of square matrx:"); //output Second matrix
scanf("%d",&n); printf("\nThe Second %d*%d matrix is:\n",n, n);
for(i=0; i<n; i++){
//input First matrix for(j=0; j<n; j++){
printf(“\nEnter First array elements:\n”); printf("%d\t",b[i][j]);
for(i=0;i<n;i++){ }
for(j=0; j<n; j++){ printf(“\n”);
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]);
}
}
//input Second array //Adding two matrix
printf(“\nEnter Second array elements:\n”); for(i=0; i<n; i++){
for(i=0;i<n;i++){ for(j=0; j<n; j++){
for(j=0; j<n; j++){ c[i][j]=a[i][j]+b[i][j];
printf("Enter b[%d][%d] element:", i, j); }
scanf("%d",&b[i][j]); }
}
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

//output Added array


printf("\nThe Added %d*%d matrix is:\n",n, n);
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d\t",c[i][j]);
}
printf(“\n”);
}
getch();
return 0;
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

13. WAP to find upper triangular matrix of n*n square //calculating upper triangular
matrix. (The upper triangular matrix is a special type of for(i=0; i<n; i++){
square matrix that has all the elements below the main for(j=0; j<n; j++){
diagonal as zero.) if(i>j){
#include<stdio.h> a[i][j]=0;
#include<conio.h> }
int main(){ }
int a[100][100],m, n, i, j, sum; }
printf("Enter the size square matrix:"); //output upper triangular matrix
scanf("%d",&n); printf("\nThe upper triangular matrix is:\n”);
//input matrix for(i=0; i<n; i++){
for(i=0;i<n;i++){ for(j=0; j<n; j++){
for(j=0; j<n; j++){ printf("%d\t",a[i][j]);
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]); printf(“\n”);
} }
}
//output matrix getch();
printf("\nThe %d*%d matrix is:\n",n, n); return 0;
for(i=0; i<n; i++){ }
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

14. WAP to find Lower triangular matrix of n*n square //calculating lower triangular
matrix. (The Lower triangular matrix is a special type of for(i=0; i<n; i++){
square matrix that has all the elements above the main for(j=0; j<n; j++){
diagonal as zero.) if(j>i){
#include<stdio.h> a[i][j]=0;
#include<conio.h> }
int main(){ }
int a[100][100],m, n, i, j, sum; }
printf("Enter the size square matrix:"); //output lower triangular matrix
scanf("%d",&n); printf("\nThe lower triangular matrix is:\n”);
//input matrix for(i=0; i<n; i++){
for(i=0;i<n;i++){ for(j=0; j<n; j++){
for(j=0; j<n; j++){ printf("%d\t",a[i][j]);
printf("Enter a[%d][%d] element:", i, j); }
scanf("%d",&a[i][j]); printf(“\n”);
} }
} getch();
//output matrix return 0;
printf("\nThe %d*%d matrix is:\n",n, n); }
for(i=0; i<n; i++){
for(j=0; j<n; j++){
printf("%d\t",a[i][j]);
}
printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

15. WAP to Multiply two m*n matrix.( For matrix


multiplication, the number of columns in the first matrix(n1) must //input first matrix
be equal to the number of rows in the second matrix(m2) The printf(“\nEnter first matrix:\n”);
result matrix has the number of rows of the first and the number of
for(i=0;i<m1;i++){
columns of the second matrix i.e. resultant matrix is m1*n2)
for(j=0; j<n1; j++){
#include<stdio.h> printf("Enter a[%d][%d] element:", i, j);
#include<conio.h> scanf("%d",&a[i][j]);
#include<stdlib.h> }
void main(){ }
int a[100][100], b[100][100], c[100][100], m1,m2, n1,
n2, i, j, k, sum=0;
printf("Enter the size of row of first matrix:"); //input Second matrix
scanf("%d",&m1); printf(“\nEnter second matrix:\n”);
for(i=0;i<m2;i++){
printf("Enter the size of column of first matrix:"); for(j=0; j<n2; j++){
scanf("%d",&n1); printf("Enter b[%d][%d] element:", i, j);
scanf("%d",&b[i][j]);
printf("Enter the size of row of Second matrix:"); }
scanf("%d",&m2); }

printf("Enter the size of column of Second matrix:"); //output First matrix


scanf("%d",&n2); printf("\nThe %d*%d matrix is:\n",m1, n1);
for(i=0; i<m1; i++){
if(n1!=m2){ for(j=0; j<n1; j++){
printf(“\matrix multiplication not possible”); printf("%d\t",a[i][j]);
exit(0); }
} printf(“\n”);
}
CHAPTER: 5 ARRAYS & STRINGS 2-D ARRAY

//output Second matrix


printf("\nThe %d*%d matrix is:\n",m2, n2);
for(i=0; i<m2; i++){
for(j=0; j<n2; j++){
printf("%d\t",b[i][j]);
}
printf(“\n”);
}
//Multiplying two matrix
for(i=0; i<m1; i++){
for(j=0; j<n2; j++){
for(k=0; k<n1; k++){
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
}
//output Multiplied matrix
printf("\nThe %d*%d matrix is:\n",m1, n2);
for(i=0; i<m1; i++){
for(j=0; j<n2; j++){
printf("%d\t",c[i][j]);
}
printf(“\n”);
}
getch();
}

You might also like