Solved

Assigning a new number if it has already been used.

Posted on 2000-04-10
5
202 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 28
Why adding test code changes the build ? 11 130
C simple state machine for client server handshake 4 270
C Programming - If Statement 8 72
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now