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
|
Mentor
|
![]() |
| 18Mar2010,14:21 | #2 |
|
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. |
|
Go4Expert Member
|
|
| 18Mar2010,19:16 | #3 |
|
Here is the full description of the assignment:
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();
}
Last edited by askmewhy25; 18Mar2010 at 20:58.. |
|
Mentor
|
![]() |
| 18Mar2010,20:02 | #4 |
|
Seems pretty detailed. Where are you stuck? Or are you just hoping someone will write it for you?
|
|
Go4Expert Member
|
|
| 18Mar2010,20:42 | #5 |
|
No! I'm stuck in the second random part, the random inside the random
|
|
Mentor
|
![]() |
| 18Mar2010,20:47 | #6 |
|
So if getNextNum() selects a card, then you need a second random number to select which side to show.
|
|
Go4Expert Member
|
|
| 18Mar2010,20:49 | #7 |
|
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
Last edited by askmewhy25; 18Mar2010 at 20:52.. |
|
Mentor
|
![]() |
| 18Mar2010,21:00 | #8 |
|
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? |
|
Go4Expert Member
|
|
| 19Mar2010,20:30 | #9 |
|
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();
}
|
|
Mentor
|
![]() |
| 19Mar2010,20:38 | #10 |
|
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");
}
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? |


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