Home > Mobile >  Why does С ignore if statement?
Why does С ignore if statement?

Time:10-19

I want to do 1D array with further conversion to 2D because I want to work with it via functions. There is an initialization function to create a system of coordinates, and then a function to draw it, but С ignores my if statement in creating the array function.

Code of initialization of array:

void inicialise(char* array, int width, int height, char ic)
{
    for (int i = 0; i < height; i  )
    {
        if (i == height / 2)
        {
            
            for (int b = 0; b < width; b  )
            {
                array[b] = 'x';
            }
        }
        else
        {
            
            for (int b = 0; b < width; b  )
            {
                if (b == width / 2)
                {
                    
                    array[b] = 'y';
                }
                else
                {
                    array[b] = ic;
                }
                
            }
        }
        
    }
}

Code of drawing like 2D array

void draw(char* array, int w, int h)
{
    for (int i = 0; i < h; i  )
    {
        for (int b = 0; b < w; b  )
        {
            printf("%c", array[b]);
        }
        printf("\n");
    }
}

Expected output:

00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
xxxxxxxxxxxxxxxxxxxxxx
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000

Real output:

00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000
00000000000y0000000000

Why X-axis isn't drawing? Thanks a lot in advance for your answer!

CodePudding user response:

When iterating over flattened 2D array you need to calculate index correctly: I guess to fix your code you need to replace each:

array[b]

with

array[width * i   b]

The offset is i rows of width character each plus b characters.

CodePudding user response:

As I understand your question, you are using a 1D array to represent what is actually a 2D structure (a matrix). This, in itself, is not a problem.

However, when doing so, you have to remember to add the 'row offset' to each and every index you use when accessing elements. This offset will be the row number multiplied by the width of the array.

In your code, each run through the outer (i-indexes) loops accesses the same row of the array (the first), because you use just the b index for every row – you are only ever writing (and re-writing) to the very first row of the matrix, so the output from the loop that writes the x-axis is replaced in the very next loop.

To fix this, you need to add a value of i * width to the array indexes you use. In your inicialise function, this will be like this:

void inicialise(char* array, int width, int height, char ic)
{
    for (int i = 0; i < height; i  ) {
        int row = i * width; // Offset for each row
        if (i == height / 2) {

            for (int b = 0; b < width; b  ) {
                array[row b] = 'x'; // Add row offset
            }
        }
        else {
            for (int b = 0; b < width; b  ) {
                if (b == width / 2) {

                    array[row b] = 'y'; // Add row offset
                }
                else {
                    array[row b] = ic; // Add row offset
                }

            }
        }
    }
}

Similarly, in your draw function:

void draw(char* array, int w, int h)
{
    for (int i = 0; i < h; i  ) {
        int row = i * w;
        for (int b = 0; b < w; b  ) {
            printf("%c", array[b row]);
        }
        printf("\n");
    }
}

Taking the 22 x 22 matrix you have shown in your example output, a main function would look something like this:

#define NROWS 22
#define NCOLS 22

int main(void)
{
    char test[NROWS * NCOLS];
    inicialise(test, NCOLS, NROWS, '0');
    draw(test, NCOLS, NROWS);
    return 0;
}

The output this generates is:

00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
xxxxxxxxxxxxxxxxxxxxxxx
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000
00000000000y00000000000

CodePudding user response:

The problem is in the way that the array is being indexed. It should be indexed with something like (i * width) b, where i is the row number and b is the column number.

The following should work as expected:

void inicialise(char* array, int width, int height, char ic)
{
    for (int i = 0; i < height; i  )
    {
        for (int b = 0; b < width; b  )
        {
            char val;
          
            if (i == height / 2)
            {
                val = 'x';
            }
            else if (b == width / 2)
            {
                val = 'y';
            }
            else
            {
                val = ic;
            }

            array[(i * width)   b] = val;
        }       
    }
}

and

void draw(char* array, int w, int h)
{
    for (int i = 0; i < h; i  )
    {
        for (int b = 0; b < w; b  )
        {
            printf("%c", array[(i*w)   b]);
        }
        printf("\n");
    }
}

Program output: enter image description here

  •  Tags:  
  • c
  • Related