📌  相关文章
📜  国际空间研究组织 | ISRO CS 2014 |问题 65(1)

📅  最后修改于: 2023-12-03 15:07:34.189000             🧑  作者: Mango

ISRO CS 2014 - Question 65

This question from the ISRO CS 2014 exam presents a problem related to matrices. The problem requires you to write a program that can perform matrix multiplication. The program must input two matrices and output their product.

Problem Statement

Given two matrices, A and B, of dimensions mxn and nxp respectively, find their product. The program should take the input matrices as input from the user and output the product matrix.

Input

The program requires the input of the following variables:

  • m, n, p - the dimensions of matrices A, B, and their product, respectively
  • The elements of matrices A and B
Output

The program outputs the product of matrices A and B, which is a matrix of dimensions mxp.

Example
Input
Enter the number of rows and columns of matrix A: 
2 3
Enter the elements of matrix A: 
1 2 3
4 5 6
Enter the number of rows and columns of matrix B: 
3 2
Enter the elements of matrix B: 
1 2
3 4
5 6
Output
Product of matrices A and B is:
22 28
49 64
Solution

The problem can be solved in C, Java, Python or any other programming language that supports nested loops. The solution involves taking input for two matrices A and B, checking if the number of columns of matrix A is equal to the number of rows in matrix B, and then multiplying the individual elements of A and B to form the product matrix.

C Solution

#include <stdio.h>

int main()
{
  int A[10][10], B[10][10], product[10][10];
  int m, n, p, q, i, j, k;

  printf("Enter the number of rows and columns of matrix A: ");
  scanf("%d %d", &m, &n);

  printf("Enter the elements of matrix A: \n");
  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
      scanf("%d", &A[i][j]);

  printf("Enter the number of rows and columns of matrix B: ");
  scanf("%d %d", &p, &q);

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

  printf("Enter the elements of matrix B: \n");
  for (i = 0; i < p; i++)
    for (j = 0; j < q; j++)
      scanf("%d", &B[i][j]);

  for (i = 0; i < m; i++) {
    for (j = 0; j < q; j++) {
      product[i][j] = 0;
      for (k = 0; k < n; k++)
        product[i][j] += A[i][k] * B[k][j];
    }
  }

  printf("Product of matrices A and B is: \n");
  for (i = 0; i < m; i++) {
    for (j = 0; j < q; j++)
      printf("%d ", product[i][j]);
    printf("\n");
  }

  return 0;
}

Python Solution

def matrix_multiplication(A, B):
    rows_A = len(A)
    cols_A = len(A[0])
    rows_B = len(B)
    cols_B = len(B[0])

    if cols_A != rows_B:
        return "Matrices cannot be multiplied"

    product = [[0 for row in range(cols_B)] for col in range(rows_A)]
    for i in range(rows_A):
        for j in range(cols_B):
            for k in range(cols_A):
                product[i][j] += A[i][k] * B[k][j]

    return product

A = [[1, 2, 3],
     [4, 5, 6]]

B = [[1, 2],
     [3, 4],
     [5, 6]]

print(matrix_multiplication(A, B))

Java Solution

import java.util.Scanner;

public class MatrixMultiplication {
  public static void main(String[] args) {
    int A[][] = new int[10][10];
    int B[][] = new int[10][10];
    int product[][] = new int[10][10];

    int m, n, p, q, i, j, k;

    Scanner s = new Scanner(System.in);
    System.out.print("Enter the number of rows and columns of matrix A: ");
    m = s.nextInt();
    n = s.nextInt();

    System.out.println("Enter the elements of matrix A: ");
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        A[i][j] = s.nextInt();

    System.out.print("Enter the number of rows and columns of matrix B: ");
    p = s.nextInt();
    q = s.nextInt();

    if (n != p) {
      System.out.println("Matrices cannot be multiplied");
      return;
    }

    System.out.println("Enter the elements of matrix B: ");
    for (i = 0; i < p; i++)
      for (j = 0; j < q; j++)
        B[i][j] = s.nextInt();

    for (i = 0; i < m; i++) {
      for (j = 0; j < q; j++) {
        product[i][j] = 0;
        for (k = 0; k < n; k++)
          product[i][j] += A[i][k] * B[k][j];
      }
    }

    System.out.println("Product of matrices A and B is: ");
    for (i = 0; i < m; i++) {
      for (j = 0; j < q; j++)
        System.out.print(product[i][j] + " ");
      System.out.println();
    }
  }
}
Conclusion

The matrix multiplication problem is an important problem in computer science and has numerous applications in fields such as graphics, physics, and robotics. The solution provided in this post shows how to solve this problem in different programming languages.