grade multiple-choice exam

Discussion in 'C++' started by tea-jay, Apr 17, 2007.

  1. tea-jay

    tea-jay Guest

    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.
     
    tea-jay, Apr 17, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Apr 17, 2007
    #2
    1. Advertising

  3. tea-jay

    osmium Guest

    "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.
     
    osmium, Apr 17, 2007
    #3
  4. tea-jay

    zeppe Guest

    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
     
    zeppe, Apr 17, 2007
    #4
  5. tea-jay

    tea-jay Guest

    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";

    }



    }


    }
     
    tea-jay, Apr 18, 2007
    #5
  6. tea-jay

    osmium Guest

    "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";
    >
    > }
    >
    >
    >
    > }
    >
    >
    > }
     
    osmium, Apr 18, 2007
    #6
  7. tea-jay

    Jerry Coffin Guest

    In article <>,
    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.
     
    Jerry Coffin, Apr 19, 2007
    #7
  8. tea-jay

    osmium Guest

    "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 :)
     
    osmium, Apr 19, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Clement
    Replies:
    5
    Views:
    799
  2. Razvan
    Replies:
    1
    Views:
    3,078
    Razvan
    Nov 22, 2004
  3. PerritoPerron

    Newbie 2 First Grade!

    PerritoPerron, Jul 30, 2003, in forum: C++
    Replies:
    1
    Views:
    336
    Alf P. Steinbach
    Jul 30, 2003
  4. Sujath
    Replies:
    3
    Views:
    458
    Muhammad Asif
    Nov 23, 2006
  5. cnb
    Replies:
    1
    Views:
    722
    George Sakkis
    Aug 29, 2008
Loading...

Share This Page