Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program that takes two square matrices of integers (up to 100*100 in

ID: 3558588 • Letter: 1

Question

1. Write a program that takes two square matrices of integers (up to 100*100 in size) and calculates the scalar product of the two none main diagonals. For example, for the two following matrices:

{1 2 3   

4 5 6   

7 8 9 }   

the second matrix

{9 1 2

3 4 5

6 7 8}

The scalar product would be 3*2 + 5*4 + 7*6 =68. Note that the scalar product will be returned to the main, not printed out in the function.

Indicate the call to the function in the main program assuming the matrices are called mat1 and mat2 and the scalar product is called nsp.

Explanation / Answer

#define MAX 100
#include<stdio.h>

int scalar_product_non_main_diag(int mat1[][MAX],int mat2 [][MAX] ,int index)
{
    int i,j, nsp=0;
   
   
    for (i=0,j=index-1; i>=0,j>=0;i++,j--){
        nsp = nsp + mat1[i][j] * mat2[i][j];
    }
    return nsp;
}

int main()
{
    int mat1[MAX][MAX],mat2[MAX][MAX];
    int i,j,index,nsp;
   
    printf("INPUT THE SIZE OF ARRAY!! => ");
    scanf("%d",&index);
   
    for (i=0;i<index;i++) {
        for (j=0;j<index;j++) {
            printf(" Enter value for MAT1[%d][%d] => ",i+1,j+1);
            scanf("%d", &mat1[i][j]);
        }
    }
   
    for (i=0;i<index;i++) {
        for (j=0;j<index;j++) {
            printf(" Enter value for MAT2[%d][%d] => ",i+1,j+1);
            scanf("%d", &mat2[i][j]);
        }
    }
      
       nsp=scalar_product_non_main_diag(mat1,mat2,index);
      
       // Was not sure if the requirement was to print or not
       //hence have commented this line
       //if you want it to be printed in main
       //you can uncomment the following line
      
       //printf(" Final scalar product=> %d", nsp);
      
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote