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.

149 lines
5.2 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "timequiz.h"
void timequiz();
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions);
void displayQuestion(char* question, char* answers[], int correctIndex);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]);
void timequiz() {
printf("Welcome to our Time Quiz!\n");
printf("You have 60 seconds to answer the questions. Have fun!\n");
char* questions[] = {
"What is the capital of France?",
"Who developed the theory of relativity?",
"Who was the first President of the United States?",
"What is the chemical symbol for water?",
"Who wrote 'To Kill a Mockingbird'?",
"What is the largest planet in our solar system?",
"Who painted the Mona Lisa?",
"What is the tallest mountain in the world?",
"Which country is known as the Land of the Rising Sun?",
"Who was the first person to walk on the moon?",
"What year did World War I begin?",
"What is the currency of Japan?",
"Who is known as the 'Father of Computers'?",
"What is the capital of Canada?",
"Who discovered penicillin?",
"What is the chemical formula for table salt?",
"Which is the largest ocean on Earth?",
"Who is the author of '1984'?",
"What is the main ingredient in guacamole?",
"Who was the Greek god of the sea?",
"What is the square root of 144?"
};
char* answers[][4] = {
{"Paris", "London", "Berlin", "Madrid"},
{"Albert Einstein", "Isaac Newton", "Galileo Galilei", "Stephen Hawking"},
{"George Washington", "Thomas Jefferson", "John Adams", "James Madison"},
{"H2O", "CO2", "NaCl", "C6H12O6"},
{"Harper Lee", "J.K. Rowling", "Ernest Hemingway", "George Orwell"},
{"Jupiter", "Saturn", "Mars", "Neptune"},
{"Leonardo da Vinci", "Michelangelo", "Pablo Picasso", "Vincent van Gogh"},
{"Mount Everest", "K2", "Kangchenjunga", "Lhotse"},
{"Japan", "China", "South Korea", "India"},
{"Neil Armstrong", "Buzz Aldrin", "Yuri Gagarin", "Alan Shepard"},
{"1914", "1918", "1923", "1939"},
{"Yen", "Euro", "Dollar", "Pound"},
{"Charles Babbage", "Alan Turing", "Ada Lovelace", "Bill Gates"},
{"Ottawa", "Toronto", "Vancouver", "Montreal"},
{"Alexander Fleming", "Louis Pasteur", "Marie Curie", "Albert Einstein"},
{"NaCl", "H2O", "CO2", "HCl"},
{"Pacific Ocean", "Atlantic Ocean", "Indian Ocean", "Arctic Ocean"},
{"George Orwell", "F. Scott Fitzgerald", "Aldous Huxley", "J.D. Salinger"},
{"Avocado", "Tomato", "Onion", "Lime"},
{"Poseidon", "Zeus", "Hades", "Apollo"},
{"12", "10", "16", "14"}
};
int correctAnswers[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++) {
int j = rand() % 4;
char* temp = answers[i][0];
answers[i][0] = answers[i][j];
answers[i][j] = temp;
if (j == 0) {
correctAnswers[i] = 1;
}
else if (j == 1) {
correctAnswers[i] = 2;
}
else if (j == 2) {
correctAnswers[i] = 3;
}
else {
correctAnswers[i] = 4;
}
}
int score = 0;
int totalAnsweredQuestions = 0;
int totalCorrectAnswers = 0;
time_t startTime = time(NULL);
time_t currentTime;
int elapsedTime = 0;
int totalQuestions = sizeof(questions) / sizeof(questions[0]);
int askedQuestions[40];
memset(askedQuestions, 0, sizeof(askedQuestions));
srand((unsigned int)time(NULL));
while (elapsedTime < 60 && totalAnsweredQuestions < totalQuestions) {
int questionIndex = getRandomQuestionIndex(askedQuestions, totalQuestions);
askedQuestions[questionIndex] = 1;
int correctIndex = correctAnswers[questionIndex] - 1;
displayQuestion(questions[questionIndex], answers[questionIndex], correctIndex);
int userAnswer;
printf("Answer (1-4): ");
scanf_s("%d", &userAnswer);
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers[questionIndex]);
totalAnsweredQuestions++;
currentTime = time(NULL);
elapsedTime = (int)difftime(currentTime, startTime);
}
printf("\nQuiz finished!\n");
printf("Your total score: %d out of %d\n", totalCorrectAnswers, totalAnsweredQuestions);
}
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions) {
int questionIndex;
do {
questionIndex = rand() % totalQuestions;
} while (askedQuestions[questionIndex] == 1);
return questionIndex;
}
void displayQuestion(char* question, char* answers[], int correctIndex) {
printf("\nQuestion: %s\n", question);
for (int i = 0; i < 4; i++) {
printf("%d. %s\n", i + 1, answers[i]);
}
}
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]) {
if (userAnswer == correctIndex + 1) {
printf("Correct!\n");
(*score)++;
(*totalCorrectAnswers)++;
}
else {
printf("Wrong! The correct answer is: %d. %s\n", correctIndex + 1, answers[correctIndex]);
}
}