Skip to content

Commit 7f941c0

Browse files
authored
Array Programs
1 parent 1bf30f1 commit 7f941c0

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

Matrix_Addition_Multiplication.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void main()
5+
{
6+
int i, j, k, rows1, columns1, rows2, columns2, a[10][10], b[10][10], sum[10][10], product[10][10];
7+
printf("Enter the Number of Rows & Columns of the 1st Matrix :: ");
8+
scanf_s("%d %d", &rows1, &columns1);
9+
printf("\nEnter the Elements of the 1st Matrix :: ");
10+
for (i = 0; i < rows1; i++)
11+
for (j = 0; j < columns1; j++)
12+
scanf_s("%d", &a[i][j]);
13+
14+
printf("Enter the Number of Rows & Columns of the 2nd Matrix :: ");
15+
scanf_s("%d %d", &rows2, &columns2);
16+
printf("\nEnter the Elements of the 2nd Matrix :: ");
17+
for (i = 0; i < rows2; i++)
18+
for (j = 0; j < columns2; j++)
19+
scanf_s("%d", &b[i][j]);
20+
21+
if ((rows1 == rows2) && (columns1 == columns2)) /* Addition of 2 Matrices */
22+
{
23+
for (i = 0; i < columns1; i++)
24+
for (j = 0; j < rows1; j++)
25+
sum[i][j] = a[j][i] + b[i][j];
26+
27+
printf("\nThe Addition of the 2 Matrices yields :: \n");
28+
for (i = 0; i < columns1; i++)
29+
{
30+
for (j = 0; j < rows1; j++)
31+
printf("%d\t", sum[i][j]);
32+
printf("\n");
33+
}
34+
}
35+
else
36+
printf("\n Addition of the 2 Matrices is not possible since their orders are different");
37+
38+
39+
if (columns1 == rows2)
40+
{
41+
for (i = 0; i < rows1; i++)
42+
for (j = 0; j < columns2; j++)
43+
{
44+
product[i][j] = 0;
45+
for (k = 0; k < columns1; k++)
46+
product[i][j] += a[i][k] * b[k][j];
47+
48+
}
49+
50+
printf("\nThe Multipication of the 2 Matrices yields :: \n");
51+
for (i = 0; i < rows1; i++)
52+
{
53+
for (j = 0; j < columns2; j++)
54+
printf("%d\t", product[i][j]);
55+
printf("\n");
56+
}
57+
}
58+
else
59+
printf("\n Multiplication of the 2 Matrices is not possible!!!");
60+
61+
62+
63+
_getch();
64+
}
65+

Sort_Array.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void main()
5+
{
6+
int i, j, no_terms, a[10], temp;
7+
8+
printf("Enter the Number of Elements of the Array :: ");
9+
scanf_s("%d", &no_terms);
10+
printf("\nEnter the Elements of the Array :: ");
11+
for (i = 0;i < no_terms;i++)
12+
scanf_s("%d", &a[i]);
13+
for (i = 0;i < no_terms-1;i++)
14+
for (j = 0;j < no_terms - 1 - i;j++)
15+
if (a[j] > a[j + 1])
16+
{
17+
temp = a[j];
18+
a[j] = a[j + 1];
19+
a[j + 1] = temp;
20+
}
21+
22+
printf("\nThe Elements of the Array afte Sorting are :: \n");
23+
for (i = 0;i < no_terms;i++)
24+
printf("%d\t", a[i]);
25+
26+
_getch();
27+
}
28+

Sum_Average_Array.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void main()
5+
{
6+
int i, no_terms, a[10], sum = 0;
7+
float average;
8+
printf("Enter the Number of Elements of the Array :: ");
9+
scanf_s("%d", &no_terms); /*No of Elements */
10+
printf("\nEnter the Elements of the Array :: ");
11+
for (i = 0;i < no_terms;i++)
12+
scanf_s("%d", &a[i]);
13+
for (i = 0;i < no_terms;i++)
14+
sum += a[i];
15+
average = (float)sum / no_terms;
16+
printf("\nThe Sum of the Elements of the Array is :: %d", sum);
17+
printf("\nThe Average of the Elements of the Array is :: %.2f", average);
18+
_getch();
19+
}
20+

Transpose.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void main()
5+
{
6+
int i, j, rows, columns, a[10][10], b[10][10];
7+
printf("Enter the Number of Rows & Columns of the Matrix :: ");
8+
scanf_s("%d %d", &rows, &columns);
9+
printf("\nEnter the Elements of the Matrix :: ");
10+
for (i = 0;i < rows;i++) /*Accepting the Values*/
11+
for (j = 0;j < columns;j++)
12+
scanf_s("%d", &a[i][j]);
13+
for (i = 0;i < columns;i++) /*Transpose*/
14+
for (j = 0;j < rows;j++)
15+
b[i][j] = a[j][i];
16+
printf("\nThe Transpose of the Matrix is :: \n");
17+
for (i = 0;i < columns;i++)
18+
{
19+
for (j = 0;j < rows;j++)
20+
printf("%d\t", b[i][j]);
21+
printf("\n");
22+
}
23+
_getch();
24+
}
25+

0 commit comments

Comments
 (0)