Why a wrong number of 1 is printed?

Viewed 123

My function should randomly insert a user-chosen number of 1 into my matrix. The difficulty lies in the fact that if a cell contains a 1 the cells around it must be set to 0. Why my code print a wrong number of 1? In the code below I had thought to first set the entire matrix to 0, then randomly generate a cell to be set to 1, after having checked it contains 0 and the distance between this cell and other cells containing 1 is >= 1. All this is done until the number m entered by the user becomes 0.

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

void initialize(int n, int a[n][n]);
void createMap(int n, int a[n][n], int m);
int check (int i, int j, int v, int w);
void print(int n, int a[n][n]);

int main(){
    
    int n;
    printf("Insert square matrix size: ");
    scanf("%d", &n);
    
    int m;
    printf("Insert 1s number: ");
    scanf("%d", &m);
    
    int a[n][n];
    
    initialize(n,a);
    createMap(n,a,m);
    
}

//Filling the matrix with 0
void initialize(int n, int a[n][n]){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            a[i][j] = 0;
        }
    }
}

//Setting in random position 1 value
void createMap(int n, int a[n][n], int m){
    int x1; int x2;
    int b[0][0];
    while (m > 0){
        int i = rand() % n;
        int j = rand() % n;
        if (a[i][j] == 0 && (check(i,j,x1,x2) == 1)){
            a[i][j] = 1;
            m--;
            //I have to fill b array with coordinates and then to pass
            //b array to check function to do the check in the whole b array
        }
    }
    print(n,a);
}

//checking if I can set the value to 1
int check (int x1, int y1, int x2, int y2){
    if (sqrt(pow((x1-x2),2) + pow((y1-y2),2)) >= 1){
        return 1;
    } else {
        return 0;
    }
}

//Printing the matrix
void print(int n, int a[n][n]){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            printf("\t%d",a[i][j]);
        }
        puts("");
    }
}
2 Answers

After generating the coordinates i and j, you should calculate the distance between that cell and the other cells set to 1. You can use the Manhattan formula to do that. If the distance between the newly generated cell and the other cells set to 1 is grater than or equal to 1, you can go ahead and generate the other ones, otherwise you should set it back to zero and generate new coordinates.

This is homework right?

A few issues:

  1. You have a variable matrix that you don't use.
  2. You set a[k][l] = 1; before you know what the old value was, and therefore can't tell if m should be incremented or not.
  3. Your bounds checking is messy, it is difficult to see if it is correct, and if you update m and all array elements around a[k][l] correctly. Nobody wants to look at messy code, unless they get paid. They will just assume that it is wrong. So that part of the code is by definition wrong, and nobody can tell you why.
  4. Your bounds checking assumes you have a 4x4 array.

... and a few others.

Point 1 is easy to fix, and it is not important. It just adds to the ugliness of your code.

Point 2 is solved by only updating a[k][l] and m if a[k][l] was zero:

if (a[k][l] == 0)
  {
    a[k][l] = 1;
    m--;
  }

Point 3 can be most easily fixed by creating a separate function that does resetting of single array elements with bounds checking. Since I assume this is homework, I am not going to write this for you, unless you have made an honest try yourself first. The function signature could look like this:

/*
   Set value of `a[line][column]` to zero and increment `*m` if value
   was changed.
   Nothing is done if `line` or `column` are out of bounds.
*/
 void matrix_element_reset(int n, a[n][n], int line, int column, int *m);

You can then easily reset array elements around a[k][l] like this:

/* This can be simplified with loops */
matrix_element_reset(n, a, k-1, l, &m);
matrix_element_reset(n, a, k, l, &m);
matrix_element_reset(n, a, k+1, l, &m);
.....

Point 4: Your variable n stores the dimension of your matrix. Use it.

Related