C ignoring incrementation

151 views Asked by At

I tried this but my compiler(Visual Studio 2013) keeps messing up things. I have a 9 by 9 matrix indexed from 1. It is 0 at the beginig. And starting from element 1:1 I start incrementing the value in the matrix or incrementing x,y, basically moving to the next matrix element.

However, the program ignores my incrementation and the fact that x,y are initially set to 1. Also it ignores a function call. Code is commented below.

I am sure this is the source I am compiling! Restarted laptop and Visual Studio but still doesn't work. Opened new project, same thing. Thanks in advance.

#include<stdio.h>
#include<stdlib.h>

unsigned int Matrix[10][10], x, y;

// Ignore this..
int checkLine()
{
    unsigned int i, j, k;
    for (k = 1; k <= 9; k++){
        if (Matrix[k][1] == 0) break;
        for (i = 1; i <= 9; i++)
            for (j = 1; j <= 9; j++)
                if (Matrix[k][i] == Matrix[k][j] && i!=j)
                    return 0;
    }
    return 1;
}

//Ignore this..
int checkColumn()
{
    unsigned int i, j, k;
    for (k = 1; k <= 9; k++){
        if (Matrix[1][k] == 0) break;
        for (i = 1; i <= 9; i++)
            for (j = 1; j <= 9; j++)
                if (Matrix[i][k] == Matrix[j][k] && i!=j)
                    return 0;
    }
    return 1;
}

//Ignore this..
int checkSquare()
{
    unsigned int i, j, k,l,m,n;
    for (m = 1; m <= 7; m = m + 3)
    for (n = 1; n <= 7; n = n + 3)

    for (k = m; k <= m + 2; k++)
    for (l = n; l <= n + 2; l++)
    for (i = m; i <= m + 2; i++)
    for (j = n; j <= n + 2; j++)
        if (Matrix[k][l] == Matrix[i][j] && !(k==i && l==j))
            return 0;
    return 1;
}

void increment()
{
    if (y == 9)
    {
        x++;
        y = 1;
    }
    else y++;
}

void decrement()
{
    if (y == 1)
    {
        x--;
        y = 9;
    }
    else
        y--;
}

void print_Matrix(){
    unsigned int i, j;

    for (i = 1; i <= 9; i++){
        for (j = 1; j <= 9; j++)
            printf("%u ", Matrix[i][j]);
        printf("\n");
    }
}

//
// MAIN. PROBLEM BELOW
//**
void main()
{
    unsigned int i, j;
    for (i = 1; i <= 9;i++)
    for (j = 1; j <= 9; j++)
        Matrix[i][j] = 0;

    print_Matrix(); // Function call is ignored here. Don't know why.***

    x = 1; 
    y = 1;
    // X and Y are OBVIOUSLY 1***

    while (x < 10) //Condition OBVIOUSLY true***
    {
        printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
        printf("\n");

        Matrix[x][y]++; //Incrementation...*** 

        print_Matrix(); // Always prints a blank Matrix consisting of only 0's***


        if (checkLine() && checkColumn() && checkSquare())
        {
            increment();
        }

        if (Matrix[x][y] == 10){
            Matrix[x][y] = 0;
            decrement();
        }



    }

    print_Matrix();

}
4

There are 4 answers

2
Pratap On

You feel that the increment is ignored because the checkSquare function is buggy. It never returned 1 and hence the increment function was never called.

1
R Sahu On

Your output is a bit confusing since the output of the line

printf("%u, %u", x, y);

runs into the output of

print_Matrix();

By adding a newline to the output of the first line, i.e. by using

printf("%u, %u\n", x, y);

you will notice that at some point x gets decremented to 0 and never gets incremented again. Since you never print Matrix[0][y], you never see the non-zero values.

In addition to the change to above printf, if you change print_Matrix to:

void print_Matrix(){
   unsigned int i, j;

   for (i = 0; i <= 9; i++){
        // ^^^ Use 0 instead of 1
      for (j = 0; j <= 9; j++)
           // ^^^ Use 0 instead of 1
         printf("%u ", Matrix[i][j]);
      printf("\n");
   }
}

you will see the non-zero values.

See working code at http://ideone.com/HlQ4xp.

0
aslg On

What happens is that you're incrementing the position marked by x and y, that's position [1][1]. Until it reaches 10 nothing interesting happens, you can actually see the top left corner of the matrix increasign to 10, but then the condition to decrement becomes true and you decrement.

See for yourself (prints),

while (x < 10) //Condition OBVIOUSLY true***
{
    printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
    printf("\n");

    Matrix[x][y]++; //Incrementation...*** 

    print_Matrix(); // Always prints a blank Matrix consisting of only 0's***


    if (checkLine() && checkColumn() && checkSquare())
    {
        increment();
    }

    if (Matrix[x][y] == 10){
        Matrix[x][y] = 0;
        decrement();
    }

    printf( "Enter to continue\n" );
    getchar();

}

It turns Y = 9, X = 0 and the [1][1] position becomes 0, so you see only zeros because you're not printing the zero indexes.

This process repeats until Y = 1 and X = 0, you increase the position until 10 so that the decrement works again.

When Y = 1, X = 0 and position [0][1] is 10, the decrement call will do x--. Since X is an unsigned int, it will underflow and become 4.2 billion something, which is greater than 10 and the loop ends.

What are you trying to achieve here?


Edit: Something even more amazing happens when you make x and y ints instead of unsigned ints.

Instead of x underflowing it will become -1. Matrix[-1][9]++ strangely increased x by 1 when I ran the code, so x went back to 0. Which means the program loops forever at this point.

The increment function was never called.

0
Sahil On

It shows matrix and increment when tested online, here are results

1) for C compiler

http://ideone.com/KRLO8w

    #include<stdio.h>
#include<stdlib.h>

unsigned int Matrix[10][10], x, y;

// Ignore this..
int checkLine()
{
    unsigned int i, j, k;
    for (k = 1; k <= 9; k++){
        if (Matrix[k][1] == 0) break;
        for (i = 1; i <= 9; i++)
            for (j = 1; j <= 9; j++)
                if (Matrix[k][i] == Matrix[k][j] && i!=j)
                    return 0;
    }
    return 1;
}

//Ignore this..
int checkColumn()
{
    unsigned int i, j, k;
    for (k = 1; k <= 9; k++){
        if (Matrix[1][k] == 0) break;
        for (i = 1; i <= 9; i++)
            for (j = 1; j <= 9; j++)
                if (Matrix[i][k] == Matrix[j][k] && i!=j)
                    return 0;
    }
    return 1;
}

//Ignore this..
int checkSquare()
{
    unsigned int i, j, k,l,m,n;
    for (m = 1; m <= 7; m = m + 3)
    for (n = 1; n <= 7; n = n + 3)

    for (k = m; k <= m + 2; k++)
    for (l = n; l <= n + 2; l++)
    for (i = m; i <= m + 2; i++)
    for (j = n; j <= n + 2; j++)
        if (Matrix[k][l] == Matrix[i][j] && !(k==i && l==j))
            return 0;
    return 1;
}

void increment()
{
    if (y == 9)
    {
        x++;
        y = 1;
    }
    else y++;
}

void decrement()
{
    if (y == 1)
    {
        x--;
        y = 9;
    }
    else
        y--;
}

void print_Matrix(){
    unsigned int i, j;

    for (i = 1; i <= 9; i++){
        for (j = 1; j <= 9; j++)
            printf("%u ", Matrix[i][j]);
        printf("\n");
    }
}

//
// MAIN. PROBLEM BELOW
//**
void main()
{
    unsigned int i, j;
    for (i = 1; i <= 9;i++)
    for (j = 1; j <= 9; j++)
        Matrix[i][j] = 0;

    print_Matrix(); // Function call is ignored here. Don't know why.***

    x = 1; 
    y = 1;
    // X and Y are OBVIOUSLY 1***

    while (x < 10) //Condition OBVIOUSLY true***
    {
        printf("%u, %u", x, y); //Keeps printing 0,3 and eventually 0,2***
        printf("\n");

        Matrix[x][y]++; //Incrementation...*** 

        print_Matrix(); // Always prints a blank Matrix consisting of only 0's***


        if (checkLine() && checkColumn() && checkSquare())
        {
            increment();
        }

        if (Matrix[x][y] == 10){
            Matrix[x][y] = 0;
            decrement();
        }



    }

    print_Matrix();

}

2) for c++ compiler C++ 4.9.2 (changed return type of main to int)

http://ideone.com/Ey5nG1

In your image starting value of 0, 3 is due to buffer limit of command prompt. As the program never ends, so it terminates abruptly and latest few bytes are stored in buffer and is only shown that much. To see complete output redirect it to a file and open it.