How would I go about making sure these two matrices have non-zero, positive row and column values?

76 views Asked by At

I set up my code without considering this fact needed to be detected, and as a result I can't seem to figure out how to implement it now. This is a simple matrix multiplier.

Basically, it needs to detect two conditions.

  1. If the columns in matrix 1 is not equal to the rows in matrix 2, exit the program with an error message. I have done this.

  2. If the value the user inputs for the row and column values for matrix1 and matrix2 are non-zero, positive values. I'm having trouble with this.

The way our prof has it set up is that we copy and paste in a set of values and it does it. But if we're copy and pasting it in to the input, I'm confused how it should detect the error with rows and columns when in order to accept the rest of the data I'm using for loops to scan all the values in, and the length of the for loop is dependent on the input of rows and columns for matrix1 and matrix2. If those are negative, the for loop won't work anyway.

Really, any help would be appreciated, I've got every other part finished except this minute detail. Thanks so much.

#include <stdio.h>

int main(int argc, char *argv[]) {
    int rows1 = 1, columns1 = 1, rows2 = 1, columns2 = 1;    // variables for number of rows and columns in each matrix 
    int i, j, k;                                             // loop variables 

    // These will affect the loop's length
    scanf("%d %d", &rows1, &columns1);
    int matrix1[rows1][columns1];
    for (i = 0; i < rows1; i++) {
        for (j = 0; j < columns1; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    scanf("%d %d", &rows2, &columns2);
    int matrix2[rows2][columns2];
    for (i = 0; i < rows2; i++) {
        for (j = 0; j < columns2; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    int resultMatrix[rows1][columns2]; // Initialization of resultMatrix, that holds the result of the multiplication

    int matrix1RowIndex = 0, matrix1ColIndex = 0, matrix2ColIndex = 0;

    if (columns1 != rows2) {
        printf("Sorry, the amount of columns in the first matrix must equal the amount of rows in the second matrix.\n");
    }
    else {      
        // Loop to set the values of resultMatrix's indices
        for (i = 0; i < rows1; i++) { // rows
            for (j = 0; j < columns2; j++) { // columns
                resultMatrix[i][j] = 0;
                for (k = 0; k < columns1; k++) { // for each individual index to be summed for the resultMatrix's value
                    resultMatrix[i][j] += (matrix1[matrix1RowIndex][matrix1ColIndex + k] * matrix2[matrix1ColIndex + k][matrix2ColIndex]);
                }
                matrix2ColIndex++;
            }
            matrix1RowIndex++;
            matrix2ColIndex = 0;
        }

        //prints resultMatrix
        for (i = 0; i < rows1; i++) { // rows
            for (j = 0; j < columns2; j++) { // columns
                printf("%6d", resultMatrix[i][j]);
            }
            printf("\n");
        }
    }
}
1

There are 1 answers

4
Perry On BEST ANSWER

It would seem that you can just check that the values in question are greater than zero using an if statement, can't you?