Perform Matrix
Multiplication using Recursion
#include
#include
void multiply(int, int, int [][10], int, int,
int [][10], int [][10]);
void display(int, int, int[][10]);
int main()
{
int
a[10][10], b[10][10], c[10][10] = {0};
int
m1, n1, m2, n2, i, j, k;
clrscr();
printf("Enter rows and columns for Matrix A respectively: ");
scanf("%d%d", &m1, &n1);
printf("Enter rows and columns for Matrix B respectively: ");
scanf("%d%d", &m2, &n2);
if
(n1 != m2)
{
printf("Matrix multiplication not possible.\n");
}
else
{
printf("Enter elements in Matrix A:\n");
for (i = 0; i < m1; i++)
for (j = 0; j < n1; j++)
{
scanf("%d", &a[i][j]);
}
printf("\nEnter elements in Matrix B:\n");
for (i = 0; i < m2; i++)
for (j = 0; j < n2; j++)
{
scanf("%d", &b[i][j]);
}
multiply(m1, n1, a, m2, n2, b, c);
}
printf("On matrix multiplication of A and B the result
is:\n");
display(m1, n2, c);
}
void multiply (int m1, int n1, int a[10][10],
int m2, int n2, int b[10][10], int c[10][10])
{
static int i = 0, j = 0, k = 0;
if
(i >= m1)
{
return;
}
else
if (i < m1)
{
if (j < n2)
{
if (k < n1)
{
c[i][j] += a[i][k] * b[k][j];
k++;
multiply(m1, n1, a, m2, n2, b, c);
}
k = 0;
j++;
multiply(m1, n1, a, m2, n2, b, c);
}
j = 0;
i++;
multiply(m1, n1, a, m2, n2, b, c);
}
}
void display(int m1, int n2, int c[10][10])
{
int
i, j;
for
(i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter rows and columns for Matrix A
respectively: 2
2
Enter rows and columns for Matrix B
respectively: 2
2
Enter elements in Matrix A:
12 56
45 78
Enter elements in Matrix B:
2 6
5 8
On matrix multiplication of A and B the result
is:
304
520
480 894
Calculate Sum & Average of an Array
#include
#include
#define maxsize 10
void main()
{
int array[Maxsize];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
clrscr();
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
getch();
}
Output
Enter the value of N
10
Enter 10 numbers (-ve, +ve and zero)
-8
9
-100
-80
90
45
-23
-1
0
16
Input array elements
-8
+9
-100
-80
+90
+45
-23
-1
+0
+16
Sum of all negative numbers = -212
Sum of all positive numbers = 160
Average of all input numbers = -5.20
Find the Largest Two Numbers in a given Array
#include
#include
#define max 4
void main()
{
int array[max], i, largest1, largest2, temp;
clrscr();
printf("Enter %d integer numbers \n", max);
for (i = 0; i < max; i++)
{
scanf("%d", &array[i]);
}
printf("Input interger are \n");
for (i = 0; i < max; i++)
{
printf("%5d", array[i]);
}
printf("\n");
largest1 = array[0];
largest2 = array[1];
if (largest1 < largest2)
{
temp = largest1;
largest1 = largest2;
largest2 = temp;
}
for (i = 2; i < 4; i++)
{
if (array[i] >= largest1)
{
largest2 = largest1;
largest1 = array[i];
}
else if (array[i] > largest2)
{
largest2 = array[i];
}
}
printf("n%d is the first largest \n", largest1);
printf("%d is the second largest \n", largest2);
printf("nAverage of %d and %d = %d \n", largest1, largest2,
(largest1 + largest2) / 2);
getch();
}
Output
Enter 4 integer numbers
80
23
79
58
Input interger are
80 23 79 58
80 is the first largest
79 is the second largest
Average of 80 and 79 = 79
Calculate the Sum of the Array Elements using Pointer
#include
#include
#include
void main()
{
int i, n, sum = 0;
int *a;
clrscr();
printf("Enter the size of array A \n");
scanf("%d", &n);
a = (int *) malloc(n * sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
for (i = 0; i < n; i++)
{
sum = sum + *(a + i);
}
printf("Sum of all elements in array = %d\n", sum);
getch();
}
Output
Enter the size of array A
5
Enter Elements of First List
4
9
10
56
100
Sum of all elements in array = 179
No comments:
Post a Comment