Solved

Assigning a new number if it has already been used.

Posted on 2000-04-10
5
230 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 75 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

623 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