Solved

# Assigning a new number if it has already been used.

Posted on 2000-04-10
187 Views
Ok well my code is nearly there. I have done this a number of different ways but I really want figure out how to accomplish it this way (a sort of personal goal thing). So what I want to do is assign a new number to the current point in the 4 element integer array if the number has already been used - see the code below. It can tell if the number has already been used because it has been "flagged" either 0 or 1 accordingly. I am sure you experts out there can give us a hint. I am sure I wanna put something in the else statement that I have left blank but I have tried a few things that didn't work and can't think of any other way of doing it!

Code:

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

#define SUCCESS 0
#define ERROR 1

int GenerateRandomNumber (void)
{
int i, j, s;
int t, r[4];
int c[10] = {0, 0, 0, 0, 0,
0, 0, 0, 0, 0};

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

return SUCCESS;
}

int main (void)
{

if (GenerateRandomNumber())
{
fprintf (stderr, "Error calling function RandomNumer\n");

return ERROR;
}
printf ("\n");

return SUCCESS;
}

Regards

Anewbis_
0
Question by:Anewbis_
• 2
• 2

LVL 1

Expert Comment

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

#define SUCCESS 0
#define ERROR 1

int GenerateRandomNumber (void)
{
int i, j, s;
int t, r[4];
int c[10] = {0, 0, 0, 0, 0,
0, 0, 0, 0, 0};

srand ((unsigned) time(0));
for (i = 0 ; i < 4 ; i++)
{
t = rand() %9;
c[t] = 1;
s = 0;
for (j = 0 ; j < 10 ; j ++)
{
if (c[j] == 1)
{
//r[i] = t;
r[s] = t; // because i will always be the same in this loop
s++;
if (s == 3) // not 4, 0 - 3
{
break;
}
else // don't need else
{
}
}
}
printf ("%d", r[i]);
}
printf ("\n%d", s);

return SUCCESS;
}

int main (void)
{

if (GenerateRandomNumber())
{
fprintf (stderr, "Error calling function RandomNumer\n");

return ERROR;
}
printf ("\n");

return SUCCESS;
}
0

Author Comment

Er!! That doesn't work! Do you understand what I am trying to do?
0

Accepted Solution

Dannemand earned 75 total points
Lemme guess: You're trying to create four different numbers?

If you want to keep on creating a new number until you have four different ones, you really want to use a while {..} loop

e.g.
for (j=1 to 4)
{
flag=0;
while (!flag)
{
t=random number;
if (!c[t])
{
c[t]=1;
flag=1;
}
}
}

Is that the sort of thing you're looking for?
0

Author Comment

Yeah I'd kinda done it that way before, but the flagging thing worked well I just wanted to do it by flagging the 0's to 1.

Thanx
0

Expert Comment

Can I also take the liberty of pointing out that rand%9 will give you a value from 0 to 8, so c[9] is not going to be referenced?

Incidentally

do
{
t=random number%9; /* 10? */
}
while (c[t]!=0);
c[t]=1;

will also do the trick.

Thanks anyway.
0

## Join & Write a Comment Already a member? Login.

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

#### 763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!