Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 9 PPS

Write a program for multiplication of two matrices.

#include <stdio.h>

int main() {
    int rows1, cols1, rows2, cols2, i, j, k;
    printf("Enter the number of rows and columns of the first matrix:\n");
    scanf("%d %d", &rows1, &cols1);
    printf("Enter the number of rows and columns of the second matrix:\n");
    scanf("%d %d", &rows2, &cols2);

    if (cols1 != rows2) {
        printf("Matrices cannot be multiplied!\n");
        return 0;
    }

    int matrix1[rows1][cols1], matrix2[rows2][cols2], product[rows1][cols2];

    printf("Enter the elements of the first matrix:\n");
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols1; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("Enter the elements of the second matrix:\n");
    for (i = 0; i < rows2; i++) {
        for (j = 0; j < cols2; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Multiplying matrices
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols2; j++) {
            product[i][j] = 0;
            for (k = 0; k < cols1; k++) {
                product[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    printf("The product of the matrices is:\n");
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < cols2; j++) {
            printf("%d ", product[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Write a program to find length of string without using library function.

#include <stdio.h>
int main()
{
char s[100];
int i;

printf(“Enter a string: “);
scanf(“%s”, s);

for(i = 0; s[i] != ‘\0’; ++i);

printf(“Length of string: %d”, i);
return 0;
}

Write a program to concatenate two strings without using library function  

#include <stdio.h>
#include <string.h>
void concat(char[], char[]);
int main()
{
    char s1[50], s2[30];
    printf("\nEnter String 1 :");
    gets(s1);
    printf("\nEnter String 2 :");
    gets(s2);
    concat(s1, s2);
    printf("\nConcated string is :%s", s1);
    return (0);
}
void concat(char s1[], char s2[])
{
    int i, j;
    i = strlen(s1);
    for (j = 0; s2[j] != '\0'; i++, j++)
    {
        s1[i] = s2[j];
    }
    s1[i] = '\0';
}


Post a Comment

0 Comments