0

Please help me finish this project. I am having trouble coming up with the code to ensure the numbers do not repeat. i made another post the other day, but this is what i have come up with since then. the output puts out the user entered amount of sets, but the numbers still repeat.

my instructor commented the code but i am still lost as to how to finish the code.

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

#define OFFSET 1
#define RANGE 53


void Flush(void); 

int GetRandomInRange(int offset, int range){
  int num = (rand() % (range - offset + 1)) + offset;

  return num;
}

void PrintRandomSet(int a, int b, int c, int d, int e, int f){

  int i;

      printf("%2d %2d %2d %2d %2d %2d\n" , a, b, c, d, e, f);//minimum width makes it align

  //}

  return;

}

int PrintInstructions(void){



     int numSets = 0;

     int success = 0;

     char c;

     for(;;)

     {

     printf("******************************************************************\n");

           printf("Enter the amount of sets of 6 random numbers you want to generate.\n");

           printf("Enter in 'q' or 'Q' to quit...\n");

     printf("******************************************************************\n");

           success = scanf(" %d" , &numSets);

           if(success == 0)

           {

                scanf(" %c", &c);

                if(tolower(c) == 'q')

                     return -1;

           }

           else

                return numSets;

     }//end for

} 



int main(void) {

  int a;

  int b;

  int c;

  int d;

  int e;

  int f;

  int i;

  srand(time(NULL));

  int numSets = 3;



  do{

    //PrintInstructions();

    numSets = PrintInstructions();

     if(numSets > 0)

     {

                for(i=0; i<numSets; i++)

           {//you need a block of code here for compound statements

                a = GetRandomInRange(OFFSET, RANGE);

                b = GetRandomInRange(OFFSET, RANGE);

                c = GetRandomInRange(OFFSET, RANGE);

                d = GetRandomInRange(OFFSET, RANGE);

                e = GetRandomInRange(OFFSET, RANGE);

                f = GetRandomInRange(OFFSET, RANGE);

                PrintRandomSet(a, b, c, d, e, f);

           }//end for

     }//end if

    }while(numSets != -1);//Yuor do while syntax was wrong.

    printf("You have chosen to quit");



  return 0;

}

void Flush()

{

     char c;

     while((c = getchar()) != '\n' && c != EOF);

     return;

}
Brad Ruiz
  • 23
  • 4
  • 1
    Have you searched for similar posts? Such as [How to create a non-repeating random number?](https://stackoverflow.com/questions/26959460/how-to-create-a-non-repeating-random-number) – kaylum Apr 02 '20 at 19:34
  • this has to be done without using arrays.... – Brad Ruiz Apr 02 '20 at 20:08
  • You can still use the info in the answer. Apply the same principles. For example, write a function that accepts 6 numbers. The first time the function is called all 6 are set to an invalid number. The function then loops generating a random number until it gets one that is not the same as any of 6 numbers. Now set that new number as one of the 6 and call the function again. Repeat until all 6 numbers are set. – kaylum Apr 02 '20 at 20:13

0 Answers0