Solved

# Confused as hell with multidimensial array!

Posted on 2000-03-15
290 Views
Ok I have generated a 4 digit random number. I want each digit to be unique (i.e. 2672 is invalid because 2 appears twice). The way I propose to get rounf this is to have a multi dimensional array of [10][2] - 0..9 and the second dimension set to all 0's.

Each digit of the random number is created seperatly in a for loop. I then want the number that is generated to look in the multidim array and "Flag" It's presence

i.e. the digit "7" looks through 0, 1, 2, 3, 4, 5, 6 and then finds "7" and flags the 0 to 1 to say it's been used. If it finds that "7" has already been used (i.e the falg has been set to 1) then it changes that number by 1 and carries on with the check. I am confused. Can i not check on dimension of a multidim array at once? The code I have has been changed so many times this probably isn't the best try I have had but hey! If you can help explain multi arrays to me properly and help me out with this code you can have all these pints (shyaa ! I mean points!)...

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

#define SUCCESS 0
#define ERROR 1

int random_number (void)
{
int i, j, k, r[4];
int a[10][1] = {{0, 0}, {1, 0},
{2, 0}, {3, 0},
{4, 0}, {5, 0},
{6, 0}, {7, 0},
{8, 0}, {9, 0}};

srand(time(0));
for (i = 0 ; i < 4 ; i++)
{
r[i] = (rand() % 9) +1;
for (j = 0 ; j < 10 ; j++)
{
if (r[i] == a[j]) // I am pretty sure this is wrong!
{
/*
* Second part of array a[][] is flagged to 1
* how do I access just [][THIS_PART!] :-(
*/
}
}
printf ("%d", r[i]);
}
printf ("\n");

return SUCCESS;
}

int main (void)
{

if (random_number())
{
printf ("Error calling function random_number\n");

return ERROR;
}

return SUCCESS;
}

Cheers

Jim
0
Question by:Jim2000

Author Comment

Damn it netscape messed up my formatting :-(!!! Not my day!!
0

LVL 1

Expert Comment

you made an array [10][2] but you need only [10]

example array[4][2] is a matrix like
let say you put in each place 0

r\c  0)    1)
0)   0     0
1)   0     0
2)   0     0
3)   0     0

[row index][col index]

for example for matrix:array[4][2]

r\c  0)    1)
0)   1     2
1)   3     4
2)   5     6
3)   7     8

array[0][0]=1;
array[1][1]=4;
array[2][0]=5;
array[3][1]=8;

now your code change it to:

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

#define SUCCESS 0
#define ERROR 1

int random_number (void)
{
int i, j, k, r[4];
int a[] = {0,0,0,0,0,0,0,0,0,0};

srand(time(0));
for (i = 0 ; i < 4 ; i++)
{
r[i] = (rand() % 9) +1;
if ((r[i]>-1)&&(r[i]<10))
{
if (a[r[i]]=0)
a[r[i]]=1;
else return ERROR;
}
printf ("%d", r[i]);
}
printf ("\n");

return SUCCESS;
}

int main (void)
{

if (random_number())
{
printf ("Error calling function random_number\n");

return ERROR;
}

return SUCCESS;
}

i hope i understand right what you wanted to do if not explain it to me again and i"ll rewrite the code

if you still want to use a multidim array

like you did:
int a[10][1] = {{0, 0}, {1, 0},
{2, 0}, {3, 0},
{4, 0}, {5, 0},
{6, 0}, {7, 0},
{8, 0}, {9, 0}};

first it should be:
int a[10][2] = {{0, 0}, {1, 0},
{2, 0}, {3, 0},
{4, 0}, {5, 0},
{6, 0}, {7, 0},
{8, 0}, {9, 0}};

then a[x][0] are the numbers 0,1,2,..,9
and a[x][1] are 0 or 1

r/c  0)     1)
0)   0      0
1)   1      0
2)   2      0
3)   3      0
4)   4      0
5)   5      0
6)   6      0
7)   7      0
8)   8      0
9)   9      0

them the if will be
if (a[i][0]==r[j])

and in the loop you change a[i][1]

but my prog much faster so check it

explain you problam again if i mis understood
0

Author Comment

Good reply ntdragon. The only reason I am rejecting this answer is because maybe I didn't explain this q
0

Author Comment

Good reply ntdragon. The only reason I am rejecting this answer is because maybe I didn't explain this q properly. I have changed my code slightly cos I realised that I didn't need a multi dim array (thanx) yet when I write what I think should work - it doesn't! Can you tell me why! Again what I am trying to do is:

generate a 4 digit random number
have each number checked indiviually against an array of 10 elements (0..9) and mark a variable as either 0 or 1 to define whether it has been used. 0 is free 1 has been used. If the number generated has already been used use one that hasn't.

Here is my code again:

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

#define SUCCESS 0
#define ERROR 1

int random_number (void)
{
int i, j, k, r[4];
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

srand((unsigned)time(0));
for (i = 0 ; i < 4 ; i++)
{
k = 0;
r[i] = (rand() % 9) +1;
for (j = 0 ; j < 10 ; j++)
{
if (r[i] == a[j])
{
k[j] = 1;
}
}
printf ("%d", r[i]);
}
printf ("\n");

return SUCCESS;
}

int main (void)
{

if (random_number())
{
printf ("Error calling function random_number\n");

return ERROR;
}

return SUCCESS;
}
0

LVL 1

Expert Comment

1)you try to use k as an array you k[j]
but k isn't an array in your code it just a singel int

second i don't see in you code the check if the number is all ready marked
you said that you want to mark numbers 0 isn't marked and 1 is marked but i don't see where you check it

i don't understand what do you want to do exectlly if the number that you're checking is marked <1> what do you do then???
0

Expert Comment

Here this should work better i aproched it a bit diffrent firstly i used random to search for a new nuber again if the on that was assigned is already in use. THis gives an even better random patern and can be used with up to 10 digits.

Hope it help.
=================================================
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define SUCCESS   0
#define ERROR     1

int random_number (void)
{
int i, r[4];
bool UsedNums[10] = {false, false, false, false, false,
false, false, false, false, false};
bool done = false;

srand((unsigned)time(0));
for (i = 0 ; i < 4 ; i++)
{
while(!done)
{
r[i] = (rand() % 9);    //Asign Random number
if ( UsedNums[r[i]] ) //checks if the number has been used
{//yes it is so reasign a new random number
r[i] = (rand() % 9);
}
else
{//no it hasn't so mark it as used
UsedNums[r[i]] = true;
done = true;//found a number so exit the while loop
}
}
done = false;//reset the while loop
printf ("%d", r[i]);
}
printf ("\n");
return SUCCESS;
}

int main (void)
{
if (random_number())
{
printf ("Error calling function random_number\n");
return ERROR;
}
getch();
return SUCCESS;
}
===============================================
0

Accepted Solution

sylphe earned 200 total points
Hi all,

I think that there is a shorter/faster way to do it, without UsedNums.
The main idea is to generate a random digit and to keep it if it was not previously generated.

/*
*      Different random digits
*/

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

void random_number(void);

void main(void)
{
random_number();
}

void random_number(void)
{
int i,j,verif;
int r[4];

srand((unsigned)time(0));

for(i=0;i<4;i++)
{
int tmp;

do            /* Till the digit is a new one */
{
verif=0;
tmp=rand()%9;

for(j=0;j<i;j++)      /* Did we see it before ? */
{
if(r[j]==tmp)      /* Yes */
{
verif++;
break;
}
}
}
while(verif);

r[i]=tmp;

fprintf(stderr,"%d\n",r[i]);/*Trace*/
}
}
0

Author Comment

Cheers for all ya help guys! Can I not split the points up? I really want to accept sylphe answer cos it is easy to understand and worked well and quick. Alien's seemed to work buu when compiling i got loadsa boolean errors - I've never had much luck with bools? Why doesn't gcc like them?
0

## Featured Post

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.