Awais
Awais

Reputation: 1

Is there a way to determine whether the diagonal elements in a matrix are all the same in C

I have this assignment which asks the following questions:

Write the pseudocode (algorithm) for the following program definition: A text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.

The function is called checkdiag (int checkdiag (int matrix[][100], int size)) that will return 1 if all the numbers on the main diagonal are the same and 0 otherwise.

Your program will read the array from the file, check the diagonal and print a report containing the size and the status of the diagonal. A typical report would look like The matrix is 3x3 and all the numbers on the main diagonal are the same.

Here is the code I have so far.

int checkdiag(int matrix[][100], int size);

int checkdiag(int matrix[][100], int size)
{
    int status;
    for (int k=0;k<size;++k)
    {
        if ((k>0)&&(matrix[k][k] == matrix[k-1][k-1]))
        {
            status =1;
        }
        else if ((matrix[k][k]!=matrix[k-1][k-1])&&(k>0))
        {
            status = 0;
        }       
    }
    return(status);
}

int main(void)
{
    FILE*mat = fopen ("matrix.txt","r");
    int s, diagonal;
    fscanf(mat, "%d", &s);
    int  matr[s][s];
    for (int i=0; i<s;++i)
    {
        for(int j=0;j<s;++j)
        {
            fscanf(mat,"%d",&matr[i][j]);
        }
    }
    fclose(mat);
    diagonal = checkdiag(matr,s);
    printf("The matrix is a %dx%d matrix. ",s,s);
    if (diagonal==0)
    {
        printf("The numbers on the main diagonal are not the same.");
    }
    else if(diagonal==1)
    {
        printf("The numbers on the main diagonal are the same.");
    }
    return(0);
}

The problem with this code is that if the very last two elements in a matrix are the same, it disregards the rest of the numbers along the diagonal and returns 1. So to fix this, I included a break statement inside the second if condition, as follows

int checkdiag(int matrix[][100], int size);

int checkdiag(int matrix[][100], int size)
{
    int status;
    for (int k=0;k<size;++k)
    {
        if ((k>0)&&(matrix[k][k] == matrix[k-1][k-1]))
        {
            status =1;
        }
        else if ((matrix[k][k]!=matrix[k-1][k-1])&&(k>0))
        {
            status = 0;
            break;
        }       
    }
    return(status);
}

int main(void)
{
    FILE*mat = fopen ("matrix.txt","r");
    int s, diagonal;
    fscanf(mat, "%d", &s);
    int  matr[s][s];
    for (int i=0; i<s;++i)
    {
        for(int j=0;j<s;++j)
        {
            fscanf(mat,"%d",&matr[i][j]);
        }
    }
    fclose(mat);
    diagonal = checkdiag(matr,s);
    printf("The matrix is a %dx%d matrix. ",s,s);
    if (diagonal==0)
    {
        printf("The numbers on the main diagonal are not the same.");
    }
    else if(diagonal==1)
    {
        printf("The numbers on the main diagonal are the same.");
    }
    return(0);
}

The issue this presented is my code would always go to status = 0, regardless of the array I use. This is an example of an array I used and the output it gave me (with the code including the break statement)

matrix.txt:

4
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4

output:

enter image description here

The image reads:

The matrix is a 4x4 matrix. The numbers on the main diagonal are not the same.

Any help on this?

Upvotes: 0

Views: 963

Answers (1)

Sam Henke
Sam Henke

Reputation: 399

When you reset a status variable for each k, you only keep the information about the last pair you've checked. Instead, you want to return 0 if any pair doesn't check equal, and 1 if they are all equal. That could look like this:

for (int k = 1; k < size; ++k) {
  if (matrix[k-1][k-1] != matrix[k][k]) return 0;
}
return 1;

Notice that we only reach return 1 if matrix[k-1][k-1] == matrix[k][k] for each k from 1 to size.1.

Upvotes: 0

Related Questions