Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Assigning a new number if it has already been used.

Posted on 2000-04-10
5
Medium Priority
?
238 Views
Last Modified: 2010-04-15
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
Comment
Question by:Anewbis_
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:bcoleman
ID: 2702335
#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

by:Anewbis_
ID: 2702461
Er!! That doesn't work! Do you understand what I am trying to do?
0
 

Accepted Solution

by:
Dannemand earned 150 total points
ID: 2703621
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

by:Anewbis_
ID: 2704039
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

by:Dannemand
ID: 2704459
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

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

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

Join & Ask a Question