In: Computer Science
A multiple-choice examination consists of 20 questions. Each
question has five choices, labeled A, B, C, D and E. All data for
that exam is stored in a file exam.txt. The first line of data
contains the correct answers to the twenty questions in the first
20 consecutive (one after the other) character positions.
For Example: BECDCBAADEBACBEDDBED
Each subsequent line in the file contains the answers for a single
candidate. Data on a line consists of a candidate number (an
integer), then by one or more spaces, followed by the twenty
answers given by the candidate in the next 20 consecutive character
positions. An X is used if a candidate did not answer a particular
question. A sample line is as follows:
5555 BECDCXACCAEDCBEDDACB
There can be an unlimited number of candidates. A line containing a
“candidate number” 0 indicates the end of the data.
A student’s final score is calculated by adding up the points
awarded for all the questions. Points for a question are awarded as
follows:
• Correct answer 4 points
• Wrong answer -1 point
• No answer 0 points
Write a C# program to process the data in the file exam.txt and
generate a report that shows:
1. Each candidate number and their final score (the total points
obtained by the candidate).
2. The total number of candidates.
3. The number of correct responses to each of the 20
questions.
4. The minimum score attained by a student in the exam.
5. The maximum score attained by a student in the exam.
6. The average score attained by a student in the exam.
The report must be written to a file called Report.txt.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace smallcsarp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the file name : ");
// enter file name
String fileName = Console.ReadLine();
// raed alla line in a string
string[] lines = File.ReadAllLines(fileName);
// store all correct answers
char[] correctAnswers = lines[0].ToCharArray();
// to collect correct answers
int[] noOfcorrectAns = new int[20];
// minimum and maximum value
int max = 0, min = 100;
// student number
int[] stdNos = new int[lines.Length-1];
// number of student answers
int[] stdAns = new int[lines.Length - 1];
// each student score
int[] stdScore = new int[lines.Length - 1];
// initialize number of answers
for (int i = 0; i < 20; i++)
{
noOfcorrectAns[i] = 0;
}
for(int i = 1; i < lines.Length - 1; i++)
{
// split line
String[] spltRslt = lines[i].Split(' ');
// set student number
stdNos[i - 1] = Convert.ToInt16(spltRslt[0]);
// student answer
char[] studentAns = spltRslt[1].ToCharArray();
stdAns[i] = 0;
int total = 0;
for (int j = 0; j < 20; j++)
{
// for correct answer
if (correctAnswers[j] == studentAns[j])
{
// total added by 4
total += 4;
// correct answers
noOfcorrectAns[j]++;
}
else
{
if (correctAnswers[j] == 'X')
{
total += 0;
}
// wrong answer
else
{
total -= 1;
}
}
}
// set score
stdScore[i] = total;
// obtain minimum value
if (min > total)
{
min = total;
}
// obtain maximum value
if (max < total)
{
max = total;
}
}
// write in a file
Console.Write("Enter file name to write : ");
String writeFile = Console.ReadLine();
StreamWriter writer = new StreamWriter(writeFile);
// display report
Console.WriteLine("Candidate Number and their Score");
writer.WriteLine("Candidate Number and their Score");
for(int i = 0; i < lines.Length-2; i++)
{
Console.WriteLine("{0} {1}",stdNos[i],stdScore[i]);
writer.WriteLine("{0} {1}", stdNos[i], stdScore[i]);
}
Console.WriteLine("Total number of candidate is
{0}",lines.Length-2);
writer.WriteLine("Total number of candidate is {0}", lines.Length -
2);
Console.WriteLine("Numer of correct response : ");
writer.WriteLine("Numer of correct response : ");
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Question {0} is {1}", i+1,noOfcorrectAns[i]
);
writer.WriteLine("Question {0} is {1}", i + 1,
noOfcorrectAns[i]);
}
Console.WriteLine("Maximum score by a student in exam :
{0}",max);
writer.WriteLine("Maximum score by a student in exam : {0}",
max);
Console.WriteLine("Minimum score by a student in exam :
{0}",min);
writer.WriteLine("Minimum score by a student in exam : {0}",
min);
double stotal = 0;
for (int i = 0; i < lines.Length - 2; i++)
{
stotal += stdScore[i];
}
double avg = stotal / (double)(lines.Length - 2);
Console.WriteLine("Average score by studenin the exam :
{0}",avg);
writer.WriteLine("Average score by studenin the exam : {0}",
avg);
writer.Close();
Console.ReadKey();
}
}
}
BECDCBAADEBACBEDDBED
2342 ABBXABBDAEFFAEFDEACA
4323 XAXACCAXADAEXACEAXBC
5673 ADEXDAECXABAXACDEACA
4525 CADXECBACCACAXXEDACX
2345 DECBCAXEDCAXAXDBXCDX
6745 XADECABXCADECXBBXCAD
3455 EXXEDXACDDAEDCBEDACD
6574 ECDBBDBABBBEDDXDAEDB
2342 AADEBBBAFAEDABEEDXXA
6464 DAEXABADEXADEDACAEXE
3452 AAABXXAEXACDDACDEAXA
7465 CDAABCXEADCAXDAECBXC
6456 CADEADADABACACAXEDAX
5345 CAEEBCACDEABADEACDEA
1232 XEDACEDDDEADCCBACCDA
7567 ADADBBACADECADAECDEA
4353 BCADXEDACABBCAXDEABX
6456 AEAXAXDEABDAEXDEABXX
0