By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,668 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,668 IT Pros & Developers. It's quick & easy.

grade multiple-choice exam

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.