Thread: randomizing

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    randomizing

    hi i wrote a trivia program but i want to improve it by randomizing the questions in it. but how do i do it? thanks

    Code:
    main()
    {
    	int l;
    	clrscr();
    	gotoxy(30,7);printf("[1] QUESTIONS\n");
    	gotoxy(30,9);printf("[2] QUIT\n\n");
    	gotoxy(30,13);printf("Choose:  ");
    	scanf("%d",&l);
    	if(l==1)
    	easy();
    	else if(l==2)
    	exit();
    	}
    easy()
    {
    	int x;
    	char d[5];
    	int i=0;
    	char a[]="a";
    	char b[]="b";
    	char c[]="c";
    	for(x=0;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,5);printf("Direction: Choose the letter of the correct answer.");
    	gotoxy(10,7);printf("1. What two letters are both symbols for 1,000?");
    	gotoxy(10,9);printf("%s. K and M.",a);
    	gotoxy(10,11);printf("%s. A and B.",b);
    	gotoxy(10,13);printf("%s. L and D",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,a)==0)
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	if(strcmp(d,a)!=0)
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("2. What's short for Binary Digit?");
    	gotoxy(10,9);printf("%s. BD.",a);
    	gotoxy(10,11);printf("%s. Bit.",b);
    	gotoxy(10,13);printf("%s. Digit.",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,b)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("3. What digit did Arab mathematician al-Khwarizmi give to the West\n            around 800 B/B.?");
    	gotoxy(10,10);printf("%s. Zero",a);
    	gotoxy(10,12);printf("%s. One",b);
    	gotoxy(10,14);printf("%s. Two",c);
    	gotoxy(10,16);printf("Answer: ");gets(d);
    	if(strcmp(d,a)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("4. What word describes a number system with a base of two?");
    	gotoxy(10,9);printf("%s. Decimal",a);
    	gotoxy(10,11);printf("%s. Octal",b);
    	gotoxy(10,13);printf("%s. Binary",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,c)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    	for(x=1;x<=3;x++)
    	{
    	clrscr();
    	gotoxy(10,7);printf("5. How many equal sides does an icosahedron have?");
    	gotoxy(10,9);printf("%s. Ten",a);
    	gotoxy(10,11);printf("%s. Twenty",b);
    	gotoxy(10,13);printf("%s. Thirty",c);
    	gotoxy(10,15);printf("Answer: ");gets(d);
    	if(strcmp(d,b)==0)
    
    	{
    	gotoxy(12,20);printf("Correct!!!\n");
    	i=i+1;
    	gotoxy(12,21);printf("Your score is %d",i);
    	x=3;
    	getch();
    	}
    	else
    	{
    	gotoxy(12,20);printf("Wrong!!!\n");
    	gotoxy(12,21);printf("Life[%d]",x);
    	getch();
    	if(x>=3)
    	{
    	main();
    	}
    	}
    	}
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the function prototype should include return type and parameters type:
    Code:
    int main(void)
    and do not call main from your code


    you may want to look at rand function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    What Vart said

    +

    Learn to indent please.

    Randomizing is fairly easy.

    What you want to do is import the time library.

    Code:
    #include <time.h>
    then randomize a seed based on the time.

    Code:
     srand(time(NULL));
    This is the seed that will be used in the rand function, but since it changes with time, your program will see more realistically random.

    The rand function can be called by:

    Code:
     rand () &#37; X
    Where X is the upper limit on the number. So if you had 10, it would generate between 0 and 10, there are different ways to use rand so as to manipulate the output better, look it up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Randomizing dealing program C
    By BSmith4740 in forum C Programming
    Replies: 2
    Last Post: 08-04-2008, 01:42 PM
  2. Randomizing link list.
    By Axel in forum C Programming
    Replies: 4
    Last Post: 10-24-2005, 10:03 AM
  3. srand() not randomizing me?!
    By crummy in forum C Programming
    Replies: 3
    Last Post: 02-11-2005, 07:23 PM
  4. randomizing?
    By cerin in forum C++ Programming
    Replies: 55
    Last Post: 02-03-2005, 06:58 PM
  5. Randomizing
    By Morph in forum C++ Programming
    Replies: 18
    Last Post: 01-26-2003, 06:36 PM