Thread: help needed on loop ??

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    help needed on loop ??

    Hi all!

    Newbie question,
    Im currently working on an assignment for college, part of my prog needs to generate a random quantity <4 , of random numbers <7 if that makes sense.

    but if the random generator outputs the same number twice it should restart until it gives me a series of different numbers.
    Quite tricky to explain but the part im stuck on is when it generates 2 numbers the same how do i tell it to restart?

    I.E if(count>0)
    restart loop

    Is there a redo command in c?
    theres hopefully a simple solution but i cant find anything in my textbooks.

    Heres my code:
    Code:
    
    
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <time.h>  
    
    int main(void)
    {
      int i,x,y,count=0,p,array[3],array2[5];
    
      srand(time(NULL));
     
      x = rand()%4+1;    /*  generate number of choices*/
      printf("%d",x);
      printf("\n\n");
      
     
     
      for(i=0;i<x;i++)
      array[i]=rand()%7;     /*create array of number of choices & fill with menu selections*/
        
      for(i=0;i<x;i++)
      {
                       
                       
                      for(p=i+1;p<x;p++)
                      {
                      if(array[p]==array[i])
                      count++;
                      }
                      if(count>0)     /* if count >0 redo*/
                      
    
      
      
      for(i=0;i<x;i++)
      printf("%d\n",array[i]);
      printf("\n");
      
      
    
    
    printf("\n count=%d \n \n",count);
       
    system("pause");
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You can simplify your code by make a function to check to see if an number generated is already in the array. Then use a do-while to loop while there is a duplicate.
    Code:
      for(i=0;i<x;i++)
      {
         do {
            num=rand()%7;     /*create array of number of choices & fill with menu selections*/
         } while (same(num, array, i+1));
         array[i] = num;
      }
    I called function to check for a duplicate same().

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    cheers swoopy, that seems to have solved my problem.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    By the way, a prototype for same() would look something like:
    Code:
    same(int num, int array[], int size)
    /* Check to see if num is already in the array. */
    You might find a better name like in_array() or is_in_array().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM