I need help with this program. I need it in C language. I also have the shuffle
ID: 3908654 • Letter: I
Question
I need help with this program. I need it in C language. I also have the shuffle code. I will provide it. Thank you
#include <stdio.h>
int main() {
srand(time(NULL));
unsigned char data2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int i, temp, ind;
for(i = 0; i < sizeof(data2); ++i) {
ind = rand() % sizeof(data2);
temp = data2[ind];
data2[ind] = data2[i];
data2[i] = temp;
}
for(i = 0; i < sizeof(data2); ++i) {
printf("%d ", data2[i]);
}
printf(" ");
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct _question {
char question[100];
char choice1[100];
char choice2[100];
char choice3[100];
char choice4[100];
char correctAnswer[4];
};
int main(){
int i;
struct _question data[50];
srand(time(NULL));
FILE *fp = fopen("questionbank.txt", "r");
if (fp == NULL){
printf("Error in opening file ");
return;
}
int score = 0;
for (i = 0; i<50; i++){
fgets(data[i].question,100, fp);
fgets(data[i].choice1,100, fp);
fgets(data[i].choice2,100, fp);
fgets(data[i].choice3,100, fp);
fgets(data[i].choice4,100, fp);
fgets(data[i].correctAnswer,100, fp);
}
for (i = 0; i<10; i++){
int n = rand() % 50;
printf("%s ",data[n].question);
printf("%s ",data[n].choice1);
printf("%s ",data[n].choice2);
printf("%s ",data[n].choice3);
printf("%s ",data[n].choice4);
printf("Enter your answer:");
char ans[4];
scanf("%s",ans);
if (strcmp(ans,data[i].correctAnswer)== 0){
score++;
printf("%s ","Good Job!");
}
else {
printf("Sorry ");
}
}
return 0;
}