pages

Sunday, March 29, 2020

C Progrmming Slip 7


BCA PRACTICAL SLIPS SOLUTION

Slip7
Q1. Write a C program to accept the x and y coordinates of two points and compute the
distance between the two points. [15 Marks]
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float distance, a, b, c, d;
printf("\nEnter The Coordinates of Point A:\n");
printf("\nX - Axis Coordinate: \t");
scanf("%f", &a);
printf("\nY - Axis Coordinate: \t");
scanf("%f", &b);
printf("\nEnter The Coordinates of Point B:\n");
printf("\nx - Axis Coordinate:\t");
scanf("%f", &c);
printf("\nY - Axis Coordinate: \t");
scanf("%f", &d);
distance = sqrt((c - a) * (c - a) + (d - b) * (d- b));
printf("\nDistance between Points A and B: %f\n", distance);
return 0;
}

------------------------------------------------------------------------

Q2. Write a program to calculate Multiplication of two matrices of order m*n.
[25 Marks]
#include <stdio.h>
int main()
{
 int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
 printf("Enter rows and column for first matrix: ");
 scanf("%d %d", &r1, &c1);
 printf("Enter rows and column for second matrix: ");
 scanf("%d %d",&r2, &c2);
 // Column of first matrix should be equal to column of second matrix
and
 while (c1 != r2)
 {
 printf("Error! column of first matrix not equal to row of
second.\n\n");
 printf("Enter rows and column for first matrix: ");
 scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
 scanf("%d %d",&r2, &c2);
 }
 // Storing elements of first matrix.
 printf("\nEnter elements of matrix 1:\n");
 for(i=0; i<r1; ++i)
 for(j=0; j<c1; ++j)
 {
 printf("Enter elements a%d%d: ",i+1, j+1);
 scanf("%d", &a[i][j]);
 }
 // Storing elements of second matrix.
 printf("\nEnter elements of matrix 2:\n");
 for(i=0; i<r2; ++i)
 for(j=0; j<c2; ++j)
 {
 printf("Enter elements b%d%d: ",i+1, j+1);
 scanf("%d",&b[i][j]);
 }
 // Initializing all elements of result matrix to 0
 for(i=0; i<r1; ++i)
 for(j=0; j<c2; ++j)
 {
 result[i][j] = 0;
 }
 // Multiplying matrices a and b and
 // storing result in result matrix
 for(i=0; i<r1; ++i)
 for(j=0; j<c2; ++j)
 for(k=0; k<c1; ++k)
 {
 result[i][j]+=a[i][k]*b[k][j];
 }
 // Displaying the result
 printf("\nOutput Matrix:\n");
 for(i=0; i<r1; ++i)
 for(j=0; j<c2; ++j)
 {
 printf("%d \t ", result[i][j]);

 }
 return 0;
}

No comments:

Post a Comment