Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > grade multiple-choice exam

Reply
Thread Tools

grade multiple-choice exam

 
 
tea-jay
Guest
Posts: n/a
 
      04-17-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-17-2007
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


 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      04-17-2007
"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.



 
Reply With Quote
 
zeppe
Guest
Posts: n/a
 
      04-17-2007
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

 
Reply With Quote
 
tea-jay
Guest
Posts: n/a
 
      04-18-2007
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";

}



}


}






 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-18-2007
"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";
>
> }
>
>
>
> }
>
>
> }




 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-19-2007
In article < .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.
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-19-2007
"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
return reduce(lambda x, y: x.grade+y.grade, self.reviews) cnb Python 1 08-29-2008 06:20 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM



Advertisments