Passing parameters, arrays, functions

Hi, I have this assignment to do and I'm trying to figure out some concepts so any help would be appreciated:
Here is the problem:

a) Write a function called ReadAnswers(…) of type void that reads and stores the correct answers a one-dimensional array.

b) Write a function called ReadStudents(…) of type void that reads the student’s answers for each of the 20 questions from a file and stores them in a two-dimensional array. Each line of the file has a student identification followed by 20 letter answers. (See input).


c) Write a function called PassFail(…) of type void that finds the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions.

d) Write a function printresult(…) of type void that displays all results as indicated in the output sample.


The exam has 20 multiple choice questions. Here are the correct answers:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. B
4. A 9. C 14. A 19. D
5. C 10. D 15. D 20. A

Note: The answers should be only A, B, C, or D. Also; you assume you have 15 students in the Biology class.

A sample Input file of 5 students:

2061090 B D A C C A B C C A B D D A B C A B D B
2061143 B C A C B A B C C A B A D A B C A B D A
2071233 B C A C B B A A C A A A D A B C A A D D
2081251 A A A C B B A A C A A A D A B C A A D D
2071217 B D A A C A B A C D B C D A D C C B D A

Sample of Output:
Question 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
CORRECT B D A A C A B A C D B C D A D C C B D A

ID PASS/FAIL CORRECT INCORRECT ANSWERS
___________________________________________________________________________
2061090 PASS 13 4, 8 10, 12, 15, 17, 20
2061143 PASS 8 2, 4, 5, 8, 10, 12, 13, 15
2071233 FAIL 12 2, 4, 5, 6, 7, 10, 11, 12, 15, 17, 18, 20
2081251 FAIL 13 1, 2, 4, 5, 6, 7, 10, 11, 12, 15, 17, 18, 20
2071217 PASS 20 0

And the code I came up with was:


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<string>
#include <fstream>
using namespace std;
void ReadAnswers(); 
void ReadStudents(char array[5][]);  
void PassFail(); 
void printresult(); 

int main ()
{
	
	ifstream infile;
	ofstream outfile;
	infile.open("InputOfAnswers.txt");  
	outfile.open("OutputOfResults.txt"); 

	
	int QuestionNum[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; // these are the question numbers
	char Correctresults[20] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};  // This is the 1D array that is used as the basis for comparison it contains the correct answers
	char AnswersOfStudents[5][20];  // the answer of the students are stored in a 2D array
    string PF;  // pass or fail since this is goin to be a text use string

	
void ReadAnswers()
{
	 for (int o=0; o<20; o++)
	infile>>Correctresults[o]>>endl;
}




 void ReadStudents(char array[5][])  
 { 
	 
		infile>>
	 for (int i =1; i<5;  i++)
	 {   int ID;
		 
		 infile>>ID>>endl;//to read the ID first 

		   for (int j=0; j<20; j++)  // this loop reads the answers of each student from the text file
		   {
			   
			   infile>>AnswersOfStudents[i][j];
	
		   }
			 
	   }
 }

 void PassFail(AnswersOfStudents[][])
 {
  for (int k=0; k<5;  k++)  // a for loop for calculating the correct answers for the students (in this sample 5 i mentioned above why its 5) 
	   { 
		   int right = 0;

		   for (int r=0; r<20; r++)  
		   {
			    if (AnswersOfStudents[k][r]==Correctresults[r])// in this part compare if the result is correct-if correct then increment the counter for correct answers
				   right++;
		   }
		
		   if (right>=12)  //loop to determine whether the student passed or failed this depends on the result
			   PF = "PASS";

		   else
			   PF = "FAIL";
//The outfile below are the headings for the outfile concerning the students data
			  
           outfile<<PF<<"\t\t\t"<<right<<"\t\t\t"<<20-right<<"\t\t"; 

  }	   


  for (int p=0; p<20; p++)  // the function of this loop is to outfile the question numbers for the wrong answers- This is the content of the last column in the output file
		   {

		 if(AnswersOfStudents[k][p]!=Correctresults[p]) // to display the question number of the wrong answers
			{
			   if (QuestionNum[p]!=20)
			     outfile<<QuestionNum[p]<<", ";  // to remove the annoying comma at the end

			   else
                 outfile<<QuestionNum[p];
			}
            
		   }
		   outfile<<endl; 
       }

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

return 0;

}

void printresult()
{
	outfile<<"Question  ";

	for (int q=0; q<20; q++) // this loop's function is to outfile the number of the question
	{

		outfile<<QuestionNum[q]<<"   ";

	}
	outfile<<endl;


	outfile<<"CORRECT   ";

    for (int g=0; g<20; g++)  
	{

		outfile<<Correctresults[g]<<"   ";

	}
      outfile<<endl;  
 
	
 
	outfile<<endl;
	outfile<<"ID\t"<<"PASS/FAIL\t"<<"CORRECT ANSWERS\t\t"<<"INCORRECT ANSWERS\t"<<"INCORRECT"<<endl;
 	outfile<<"---------------------------------------------------------------------------"<<endl;
    
}

But the problem is I have to read the ID and I dont exactly know how I can read them seperately from the txt file. I dont know am I doing something wrong? What parts should I revise? The thing is I have to out put PASS/FAIL (variable pf) in the printresult function-how would you do that?
thanx in advance

Last edited on
Use code tags to post your code. It looks messy otherwise.

GM
Last edited on
Pardon my ignorance but what are code tags?
Last edited on
I think he means such as this pattern. Look at the background and numbered lines of this code

1
2
3
4
5
6
#include <iostream>
using namespace std;
void main()
{
     cout<<"Hello World";
}


You can insert it by pressing the first icon next to the text box of your reply!! It's the "hash mark (#)".
Last edited on
Thanx-I hope thats better---so any one got any ideas?
Last edited on
Line 25 says vvoid not void
I am also having a problem with passing arrays in function(I think so)

I call a function from inside another non-main() function and I get this message.


error: passing `const KNNClassifier' as `this' argument of `void KNNClassifier::ascendSort(short unsigned int*, bool*, unsigned int)' discards qualifiers|


and also

void ascendSort(unsigned short *distanceArray,bool *catArray,unsigned size)

Can anybody help me please?
DarkFire21: Sounds like you're trying to call a non-const method from a const object. Either unconst the object or const the method (which I think was done by putting const after the parameter list, and not before the return type).
Last edited on
Yes thats it.Thank you very mutch helios.I will fix it.

P.S:Something a bit offtopic...As I googled before posting my question here I saw http://cplusplus.syntaxerrors.info/index.php?title=Main_Page

I think its very useful for developers community and I suggest to everybody to start all together synchronously to expand the wiki....
Topic archived. No new replies allowed.