Page 1 of 212> Show 40 post(s) from this thread on one page

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   New program..need help with random (http://www.go4expert.com/forums/programneed-help-random-t21396/)

New program..need help with random

I need to make a program that contains 9 flashcards (question at one side and its answer overleaf). The program randomly selects between the 9 different flashcards then it prints any side of the flashcard (either the question or the answer side).
C Language

 xpi0t0s 18Mar2010 14:21

Re: New program..need help with random

OK, it's not difficult. How far have you got and where are you stuck? Do you understand the assignment?
Have you worked out how to represent the flash cards? (for example would a simple array of strings be sufficient?)
Do you know how to get a random number?
How would you determine whether to display question or answer: is that also random?
Having worked out how to represent the flash cards, can you think of a way to combine that with the answers to the last two questions?

If the answer to all those is yes then you should have enough to start coding.

Re: New program..need help with random

Here is the full description of the assignment:

• The flashcards will be group into boxes A, B and C according to the flow as follows.
1. From the selection screen, user chooses to start
2. The program randomly shows a flashcard (either side, randomly) and the user guesses the other side (no input required)
3. After guessing, the user selects to show the other side
4. If correct, the user promotes the flashcard to box A. If incorrect, the user promotes the flashcard to box C. If the user is unsure or almost close to the correct one, the user promotes the flashcard to box B. The promotion is based on honest user input
5. After the user goes through all the flashcards, the program gives the user's score and stores it with its corresponding date and time.
6. From the selection screen, the user gets an option to redo the flashcards in box B and C as done in item 2 to 4
7. The score gets recomputed and then displayed with previous score(s) after a single process in item 6 has been completed.
• Boxes A, B and C are initially empty with the option that the user could empty them at the selection if they contain flashcards
• Put a selection screen to show the stored scores and their corresponding date and time
1. Score = number of flashcards in box A / total number of flashcards
• A selection screen option for saving and loading the following:
1. Scores (1 text file)
2. Content of each box (3 text files)
• The flashcards are biblical passages (reference at one side and passage content at the other)

Here is my starting random program:
Code:

```#include<stdio.h>  #include <conio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <time.h> int getNextNum(){   static int list[9];   static int size = -1;   if (size==-1){     size=9;     for(int i=0;i<size;i++){       list[i]=i+1;       }     }   if(size==0){     return 0;     }   int index=rand()%size;   int num=list[index];   size--;   list[index] = list[size];   return num; } int main(){   const time_t timer=time(NULL);   printf("%s\n",ctime(&timer));     srand( time(NULL));   int z;   for(int i=0;i<9;i++){     z=getNextNum();     printf("Random Number: %d\n",z);     }   getche(); }```

 xpi0t0s 18Mar2010 20:02

Re: New program..need help with random

Seems pretty detailed. Where are you stuck? Or are you just hoping someone will write it for you?

Re: New program..need help with random

No! I'm stuck in the second random part, the random inside the random

 xpi0t0s 18Mar2010 20:47

Re: New program..need help with random

So if getNextNum() selects a card, then you need a second random number to select which side to show.

Re: New program..need help with random

Yup!:) what I am thinking is to make another function that has an array which has a size equal to two but the problem is the printing part for the sides

 xpi0t0s 18Mar2010 21:00

Re: New program..need help with random

There isn't any code that displays any part of the slides. Have a go at writing it and see where you get stuck. If you can't figure out how to display a random side, just display the same side each time and I'll see if I can fit a "rand()%2" in there somewhere.

I don't see how an array of size 2 helps though. Why would you need to store two numbers?

Re: New program..need help with random

Sorry sir but I just made it complicated. Here is my problem now, how will I print the different passages without repeated because when I add a conditional statement inside the for loop the numbers are being repeated until all numbers are used.

Here is my updated code:

Code:

```#include<stdio.h>  #include <conio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <time.h> int getNextNum(){   static int list[9];   static int size=-1;   if (size==-1){     size=9;     for(int i=0;i<size;i++){       list[i]=i+1;       }     }   if(size==0){     return 0;     }   int index=rand()%size;   int num=list[index];   size--;   list[index] = list[size];   return num; } int main(){   const time_t timer=time(NULL);   printf("%s\n",ctime(&timer));     srand( time(NULL));   int z;   for(int i=0;i<9;i++){     z=getNextNum();     switch(z){       case 1:         printf("Passage Number: %d\n",z);         getche();       case 2:         printf("Passage Number: %d\n",z);         getche();       case 3:         printf("Passage Number: %d\n",z);         getche();       case 4:         printf("Passage Number: %d\n",z);         getche();       case 5:         printf("Passage Number: %d\n",z);         getche();       case 6:         printf("Passage Number: %d\n",z);         getche();       case 7:         printf("Passage Number: %d\n",z);         getche();       case 8:         printf("Passage Number: %d\n",z);         getche();       case 9:         printf("Passage Number: %d\n",z);         getche();       }     }   getche(); }```

 xpi0t0s 19Mar2010 20:38

Re: New program..need help with random

Case blocks must be terminated with break otherwise it will continue to the next:
Code:

```int x=1; switch(x) { case 1: printf("x is 1\n"); case 2: printf("x is 2\n"); }```
will print both x is 1 *and* x is 2.

I still don't see where you're deciding which side of the card to show (perhaps you haven't attempted to write that code yet). I also don't get what GetNextNum is trying to do; it seems unnecessarily complicated.

A much simpler algorithm for GetNextNumber is to forget static variables and initialise the array in a separate routine which you call once at the start of main. Populate the array with numbers 0 to size-1 then for each entry in the array swap it with a randomly selected other entry in the array.

When you want to display the card you decide which side to show; this doesn't need to be stored anywhere if I understand it correctly, so just pick a random number below 2, check if it's 0 or 1 and display the relevant side of the card. Or do you need to store the side random number, and if so why?

 All times are GMT +5.5. The time now is 12:25. Page 1 of 212> Show 40 post(s) from this thread on one page