0

I'm trying to print an array of 3x3 and placing random numbers within them that has no repetition. I think what I did was right but there just isn't any output. I know I can easily get the answer somewhere but I would like to know what is wrong with my code. Can anyone help me? Sorry if it is hard to read my code.

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

int main(void)
{
    int i,j,k,l;            //loop initilizers
    int count;              //to check for repetition
    int num;
    int grid[3][3] = {};

    srand(time(NULL));

    num = 1 + rand()%9;
    
    //generating the array & checking for repetition
    for(i=1;i=3;i++)        
    {
        for(j=1;j=3;j++)
        {
            for(k=1;k=3;k++)        //checking the row for repetition
            {
                if(grid[k][j]=num)
                count++;
            }

            for(l=1;l=3;l++)        //checking the column for repetition
            {
                if(grid[i][l]=num)
                count++;
            }

            if(count<1)
            {                //if number isn't found in either row or column,
            grid[i][j]=num; //set the number into array
            }
            else
            {
            grid[i][j]=10;
            }
        }
    }

	for(i=1; i=3; i++)      //printing the array
	{
		for(j=0; j=3; j++)
        printf("%d ", grid[i][j]);

	}
    return 0;
}

Edited by rlhh: n/a

5
Contributors
8
Replies
10
Views
7 Years
Discussion Span
Last Post by abhimanipal
0

You have = instead of == in your if statements.

You only ever call rand() once.

You never initialise count.

Edited by Banfa: n/a

0

Hmm, I changed all the = in the if statements to == , placed the rand() inside for(j=1;j=3;j++) loop and initialized count to 0.

But it still doesn't work? (Btw, I've initialized it but forgot to post the updated code here earlier)

0

The loop for( i = 1; i == 3; i++ ) never does anything.

That loop says, in English, "Start i at 1. As long as i is equal to 3, do the loop and increment i."

i doesn't start out equal to three. You need the condition to be i < 3. See how that works for you and move forward from there.

0

There are many more mistakes in your code...

1. When you are accessing the matrix elements the for loops should be

for(i=0;i<3;i++)
{
       for(j=0;j<3;j++)
        {
               // Do printing checking what ever 
         }
}

2. Initialize count to -1
3. When you are checking for repetition, you have to use == to =
4. I dont think the logic you have used for checking repetition is correct as well. What I would do would be after inputting the number use the loops I have described above to check each entry of the matrix, to see if the input exists there or not
5. Why are you modding the random number by 9 ? I get it that you need 9 entries, but that does not mean all entries have to be smaller than 9. What you want to do is call rand 9 times instead.

Edited by abhimanipal: n/a

0

The loop for( i = 1; i == 3; i++ ) never does anything.

That loop says, in English, "Start i at 1. As long as i is equal to 3, do the loop and increment i."

i doesn't start out equal to three. You need the condition to be i < 3. See how that works for you and move forward from there.

Wow, what a serious mistake that I made. Thanks for pointing it out. :)

There are many more mistakes in your code...

1. When you are accessing the matrix elements the for loops should be

for(i=0;i<3;i++)
{
       for(j=0;j<3;j++)
        {
               // Do printing checking what ever 
         }
}

2. Initialize count to -1
3. When you are checking for repetition, you have to use == to =
4. I dont think the logic you have used for checking repetition is correct as well. What I would do would be after inputting the number use the loops I have described above to check each entry of the matrix, to see if the input exists there or not
5. Why are you modding the random number by 9 ? I get it that you need 9 entries, but that does not mean all entries have to be smaller than 9. What you want to do is call rand 9 times instead.

1. Thanks, not sure why that didn't hit me earlier.
2. Sorry to ask but why initialize it to -1?
3. Fixed that based on the earlier post. :)
4. Isn't it the same as the first question? Still, thanks.
5. I'm doing so because I need the random number to be a number from 1 to 9.

Anyway, I've updated my code but still it doesn't check for repetition? Any help will be appreciated.

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

int main(void)
{
    int i,j,k,l;            //loop initilizers
    int count =-1;              //to check for repetition
    int num;
    int grid[3][3] = {};

    srand(time(NULL));

    //generating the array & checking for repetition
    for(i=1;i<=3;i++)
    {
        for(j=1;j<=3;j++)
        {
            num = 1 + rand()%9;
            grid[i][j] = num;

            for(k=1;k<=3;k++)
            {
                for(l=1;l<=3;l++)        //checking the column for repetition
                {
                if(grid[k][l]==num)
                count++;
                }
            }

            if(count<=0)
            {                //if number isn't found in either row or column,
            grid[i][j]=num; //set the number into array
            count = -1;
            }
            else
            {
            printf("repeated number = %d\n",num);
            grid[i][j]=-1;
            }
        }
    }


	for(i=1; i<=3; i++)      //printing the array
	{
		for(j=1; j<=3; j++)
        printf("%d ", grid[i][j]);

	}
    return 0;
}

Edited by rlhh: n/a

0
for(i=1;i<=3;i++)
    {
        for(j=1;j<=3;j++)
        {
            num = 1 + rand()%9;
            grid[i][j] = num;

Nop that is an out of bounds data access, undefined behaviour and asking for a crash.

You did not read post #5 by abhimanipal closely enough.

Arrays are indexed from 0 if you declare an array with a size of 3, then valid indexes are 0, 1, 2

int array[3] = {};
int a;

a = array[0]; // Good
a = array[1]; // Good
a = array[2]; // Good
a = array[3]; // Bad - out of bounds access

Edited by Banfa: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.