3

I have a matrix program that so far takes an input file in and expresses the numbers in matrices. I also wrote a function to find its transpose but I can't figure out how to do the symmetrical.

I have to take only 3 input parameters (MainMatrix[max][max] MainTranspose[max][max] and the Size)

It should return -1 if it isn't symmetrical and 0 if it is.

My program shows everything right except for 1 that says it is not symmetrical when it SHOULD be.

I hope one of you guys can help me with this.

int Symmetry (int mainmatrix[max][max], int maintranspose[max][max], int size) {
    double thesqrtSize = sqrt((double)Size);
    double fract = thesqrtSize - floor(thesqrtSize);
    if(fract > 0.0001) 
        return -1;
    int isqrtSize = (int)thesqrtSize;
    int i, j;
    for(i=0;i<isqrtSize;i++){
        for(j=0;j<isqrtSize;j++) {
            if(mainmatrix[i][j]!=maintranspose[i][j]) {
                return -1; 
            }
        }
    }
    return 0; 
}

2 Answers2

0

Floating point numbers are notoriously prone to rounding errors. And if the result of sqrt is just a little bit below the intended result, the call to floor will make it off by (almost) 1. Try adding 1 to size before taking the square root. This is safe and will guarantee a correct result provided size is a square number.

The suggested change:

double thesqrtSize = sqrt((double)(Size+1));

Obviously this means your method of using floor to check for squareness won't work. I suggest casting to int, squaring, and comparing to the original size to see if they're the same.

Imre Kerr
  • 2,388
  • 14
  • 34
  • Which line are you talking about that I should add it? – Davis Apanasova May 26 '13 at 20:34
  • For reasonably small sizes, if `Size` is a square, the computed square root will be exact in all usual hardware. – Daniel Fischer May 26 '13 at 20:36
  • Wow that worked for the one that was showing the incorrect output but I have a single number (14) in the matrix and the transpose of that is also 14 but that isn't showing it's symmetrical. I am assuming it is since the matrix=transpose. – Davis Apanasova May 26 '13 at 20:38
  • @DavisApanasova maybe `Size` is not equal to `size`. how comes size and Size from? –  May 26 '13 at 21:03
  • Only one `size` is declared, but I think the asker typed his code in here rather than copypasting it, and that's why the case is all over the place. – Imre Kerr May 26 '13 at 21:07
  • Of course if there is a global variable named `Size` somewhere, that would explain a whole lot. As a side note, who here wants to see case sensitivity burnt at the stake? :D – Imre Kerr May 26 '13 at 21:08
  • Not only that. When assinging value to `fract`, there's a mysterious `sqrtSize`. That must be `thesqrtSize`. –  May 26 '13 at 21:11
  • Size = size ( i just wrote it fast. i also edited the code, sorry about the confusion guys) – Davis Apanasova May 26 '13 at 21:18
0

Symmetry function doesn't need the transpose matrix. You can find the answer without it:

int Symmetry (int mainmatrix[max][max], int size) {
    int isqrtSize= (int)sqrt((double)size);
    if(isqrtSize*isqrtSize!=size) 
        return -1;
    int i, j;
    for(i=0;i<isqrtSize;i++){
        for(j=0;j<isqrtSize;j++) {
            if(mainmatrix[i][j]!=mainmatrix[j][i]) {
                return -1; 
            }
        }
    }
    return 0; 
}

You didn't solve the multidimensional array parameter thing in the most elegant way, but lets use this. I think your program might work on this way:

  • you declare a matrix with the maximum size
  • You fill the upper left corner of the matrix and check for symmetricity
  • symmetry method gets the entire matrix and try to figure out the dimensions and check the symmetry.

Now please see THIS CODE. I wrote a simple main function for this. As You can see, it works. So the problem should be at the method that generates the transpose matrix or with the given size in your program.

Another problem with the algorithm: You can not find dimensions from the number of the element. A matrix of 8*2 contains 16 element, so the method will see and handle it like it was a 4*4 matrix. If there are no cases like that, You can use the code above.

But: When you call the symmetry function, You compute the size as size=rows*columns, so You must know the sizes of the matrix. This means, You can simple give them to your function too. In this case, You don't have to do compute sqrt and other floating point values, You only have to check the input parameters if they are equal. With this, the function will look like this:

int Symmetry (int mainmatrix[max][max], int rows, int cols) {
    if(rows!=cols)
        return -1;
    int i, j;
    for(i=0;i<rows;i++){
        for(j=0;j<cols;j++) {
            if(mainmatrix[i][j]!=mainmatrix[j][i]) {
                return -1; 
            }
        }
    }
    return 0; 
}
  • That doesn't make a difference since you could compare either the main or the transpose (just switch the i and j variables). I'm not sure why one of the matrices that has the number 14 says "Not symmetrical" even though it should? – Davis Apanasova May 26 '13 at 20:55
  • @DavisApanasova I edited the code. This check for perfect squares should also work if the input `size` is correct. –  May 26 '13 at 21:32
  • now another matrix says its not symmetrical when it is. :( There is something very wrong going on but can't figure out what the cause is – Davis Apanasova May 26 '13 at 21:47
  • @DavisApanasova Did you tried this or the one with the transpose? In the second case change one of the return statements to `return -2;` to see where the incorrect answer comes from. And write `size` to output to see if it is incorrect. –  May 26 '13 at 21:51
  • I tried this one. So return -2 right after the if statement comparing the main matrix ? – Davis Apanasova May 26 '13 at 21:55
  • I just tried that as well, it doesn't show anything. I get all of the correst responses (if they are symmetric then it says symmetric if not then it says not symmetric) except for just one of them. – Davis Apanasova May 26 '13 at 21:56
  • Check the value returned by the function. And please copy-paste to your question the part where you call the function. –  May 26 '13 at 21:58
  • I'm kind of confused, what do you mean check the value returned by the function? How would I check that? – Davis Apanasova May 26 '13 at 22:00
  • Please paste Your entire code, and I can help You more. It is hard to help when the problem could be somewhere else. –  May 26 '13 at 22:10
  • Main has this: r * c = rows*columns int ret = Symm(mainmatrix, maintranspose, r*c); if(retrnvalue == 0) { printf("Is symmetrical.\n"); } if(retrnvalue == -1) { printf("Is not symmetrical.\n"); } – Davis Apanasova May 26 '13 at 22:25
  • This can not call my function. So: 1: copy your ENTIRE code(Ctrl+C) 2: Click Edit your question, paste the code and format it with Ctrl+K –  May 26 '13 at 22:36
  • @DavisApanasova I edited the answer, please check it –  May 27 '13 at 14:27
  • @DavisApanasova When you ask a question(at every question) and get a good,helpful answer, You should upvote/accept it. Not neccessary mine. –  May 28 '13 at 16:20