Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1 #include<stdio.

h>
2 void main()
3 {
4 int n,check;
5 again:
6 printf("Enter the Question u want to check\n\n\n");
7 scanf("%d",&n);
8 system("cls");
9 switch(n)
10 {
11 case 1: printf("for sum and avg of number entered in array\n\n");
12 sumAvg();//QUESTION 1
13 break;
14 case 2: printf("for largest nummber in array\n\n");
15 largest();//QUESTION 2
16 break;
17 case 3: printf("for addition of matrix\n\n");
18 addMatrix();//QUESTION 3
19 break;
20 case 4: printf("program to multiply two matrix");
21 multiplyMatrix();
22 break;
23 default: printf("enter the correct question number");
24 }
25 getch();
26 system("cls");
27 printf("want to check another question \n enter 1 ");
28 scanf("%d",&check);
29 if(check==1)
30 {
31 goto again;
32 }
33 }
34 void sumAvg()
35 {
36 int arr[500],n,sum=0;
37 float avg;
38 printf("enter the how many numbers u want to add");
39 scanf("%d",&n);
40
41 for(int i=0;i<n;i++)
42 {
43 scanf("%d",&arr[i]);
44
45 }
46 for(int i=0;i<n;i++)
47 {
48 sum=sum+arr[i];
49 }
50 avg=sum/n;
51 printf("the sum and average of the numbers entered is %d and %f respectively",sum,avg);
52 getch();
53 system("cls");
54 }
55
56 void largest()
57 {
58
59 int arr[100],n,large=0,pos=0,i=0;
60 printf("enter the numbrs u want to add in array");
61 scanf("%d",&n);
62 for(i=0;i<n;i++)
63 {
64 scanf("%d",&arr[i]);
65 if(arr[i]>large)
66 {
67 large=arr[i];
68 pos=i; }
69
70 }
71 printf("%d at the index value %d",large,pos);
72 getch();
73 system("cls");
74 }
75 void addMatrix()
76 {
77 int r, c, a[100][100], b[100][100], sum[100][100], i, j;
78 printf("Enter the number of rows ");
79 scanf("%d", &r);
80 printf("Enter the number of columns ");
81 scanf("%d", &c);
82
83 printf("\nEnter elements of A matrix:\n");
84 for (i = 0; i < r; ++i)
85 for (j = 0; j < c; ++j) {
86 printf("Enter the element for A%d%d: ", i + 1, j + 1);
87 scanf("%d", &a[i][j]);
88 }
89
90 printf("Enter elements of B matrix:\n");
91 for (i = 0; i < r; ++i)
92 for (j = 0; j < c; ++j) {
93 printf("Enter the element for B%d%d: ", i + 1, j + 1);
94 scanf("%d", &b[i][j]);
95 }
96 for (i = 0; i < r; ++i)
97 for (j = 0; j < c; ++j) {
98 sum[i][j] = a[i][j] + b[i][j];
99 }
100 printf("\nSum of A and B matrices: \n");
101 for (i = 0; i < r; ++i)
102 for (j = 0; j < c; ++j) {
103 printf("%d ", sum[i][j]);
104 if (j == c - 1) {
105 printf("\n\n");
106 }
107 }
108 getch();
109 system("cls");
110 }
111 void multiplyMatrix()
112 {
113
114 int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
115 printf("Enter rows and column for the first matrix: ");
116 scanf("%d %d", &r1, &c1);
117 printf("Enter rows and column for the second matrix: ");
118 scanf("%d %d", &r2, &c2);
119 while (c1 != r2) {
120 printf("Error! Enter rows and columns again.\n");
121 printf("Enter rows and columns for the first matrix: ");
122 scanf("%d%d", &r1, &c1);
123 printf("Enter rows and columns for the second matrix: ");
124 scanf("%d%d", &r2, &c2);
125 }
126 printf("\nEnter elements of matrix 1:\n");
127
128 for (int i = 0; i < r1; ++i) {
129 for (int j = 0; j < c1; ++j) {
130 printf("Enter a%d%d: ", i + 1, j + 1);
131 scanf("%d", &first[i][j]);
132 }
133 }
134 printf("\nEnter elements of matrix 2:\n");
135
136 for (int i = 0; i < r2; ++i) {
137 for (int j = 0; j < c2; ++j) {
138 printf("Enter b%d%d: ", i + 1, j + 1);
139 scanf("%d", &second[i][j]);
140 }
141 }
142 for (int i = 0; i < r1; ++i) {
143 for (int j = 0; j < c2; ++j) {
144 mult[i][j] = 0;
145 }
146 }
147
148
149 for (int i = 0; i < r1; ++i) {
150 for (int j = 0; j < c2; ++j) {
151 for (int k = 0; k < c1; ++k) {
152 mult[i][j] += first[i][k] * second[k][j];
153 }
154 }
155 }
156 printf("\nOutput Matrix:\n");
157 for (int i = 0; i < r1; ++i) {
158 for (int j = 0; j < c2; ++j) {
159 printf("%d ", mult[i][j]);
160 if (j == c2 - 1)
161 printf("\n");
162
163
164
165
166 }
167 } getch();
168 system("cls");
169 }
170

You might also like