473,320 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

grade multiple-choice exam

hello every body
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem

>>
You have been asked to write a program to grade a multiple-choice
exam. The exam is out of 20 questions, each answered with a letter in
the range of 'a' through 'f'. The data are stored on a file exams.dat
where the first line is the key, consisting of 20 characters. The
remaining lines on the file are exam answers, and consist of a student
ID number, a space, and a string of 20 characters. The program should
read the key, then read each exam and output the ID number and score
the file scores.dat. Erroneous input should result in an error
message. For example, given the data:

abcdefabcdefabcdefab
2001321 abcdefabcdefabcdefab
2001321 aacdffabadefcbcdafab
2001321 abcdefefabcdefab
2001321 afbcddefabcdcefabcdbefab
2001321 abjdefiabcdabgcdxeab

The program would output on scores.dat:

2001321 20
2001321 15
2001321 Too few answers
2001321 Too many answers
2001321 Invalid answers

Use functional decomposition to solve the problem and code the
solution using functions as appropriate. Be sure to use proper
formatting and appropriate comments in your code. The output should be
neatly formatted, and the error messages should be informative.

Apr 17 '07 #1
7 6639
tea-jay wrote:
hello every body
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem
I think FAQ 5.2 covers this one...
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 17 '07 #2
"tea-jay" writes:
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem

>>>

You have been asked to write a program to grade a multiple-choice
exam. The exam is out of 20 questions, each answered with a letter in
the range of 'a' through 'f'. The data are stored on a file exams.dat
where the first line is the key, consisting of 20 characters. The
remaining lines on the file are exam answers, and consist of a student
ID number, a space, and a string of 20 characters. The program should
read the key, then read each exam and output the ID number and score
the file scores.dat. Erroneous input should result in an error
message. For example, given the data:

abcdefabcdefabcdefab
2001321 abcdefabcdefabcdefab
2001321 aacdffabadefcbcdafab
2001321 abcdefefabcdefab
2001321 afbcddefabcdcefabcdbefab
2001321 abjdefiabcdabgcdxeab

The program would output on scores.dat:

2001321 20
2001321 15
2001321 Too few answers
2001321 Too many answers
2001321 Invalid answers

Use functional decomposition to solve the problem and code the
solution using functions as appropriate. Be sure to use proper
formatting and appropriate comments in your code. The output should be
neatly formatted, and the error messages should be informative.
It will be easier to debug if you print results instead of writing them to a
file. When you get it working, modify the print stuff as necessary

Think of three functions

main
open the files
read the key
read lines and look for EOF
for each line
print the student id
call valid
if not valid print a message and continue
call grade
print the grade
done
-------------
bool valid(string line)
----------
int grade(string key, string line)

read the file with the getline function, read each *entire* line into a C++
string.
This applies to the key as well as to the data lines.

If you can post code up to the point where you print (or try to print) the
student id you are likely to get further help. OTOH if you get that far you
may find you do not *need* further help.

If the instructor has been talking a lot about STL, he would expect you to
use iterators quite a bit; but I think it would be easier without iterators.

Apr 17 '07 #3
Victor Bazarov wrote:
tea-jay wrote:
>hello every body
our teacher asks us to write this assiment
it has 2 question

i did write the first one but the other was really complicated 2 me
coz
our teacher doesn't know how to explain this
so
plz plz try to solve this problem

I think FAQ 5.2 covers this one...
there isn't anything about the need to write in English?

Anyway, for tea-jay:
if the teacher is not able to explain you something, please post here
your doubts.

Regards,

Zeppe

Apr 17 '07 #4
this is my code hope any one can fixed to me
coz it didn't work in right way
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void score ( string answer, string str );
fstream infile;
ofstream outfile;

int main()
{

infile.open("exams.txt");
outfile.open("scores.txt");

string answer;
string str;
string id;

infile>>str;

for(int i=1;i<=5;i++)
{

getline(infile,str);
getline(infile,answer);
getline(infile,id);

score (answer,str );
}

infile.close();
outfile.close();

return 0;
}
void score ( string answer, string str )

{

int sum=0;
string id;
for(int i=1;i<=5;i++)
{

if( answer==str)
outfile<<id<<" "<<"20";

else
sum=sum+i;
outfile<<id<<" "<<sum;
{

if( answer>str)

outfile<<id<<" "<<"Too many answers";
else if
(answer<str)

outfile<<id<<" "<<"Too few answers";

else

outfile<<" "<<"Invalid answers";

}

}
}


Apr 18 '07 #5
"tea-jay" wrote:
this is my code hope any one can fixed to me
coz it didn't work in right way
I'll offer a few random comments, nothing close to a total fix, though.
>

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void score ( string answer, string str );
fstream infile;
ofstream outfile;

int main()
{

infile.open("exams.txt");
See if the open was successful
outfile.open("scores.txt");
Same as above
>
string answer;
string str;
Is str the most descriptive name you can think of? Does the str represent
anything? Give it that name. If it is the key, call it a key, str is just
too generic in this instance.
string id;

infile>>str;

for(int i=1;i<=5;i++)
Why 5? There are not 5 students. 5 is an *example* in the file the
instructor gave you. You simply must detect EOF to do this right. The
getline function will help you detect EOF; don't ignore the value returned.
{

getline(infile,str);
getline(infile,answer);
getline(infile,id);
The id is not on a line by itself. Look at the sample you posted.
score (answer,str );
This is a bit picky, but some think that I/O should not be offloaded to a
called funstion.
}

infile.close();
outfile.close();

return 0;
}
This isn't going to work but the first step is to get a skelton program that
gives decent parameters to the score function. One that can call score
exactly once for each student represented in the input file.
void score ( string answer, string str )

{

int sum=0;
string id;
for(int i=1;i<=5;i++)
score should compute the score for a single student, not all the students
{

if( answer==str)
outfile<<id<<" "<<"20";
Did you see the message I posted about printing as opposed to writing to a
file??????
>
else
sum=sum+i;
outfile<<id<<" "<<sum;
{

if( answer>str)

outfile<<id<<" "<<"Too many answers";
else if
(answer<str)

outfile<<id<<" "<<"Too few answers";

else

outfile<<" "<<"Invalid answers";

}

}
}


Apr 18 '07 #6
In article <11**********************@b75g2000hsg.googlegroups .com>,
ta******@hotmail.com says...

[ ... ]
for(int i=1;i<=5;i++)
{

getline(infile,str);
getline(infile,answer);
getline(infile,id);
At least as I read it, this simply doesn't correspond to the file format
you posted. That file format had the answer key on one line, followed by
five lines, each of which contained a student ID followed by that
student's answers.

For that format, you'd read the data something like:

read answer key
for number of proposed answers {
read ID
read proposed answer
score proposed answer against key
}

Also, unless memory fails, the student ID and that student's proposed
answers were on the same line, so you wouldn't normally want to use
readline to read them (or at least not the ID).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Apr 19 '07 #7
"Jerry Coffin" wrote:

<snip>
Also, unless memory fails, the student ID and that student's proposed
answers were on the same line, so you wouldn't normally want to use
readline to read them (or at least not the ID).
I thought getline was a good choice for reading this line, too, it
demonstrates the nice substring capabilities available in <string>.

Something like this

while(getline(inf, line) ) // inf is a file that has been
// opened for reading
{
string id(line, 0, 7);
cout << id; // note no endl
string answers(line, 8, 100);
int code = validate(answers); // code of 0 - input
//is fine to continue to score
int sc = score(answers);
}
// do EOF stuff

where the results returned by both validate and score() are ignored, to
avoid clutter.
In my earlier post to the OP, I think I suggested validate returns a bool,
the above changes that to an int so that printing can be in main, rather
than farmed out to the functions - this strikes me as "more pure". The above
is intended to pay attention to the instructors desire for "functional
decomposition". .

In testing the snippet above, I belatedly noted that the student ids in the
test file are all the same :-)
Apr 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: marcello stanley | last post by:
--Boundary_(ID_89yLhNmAybJBOhP/kmhVvw) Content-type: text/plain; charset=iso-8859-1 Content-transfer-encoding: 7BIT Hello, I am a mySQL newbie here and have some problem defining the mySQL...
35
by: Henry | last post by:
I was doing this program for an exercise in a book. The point was to create a program that would take a numerical grade from a user and convert it to a letter grade (yeah really easy). I tried to...
1
by: sparkid | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
1
by: mercea | last post by:
Hi all, i have a web page which contains a gridview. On this gridview, the user uses a set of radiobuttonlists to select an option. the selected option is then to be compared with the data in a...
3
by: hanie | last post by:
a student wants to know his grade average for the semester. the grades are give in letter grades with numeric equivalents. develop a solution to calculate a grade average given the letter grades(the...
0
by: cnb | last post by:
class Customer(object): def __init__(self, idnumber, review): self.idnumber = idnumber self.reviews = def addReview(self, review): self.reviews.append(review) def averageGrade(self): tot...
3
by: blamp | last post by:
I have completed most of the program I just cant get the letter grade to print from the void printGrade function.The output of the program should be: Line 1: Based on the course score, this...
9
tiktik
by: tiktik | last post by:
Hi... I am doing this simple Java program which displays a particular grade (A, B, C...) according to the mark entered. However I cannot arrange it in such a way that it displays "Invalid" if...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.