You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.5 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include "timequiz.h"
  6. void timequiz();
  7. int getRandomQuestionIndex(int askedQuestions[], int totalQuestions);
  8. void displayQuestion(char* question, char* answers[], int correctIndex);
  9. void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]);
  10. void timequiz() {
  11. printf("Welcome to our Time Quiz!\n");
  12. printf("You have 60 seconds to answer the questions. Have fun!\n");
  13. char* questions[] = {
  14. "What is the capital of France?",
  15. };
  16. char* answers[][4] = {
  17. {"Paris", "London", "Berlin", "Madrid"},
  18. };
  19. int correctAnswers[] = { 1 };
  20. for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++) {
  21. int j = rand() % 4;
  22. char* temp = answers[i][0];
  23. answers[i][0] = answers[i][j];
  24. answers[i][j] = temp;
  25. if (j == 0) {
  26. correctAnswers[i] = 1;
  27. }
  28. else if (j == 1) {
  29. correctAnswers[i] = 2;
  30. }
  31. else if (j == 2) {
  32. correctAnswers[i] = 3;
  33. }
  34. else {
  35. correctAnswers[i] = 4;
  36. }
  37. }
  38. int score = 0;
  39. int totalAnsweredQuestions = 0;
  40. int totalCorrectAnswers = 0;
  41. time_t startTime = time(NULL);
  42. time_t currentTime;
  43. int elapsedTime = 0;
  44. int totalQuestions = sizeof(questions) / sizeof(questions[0]);
  45. int askedQuestions[40];
  46. memset(askedQuestions, 0, sizeof(askedQuestions));
  47. srand((unsigned int)time(NULL));
  48. while (elapsedTime < 60 && totalAnsweredQuestions < totalQuestions) {
  49. int questionIndex = getRandomQuestionIndex(askedQuestions, totalQuestions);
  50. askedQuestions[questionIndex] = 1;
  51. int correctIndex = correctAnswers[questionIndex] - 1;
  52. displayQuestion(questions[questionIndex], answers[questionIndex], correctIndex);
  53. int userAnswer;
  54. printf("Answer (1-4): ");
  55. scanf_s("%d", &userAnswer);
  56. }
  57. }
  58. int getRandomQuestionIndex(int askedQuestions[], int totalQuestions) {
  59. int questionIndex;
  60. do {
  61. questionIndex = rand() % totalQuestions;
  62. } while (askedQuestions[questionIndex] == 1);
  63. return questionIndex;
  64. }
  65. void displayQuestion(char* question, char* answers[], int correctIndex) {
  66. printf("\nQuestion: %s\n", question);
  67. for (int i = 0; i < 4; i++) {
  68. printf("%d. %s\n", i + 1, answers[i]);
  69. }
  70. }
  71. void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]) {
  72. }