DR. D. Y.
PATIL SCHOOL OF SCIENCE & TECHNOLOGY
DR. D. Y. PATIL VIDYAPEETH, PUNE
(Deemed to be University)
(Accredited (3rd cycle) by NAAC with a CGPA of 3.64 on four-point scale at ‘A++’ Grade)
(Declared as Category - I University by UGC Under Graded Autonomy Regulations, 2018)
(An ISO 9001: 2015 and 14001:2015 Certified University and Green Education Campus)
Date: 29/01/2024
Assignment No: 3
Problem Statement: Design a simple quiz application that asks multiple-choice questions.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_QUESTIONS 5
typedef struct {
char question[256];
char options[4][64];
int correct_option;
} Question;
void displayQuestion(Question q)
printf("%s\n", q.question);
for (int i = 0; i < 4; i++)
printf("%d. %s\n", i + 1, q.options[i]);
}
int checkAnswer(Question q, int user_answer)
return (user_answer == q.correct_option);
int main()
srand(time(NULL));
Question original_questions[MAX_QUESTIONS] =
{ "Which bird lays the largest egg?",
{ "Owl", "Ostrich", "Kingfisher", "Woodpecker" },
2 },
{ "How many legs does a spider have?",
{ "7", "8", "6", "5" },
2 },
{ "Where does the President of the United States "
"live while in office?",
{ "The White House", "The Parliament",
"House of Commons", "Washington DC" },
1 },
{ "Which state is famous for Hollywood?",
{ "Sydney", "California", "New York", "Paris" },
2 },
{ "What is a group of lions called?",
{ "Drift", "Pride", "Flock", "Drove" },
2}
};
Question questions[MAX_QUESTIONS];
memcpy(questions, original_questions,
sizeof(original_questions));
int num_questions = MAX_QUESTIONS;
int score = 0;
printf("Hola! Here's your Quiz Game!\n");
for (int i = 0; i < MAX_QUESTIONS; i++)
int random_index = rand() % num_questions;
Question current_question = questions[random_index];
displayQuestion(current_question);
int user_answer;
printf("Enter your answer (1-4): ");
scanf("%d", &user_answer);
if (user_answer >= 1 && user_answer <= 4)
if (checkAnswer(current_question,
user_answer))
printf("Correct!\n");
score++;
else
printf("Incorrect. The correct answer is "
"%d. %s\n",
current_question.correct_option,
current_question.options
[current_question.correct_option
- 1]);
else
printf("Invalid choice. Please enter a number "
"between 1 and 4.\n");
questions[random_index]
= questions[num_questions - 1];
num_questions--;
printf("Well Done Champ !!!! Quiz completed! Your "
"score: %d/%d\n",
score, MAX_QUESTIONS);
return 0;
}
Sample Output:
Hola! Here's your Quiz Game!
What is a group of lions called?
1. Drift
2. Pride
3. Flock
4. Drove
Enter your answer (1-4): 2
Correct!
Which state is famous for Hollywood?
1. Sydney
2. California
3. New York
4. Paris
Enter your answer (1-4): 2
Correct!
Which bird lays the largest egg?
1. Owl
2. Ostrich
3. Kingfisher
4. Woodpecker
Enter your answer (1-4): 2
Correct!
How many legs does a spider have?
1. 7
2. 8
3. 6
4. 5
Enter your answer (1-4): 2
Correct!
Where does the President of the United States live while in office?
1. The White House
2. The Parliament
3. House of Commons
4. Washington DC
Enter your answer (1-4): 1
Correct!
Well Done Champ !!!! Quiz completed! Your score: 5/5