A challenging bit of code for you

Pages: 12
So i was given an assignment with some unusual parameters. My teacher likes to add difficult objectives to make the code longer (really just to be evil) and this project is proving to be a bit of a problem. I have worked on several programs he has assigned but the one i can not wrap my finger around is the one based on creating answer key and test results for students. I will post it below:

{



DECRIPTION
Write a C++ program to grade multiple-choice, including True/False, questions for an exam given in a class. A class may have one or more sections. The answer keys are the same for all sections of a class for one exam, but the number of students is normally different for different sections. Your program will be used for different classes and/or different exams, and the number of questions will be different. But any exam can have at most 30 questions. The answer key and a student's answer to a question could be a digit, between 1 and 5 (inclusive), or a character among A, B, C, D, E, F and T, or lower case equivalent.

Input Data 1. The first value in the input file is the number of questions of the exam.

2. The second line is the answer keys without spaces between any two answers.

3. The next line is the number of sections, followed by data for all sections.

4. The data for a section begins with the number of students, followed by students' data.

5. The data for each student is on a separate line, beginning with the answers from the student followed by the student's last name.

6. As the key answers, there is no space between any two answers.

7. The student's last name comes after his/her last answer without any spaces between.

8. Any student's last name has at most 15 characters.


Sample input is given later.
You can assume the input data are correct and do not check for invalid values.


Output Data 1. For each student, last name, number of correct answers, and Pass or Fail. A student passes an exam if the number of correct answers is 60% or better of total number of questions, and fails otherwise.

For each section, the total number of students and the number of students who passed the exam.

For the entire class, the number of sections, the total number of students and the number of students who passed the exam.


See sample output later for the exact output format.

The following requirements must be followed:

You must NOT use structs or 2D array.

You must use the following functions in your program.
// The function reads in numQuestions answers into array answers.
// Parameters: (out, in)
void ReadAnswers(char answers[], int numQuestions);

// The function compares a student's answers against the answer
// keys and passes back the number of correct answers and
// whether the student passed the exam.
// Parameters: (in, in, in, out, out)
void ProcessAnswer(const char keys[], const char answers[], int numQuestion, int& correct, bool& pass);

You must use the same function ReadAnswers() to read the answer keys and the answers for each student.

You must use for loops to process all sections of the class, all students of a section, and all answers of a student. No while loops are allowed on this assignment.


Your main() function could be like the following:
Read number of questions
Read and store the answer keys
Read number of sections
For each section
Process all students' answers
Update class results
Display results for the section
Display results for the entire class


You can also use function toupper(x) to convert a character to upper case. You need to include header file <ctype.h> to use the function.

Sample Input
10
t2ft5Ttfft
2
4
T2Ft4ttftTHockney
t1fF5ffttfHill
f3ft5fTTftLongwood
t4Tf5TFTFTSharp
5
T2Ft5ttffTHockney
t2ft4ttfFtHill
f2ft4tTTftLongwood
t2Tf5FFTFTSharp
t2fT5FFTtTQuick

Sample Output
Name          Correct Answers               PASS/FAIL
---------------      ---------------               ---------
        Hockney           8                         PASS
           Hill           3                         FAIL
       Longwood           6                         PASS
          Sharp           5                         FAIL

2 out of 4 students of section 1 passed the exam.

        Name             Correct Answers            PASS/FAIL
---------------          ---------------            ---------
        Hockney                   10                PASS
           Hill                    9                PASS
       Longwood                    7                PASS
          Sharp                    5                FAIL
          Quick                    6                PASS

4 out of 5 students of section 2 passed the exam.
There are 2 sections, and 6 out of 9 students passed the exam.


}




So that is the entire thing. I understand if you do not want to help me with all of it however any help will be appreciated. Thank you for anything you are willing to/can do.
Please feel free to send me questions or emails if you have any questions about it.
Again you all rock for the help, this community is a great one indeed
Last edited on
What is your basic outline for the program right now? we need at least something to work off.What problems? reading in files? displaying information?
So we have advanced to using functions/boolean/strings. We have not started reading in files so the code itself has to do everything and you will enter data right into the program itself. Its a single input at a time. And after you type everything in the order (specified above) it should print out a little chart that shows how many sections are there. The students in each section. their grades and if they passed or failed. And the key is put straight into program as well as the students names and answers. Its not intended to be permanent by any means.
Thank you so much Need4Sleep.
I know this is ALOT of stuff and i am asking for free help and all, but it measn alot that you at least gave it a look
Is this due tonight? im getting some sleep now, but i can be on around 12 in the afternoon tomorrow to help you with this exercise if someone else doesnt come around to help you out. try taking this code in steps, think of what exactly you want to do. A good idea if you're really stuck is write the code down on a piece of paper using statements such as:

MAIN FUNCTION
   I want to declare all the varibles i need
   (list varibles)
   
   Read in a value
   use this value in a function to determine.. etc... etc..

this method surprisingly helps, sometimes just sleeping it off can help the most though. Just start with the simple parts of the program and try and progress slowly, the biggest thing right now if you're having trouble is commenting alot using "//". Try to explain what you just coded and what its uses are, if it doesnt make sense to even you, chances are it is not right.
Last edited on
I really appreciate the time your giving me. It isn't due until Friday i believe but i would like to get most of it done before Wednesday night. I am going to sleep now but i will start writing the program out in the way you described, and i can post that process tomorrow if you would like. Thanks again, my teacher does not speak English very well so it is kinda hard to get extra help outside of class. Once i found this forum i knew someone would have the knowledge to help me, and i was right. Thanks again and i look forward to your post tomorrow as well.
Any code to post at the moment?
I will post within the hour. I have two tests tomorrow (in coding classes of all things) that i was studying for earlier so i am just now getting to this. I will have it up very soon
This actually doesn't sound too bad if you follow the instructions.

You need to look in a file.
Read a bunch of data.
And follow the restrictions.
Then output everything nicely.

Which part are you having trouble with?
Last edited on
Int numQuestions//number of questions on exam
Char ansKey//correct answers on exam(has to equal same amount of inputs as numQuestions)
// ansKey can be values: 1,2,3,4,5,A,B,C,D,F,T
Int numSections// number of sections
I will need a Struct Section
string StudentName
Char studentAns//student answers
String “FAIL”
String “PASS”

FUNCTIONS NEEDED:
void ReadAnswers(char answers[], int numQuestions);
// The function reads in numQuestions answers into array answers.
// Parameters: (out, in)

void ProcessAnswer(const char keys[], const char answers[], int numQuestion, int& correct, bool& pass);
// The function compares a student's answers against the answer
// keys and passes back the number of correct answers and
// whether the student passed the exam.
// Parameters: (in, in, in, out, out)
use function toupper(x) to convert a character to upper case. Just so you can enter lower case letters or uppercase into grades and I won’t need to type out a long function that makes them uppercase.

This is the part i struggle with the most when it comes to creating a program. So if you see anything that i need more (and i know there is i just can't wrap my head around it) please let me know. thanks again
You must NOT use structs or 2D array.

So why do you need a Struct Section?
toupper(x)
is just a C++ global function that turns a lowercase letter (char type) into its uppercase counterpart.
Oh i defined that in there and forgot about it. Wow thanks for the catch. so then i will just use a function that reads it all. thank you for the catch
No problem.
Please put your code into code tags, helps us read easier
Alright. i have spent all day on this and i want you all to look it over and tell me what it needs. or what you don't like/like about it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <cmath>
#include <string>
#include <ctype.h>
#include <iomanip>
using namespace std;

const int MAXQ = 30;

void  StoreData(int correct, string name, bool pass, int counter, string names[], int scores[], int studpassed[]);
void  ReadAnswers(char answers[], int numQuestions);
void  ProcessAnswers( const char key[], const char answers[], int &correct, bool &pass, const int NumQuest);
void  ProcessOneSect( int& NumStu, const int NumQuest, const char key[], int Num_sect, int count, string names[], int scores[], int studpassed[], int &passed);
void  UpdateTotal(int NumStu,int passed,int &totalStu, int &totalPassed);
void  DisplayResult(int Num_stu, string names[], int scores[], int studpassed[]);
void  header ();

void main ()
{
   int Num_quest = 0, Num_sect = 0, kids = 0, passed = 0, counter = 0;
   int totalPassed = 0, totalStu=0;
   char answers [MAXQ], key [MAXQ];
   string names[60];
   int scores[60];
   int studpassed[60];
   
   cout << "enter number of questions: "; 
   cin >> Num_quest;
   if (Num_quest > 30)
      {
         cout << "number entered is greater than maximum supported";
      }
   
   cout << endl << "enter answer key: ";
   ReadAnswers(key, Num_quest);
   
   cout << endl << "enter number of sections: ";
   cin >> Num_sect;
   
   for (int i = 1; i <= Num_sect; i++)
   {  
      ProcessOneSect(kids,Num_quest,key, Num_sect, counter, names, scores, studpassed, passed);
      UpdateTotal(kids, passed,totalStu, totalPassed,);
      counter ++;
      DisplayResult(kids, names, scores, studpassed);
      cout<<endl<<passed<<"out of "<<kids<<" students of section "<<i<<" passed the exam"<<endl;
      passed = 0;
   }
    cout << "there are " << Num_sect <<" sections, and "<< totalPassed << " out of ";
    cout << totalStu << " passed the exam.";
   }

void ReadAnswers(char answers[], int numQuestions)
{
      for (int i = 0; i < numQuestions; i++)
      cin >> answers [i];      
}

void ProcessOneSect ( int& NumStu, const int NumQuest, const char key[], int Num_sect, int count, string names[],int scores[], int studpassed[], int &passed)
{
   char answers [MAXQ];
   string name;
   int correct = 0, counter = 0;
   bool pass = true;
   
   
   
   cout << endl << "enter the number of students in the section: ";
   cin >> NumStu;
   for ( int i = 0; i < NumStu; i++)
   {
      ReadAnswers(answers, NumQuest);
         cin >> name;
      ProcessAnswers(key, answers, correct, pass, NumQuest);
      StoreData(correct, name, pass, counter, names, scores, studpassed);
      counter ++;
      if (pass==true)
         passed++;
   }
   header();
}

void ProcessAnswers( const char key[], const char answers[], int &correct, bool &pass, const int NumQuest)
{
   correct = 0;
   for (int i = 0; i < NumQuest; i++)
   {
      if ( toupper(key[i])== toupper(answers[i]))
         correct++;
   }
   if ((correct/float(NumQuest)) <= .60)
      pass = false;
}

void UpdateTotal (int NumStu,int passed,int &totalStu, int &totalPassed)
{
   totalStu += NumStu;
   totalPassed += passed;
   
}

void DisplayResult(int Num_stu, string names[], int scores[], int studpassed[])  
{
 for(int i=0; i<Num_stu; i++)
 {
   cout<<endl;
   cout<<names[i]<<"          "<<scores[i]<<"          ";
   if (studpassed[i] == 1)
         cout<<"PASSED";
   else
         cout<<"FAILED";
  }
}

void header()
{
  cout << "Name" <<"          " << "Correct Answers" <<"          "<< "Pass/Fail";
  cout << endl << endl;
  cout << "----------------------------------------------------------------------";
}

void StoreData(int correct, string name, bool pass, int counter, string names[], int scores[], int studpassed[])
{

   names[counter] = name;
   scores[counter]=correct;
   if (pass==true)
      studpassed[counter]=1;
   else
      studpassed[counter]=0;
}
First off. "void main ( )"... where is my shotgun.
int main ( ) only unless you want to be flamed for it.

Second: UpdateTotal ( kids, passed, totalStu, totalPassed, );
Missing the last parameter. *Extra comma?

See: http://www.cplusplus.com/forum/beginner/19979/
For void main vs int main.
Last edited on
Need to check for user errors. If user types to many answer keys, it throws off the program.

Other than that.. Not sure what you wanted your program to do anymore..
Thanks for the feedback. Yeah it is an extra comma i forgot about. Sorry for the answers my teacher gave us the restraint of only using "1,2,3,4,5,a,b,c,d,t,f" forgot to mention that. But essentially it has couts for everything up until you type in student answers and student name 12345abcGreg is how he wanted us to enter the data (he puts really bizarre structures to his programs) and since its Characters you don't put spaces in between them. So the input should look like:

10
t2ft5Ttfft
2
4
T2Ft4ttftTHockney
t1fF5ffttfHill
f3ft5fTTftLongwood
t4Tf5TFTFTSharp
5
T2Ft5ttffTHockney
t2ft4ttfFtHill
f2ft4tTTftLongwood
t2Tf5FFTFTSharp
t2fT5FFTtTQuick

Ok I see. Well. It would be BEST if you put couts for everything. Also. There were a few logical errors, though I'm not really sure what caused them. After type 5, I typed one student's info and the program thought I was finished.

----------------------------------------------------------------------
THockney          8          PASSED
Hill          3          FAILED
Longwood          6          FAILED
THockney          2          FAILED
1out of 4 students of section 1 passed the exam
Last edited on
hmm. I will make sure to fix that. Yeah my teacher likes our programs to look EXACTLY like his example input/output for some reason. but i agree i would normally do cout<< for everything that asks for an input so you know what the hell your typing in.
thanks for the help
Pages: 12