0% found this document useful (0 votes)
16 views10 pages

Quiz Competition Score Calculation

The document outlines a program for a quiz competition with specific requirements for participant input and scoring. It details the structure of the program, including input validation, answer storage, score calculation, and output of results. The program must handle between 4 to 10 participants, display their scores, and identify those with the highest score.

Uploaded by

Binit Karmakar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

Quiz Competition Score Calculation

The document outlines a program for a quiz competition with specific requirements for participant input and scoring. It details the structure of the program, including input validation, answer storage, score calculation, and output of results. The program must handle between 4 to 10 participants, display their scores, and identify those with the highest score.

Uploaded by

Binit Karmakar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question :

The result of a quiz competition is to be prepared as


follows:

The quiz has five questions with four multiple choices (A,
B, C, D), with each question carrying 1 mark for the correct
answer. Design a program to accept the number of
participants N such that N must be greater than 3 and less
than 11. Create a double-dimensional array of size (Nx5) to
store the answers of each participant row-wise. Calculate
the marks for each participant by matching the correct
answer stored in a single-dimensional array of size 5.
Display the scores for each participant and also the
participant(s) having the highest score.

Example: If the value of N = 4, then the array would be:


Q1 Q2 Q3 Q4 Q5

Participant 1 A B B C A

Participant 2 D A D C B

Participant 3 A A B A C

Participant 4 D C C A B

Key to the question: D C C B A


Note: Array entries are line fed (i.e. one entry per line)

Test your program for the following data and some random
data.
Example 1

INPUT:
N=5
Participant 1 D A B C C
Participant 2 A A D C B
Participant 3 B A C D B
Participant 4 D A D C B
Participant 5 B C A D D
Key: B C D A A

OUTPUT:
Scores:
Participant 1 = 0
Participant 2 = 1
Participant 3 = 1
Participant 4 = 1
Participant 5 = 2
Highest Score:
Participant 5

Example 2

INPUT:
N=4
Participant 1 A C C B D
Participant 2 B C A A C
Participant 3 B C B A A
Participant 4 C C D D B
Key: A C D B B

OUTPUT:
Scores:
Participant 1 = 3
Participant 2 = 1
Participant 3 = 1
Participant 4 = 3
Highest Score:
Participant 1
Participant 4

Example 3

INPUT:
N = 12

OUTPUT:
INPUT SIZE OUT OF RANGE.

Algorithm :
Step 1: Import Required Classes
1. Import the Scanner class from the [Link] package to
handle user input.
Step 2: Define the Main Class
1. Class Declaration: Define the class QuizCompetition.
Step 3: Main Method
1. Declare Scanner Object: Create a Scanner object
named in to read input from the console.
2. Input Number of Participants:
• Prompt the user to enter the number of

participants (n).
• Read the integer input for n.
3. Validate Input Size:
• If n is less than or equal to 3 or greater than or
equal to 11, print "INPUT SIZE OUT OF RANGE."
and terminate the program.
Step 4: Declare Arrays for Answers and Key
1. Declare 2D Array: Create a 2D character
array answers of size n x 5 to store the answers of
each participant.
2. Declare 1D Array: Create a character array key of size
5 to store the correct answers (answer key).
Step 5: Input Answers from Participants
1. Loop Through Participants:
• For each participant i from 0 to n-1:
• Print "Participant i+1".
• Loop through 5 questions:
• Read the answer for each question and
store it in answers[i][j].
Step 6: Input Answer Key
1. Prompt for Answer Key:
• Print "Enter Answer Key:".

• Loop through 5 questions to read the correct


answers:
• Store each answer in the key array.
Step 7: Calculate Scores
1. Initialize Variables:
• Declare an integer variable hScore and initialize it
to 0 to track the highest score.
• Declare an integer array score of size n to store
the scores of each participant.
2. Score Calculation Loop:
• For each participant i from 0 to n-1:
• Initialize score[i] to 0.
• Loop through 5 questions:
• If the participant's
answer answers[i][j] matches the
answer key key[j], increment score[i] by
1.
• If score[i] is greater than hScore,
update hScore to score[i].
• Print the score for the participant:
"Participant i+1 = score[i]".
Step 8: Identify and Print Highest Scorers
1. Print Highest Score:
• Print "Highest Score:".

• Loop through participants to check who has the


highest score:
• If score[i] equals hScore, print "Participant
i+1".
Step 9: Program Termination
• The program ends after displaying the highest
scorers.

Source code :
import [Link];

public class QuizCompetition


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter the Number of Participants
(N): ");
int n = [Link]();

if (n <= 3 || n >= 11) {


[Link]("INPUT SIZE OUT OF RANGE.");
return;
}

char answers[][] = new char[n][5];


char key[] = new char[5];

[Link]("Enter answers of participants");


for (int i = 0; i < n; i++) {
[Link]("Participant " + (i+1));
for (int j = 0; j < 5; j++) {
answers[i][j] = [Link]().charAt(0);
}
}

[Link]("Enter Answer Key:");


for (int i = 0; i < 5; i++) {
key[i] = [Link]().charAt(0);
}

int hScore = 0;
int score[] = new int[n];

[Link]("Scores:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
if (answers[i][j] == key[j]) {
score[i]++;
}
}

if (score[i] > hScore) {


hScore = score[i];
}

[Link]("Participant " + (i+1) + " = " +


score[i]);
}
[Link]("Highest Score:");
for (int i = 0; i < n; i++) {
if (score[i] == hScore) {
[Link]("Participant " + (i+1));
}
}
}
}

Variable Description:
Main Class: QuizCompetition
• Scanner in: An instance of the Scanner class used to
read input from the user.
• int n: Stores the number of participants in the quiz
competition.
• char answers[][]: A 2D array that stores the answers
provided by each participant. The first dimension
represents the participants, and the second
dimension represents the answers (5 questions).
• char key[]: A 1D array that stores the correct answers
(answer key) for the quiz questions.
• int hScore: Variable to track the highest score among
all participants.
• int score[]: A 1D array that stores the scores of each
participant based on their answers compared to the
answer key.

Method Description :
Main Method: public static void main(String args[])
1. Input Number of Participants:
• Prompts the user to enter the number of
participants (n).
• Checks if n is within the valid range (greater than
3 and less than 11). If not, it prints an error
message and exits the program.
2. Input Participant Answers:
• Initializes the answers array to store the answers

of each participant.
• Uses nested loops to prompt each participant to
enter their answers for 5 questions.
3. Input Answer Key:
• Initializes the key array to store the correct
answers.
• Prompts the user to enter the answer key for the

5 questions.
4. Score Calculation:
• Initializes hScore to track the highest score.
• Initializes the score array to store the scores of
each participant.
• Uses nested loops to compare each participant's
answers with the answer key:
• If a participant's answer matches the
corresponding answer in the key, their score
is incremented.
• Updates hScore if the current participant's
score exceeds the previous highest score.
• Prints each participant's score.
5. Display Highest Scorer(s):
• Iterates through the score array to find and print
the participant(s) with the highest score.
Output :

Common questions

Powered by AI

The Scanner class is used for reading input from the user in a convenient way. In this program, it is used to read the number of participants, the answers entered by each participant, and the correct answer key. This allows for dynamic input from the console, making the program interactive .

The double-dimensional array, answers[][], is used to store each participant's answers row-wise. The first dimension represents different participants, and the second dimension represents the answers to the five questions for each participant. This structure allows for easy access and comparison of any participant's answers with the answer key .

Scores are calculated by comparing each participant's answers, stored in the 2D array answers[][], with the correct answers in the 1D array key[]. For each participant, an individual score is incremented for each correct answer. The program keeps track of the highest score using the variable hScore, which is updated whenever a participant's score exceeds the previous highest score .

The program does not explicitly handle input errors or invalid data, such as non-character inputs when entering answers. It assumes correct input will be provided by users based on prompts like 'Enter Answer Key' and inputs for participant answers .

The program prompts the user to enter the number of participants (N) and checks if N is greater than 3 and less than 11. If N is outside this range, the program prints 'INPUT SIZE OUT OF RANGE.' and terminates without proceeding further .

The program handles ties by looping through each participant's score and checking if it matches the highest score (hScore). If multiple participants have the highest score, it prints each one, thereby recognizing all tied participants in the output .

The program uses a variable named hScore initialized to 0 to track the highest score. After calculating each participant's score, it compares this score with hScore. If the participant's score is higher, hScore is updated to this new value, ensuring it always contains the highest score thus far .

Table-driven logic is important as it leverages a predefined structure, the 2D array for answers and 1D array for the key, to systematically compare each participant's answers with correct answers. This method simplifies checking correctness and tallying scores through straightforward iteration and comparison, ensuring efficiency and clarity in the scoring process .

After calculating scores for all participants, the program iterates through the score array to identify all participants whose scores match hScore, the highest score recorded. It prints each of these participants as having the highest score, thereby accommodating ties .

The program prints 'INPUT SIZE OUT OF RANGE.' when the number of participants (N) entered is less than or equal to 3 or greater than or equal to 11. This validation ensures that the array sizes are manageable and prevents potential errors from array bounds if the input size is unsupported .

You might also like