2

So we have a nxn matrix (n <= 20). If a[i][j] == 0 when i > j and a[i][j] != 0 when i <= j ... or ... a[i][j] == 0 when i < j and a[i][j] != 0 when i >= j ... the matrix is called triangular. I suppose in a 3x3 triangular matrixes look like this

1 1 1     1 0 0 
0 1 1     1 1 0 
0 0 1     1 1 1

Here's my code:

    bool flag;
    int count = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            if ((i > j && a[i][j] != 0) || (i <= j && a[i][j] == 0))
            {
                count++;
                break;
            }
        }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            if ((i < j && a[i][j] != 0) || (i >= j && a[i][j] == 0))
            {
                count++;
                break;
            }
        }

    if (count == 2) flag = false;
    else flag = true;
    cout << flag;

Now, about the issue - the two examples of matrixes I gave before the code should always return 1 (true) as they are triangular by definition, while, say, this:

0 1 0
0 0 5
1 3 4

should always return 0 (false) as it is by no means triangular. My problem is that no matter what I input, fore some reason it always returns 1 (true) telling me that the matrix is triangular while it clearly is not. My question is how can I possibly fix this? I have absolutely no idea what my mistake is though I suspect it has something to do with my count approach, which is basically - if some of the conditions for a triangular matrix is not met - break the cycle and add +1 to the counter, then check if it's a reversed triangular matrix and if it still isn't add one more to the counter. In the end, the counter will be either 1 or 2, which will indicate whether the matrix is triangular or not. There's a leak somewhere and I can't seem to be able to find it. If it turns out it's all wrong, I'm open for suggestions to do it some other way.

user3213110
  • 199
  • 4
  • 14
  • 6
    You've told a story but forgotten to ask a question. Did you have a question? This is not a service for debugging your buggy code; instead, ask a *specific* technical question about some aspect of your code that you don't understand. – Eric Lippert Jan 24 '14 at 17:41
  • 2
    If your question is "what are some techniques I could use to find my bug?" then a good answer would be: write down a simple non-triangular matrix. Now *by hand* write down the *exact sequence of statements that should execute up to the point where 0 is returned*. Now step through the code in the debugger. The point where your list and reality diverge is where the bug is. – Eric Lippert Jan 24 '14 at 17:44
  • There is one moment that I just can't skip: `if (count == 2) flag = false; else flag = true;` looks funny :) `flag = (count != 2);`. You can delete parenthesis if you want. – FreeNickname Jan 24 '14 at 17:44
  • You should probably also read this before you post here again: http://stackoverflow.com/questions/how-to-ask – Eric Lippert Jan 24 '14 at 17:48
  • How am I not specific enough? The output of the code should be either True or False but it ALWAYS returns True. I just can't find my mistake. Apparently there's something wrong with my count integer and flag boolean, I just can't figure out what. Debuggers don't really help as my code is working perfectly, it just doesn't return correct values. – user3213110 Jan 24 '14 at 17:59
  • 1
    If your code was working _perfectly_ it wouldn't return incorrect values :/ – Captain Obvlious Jan 24 '14 at 18:12
  • Your one an only function `main()` never returns `1`. Yo what's your question, "why does it return 1"? – harper Jan 24 '14 at 18:32
  • @Captain Obvlious - I meant it works perfectly in a "debugging way". Anyway, now that I can't figure where I've mistaken how am I going to correct it? Isn't that the very purpose of the site? To help people with such problems. A few weeks ago my account got banned because I asked a question without providing a code. Now I get yelled at because I've given too much code? Is this site of any help to newbies at all? It's full of useless Captain Obviouses, that's all. – user3213110 Jan 24 '14 at 19:07
  • @harper - Apparently I've got something called "bool flag". That flag returns 1 if true and 0 if false. It stands out for whether the said nxn matrix is triangular or not. If it is - it returns 1, if it ain't - it returns 0. What's so hard to understand about it? The problem is that it basically tells me that every friggin matrix I input is a triangular one, even ones which most certainly are not. No one talks about any main functions. – user3213110 Jan 24 '14 at 19:09
  • Did I gave sufficient enough information now? Cause that's really all I've got. – user3213110 Jan 24 '14 at 19:32
  • 1
    @user3213110 Try printing your matrix before checking it. Maybe your input code (I see it in a previous version of your question) is buggy? Also print your `count` and `n`: do they have unexpected values, like `0`? – anatolyg Jan 24 '14 at 20:35
  • @anatolyg - I think I found my mistake. Apparently I'm breaking only out of the inner for cycles. Any idea how to break both "for's" when I get a mismatch? – user3213110 Jan 24 '14 at 22:32
  • When you want to break the outer loop too you have to modify the outer loop's counter variable with `i = n;`. This only works if there is no other code in the outer loop after your inner loop. Your example fulfills this condition. – harper Jan 25 '14 at 08:06

0 Answers0