2D Array problem

Newbie here. Here's a description: A class of students take a 20-question MC exam and each question has five choices (A,B,C,D,E), only one correct and assuming every student answers all questions. The Tests.dat file consist of StudentID and their responses to the exam. Answers.dat is the answer key.

PART 1) I need to write a program that reads in the correct answers from Answers.dat and then loop through the Tests.dat to produce a report that displays the studentid and number correct as follows:

Student ID Number Correct

430-52-6192 13
112-81-5225 19
etc etc.

PART 2) Also, it needs to display number of students taking exam.
PART 3) I need to display a 2D array (counter[20][5]) to show the total number of students who chose each choice by question and print asterisks next to the correct answer as follows:

Question A B C D E
1 5 1 13* 3 1
2 4 7* 1 7 4
....etc
20 2 0 1 1 19*

Part 3 is the trouble I am having trouble with. I tried google and this forum but can't seem to find an answer. I posted my code and any help is appreciative.

MY CODE:

#include <fstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
ifstream finans, fintest, fin;
ofstream fout;

char ch, ans[21], id[12], responses[21];
int numstudents=0, numcorrect, i, j, numA=0, numB=0, numC=0, numD=0, numE=0;
int counter[20][5]={0};

finans.open("Answers.dat");
fintest.open("Tests.dat");
fout.open("Output.dat");

finans >> ans; //reads 20 char in a row
fintest >> id >> responses; //reads first student's id and that one student's responses

fout << setw(15) << "Student ID" << "\t\t" << setw(15) << "Number Correct" << endl << endl;

while(!fintest.eof())
{
numstudents++;
numcorrect=0;
for(i=0; i<=19; i++)
{
if(ans[i]==responses[i])
{
++numcorrect;
}
for(j=0; j<5; j++)
{
//PRETTY SURE SOMETHING GOES HERE BUT NOT SURE
}
}

fout << setw(15) << id << "\t\t" << setw(15) << numcorrect << endl;
fintest >> id >> responses;
}

fout << "\n\n" << setw(37) << "Number of students taking exam = " << numstudents << "\n\n\n\n";
fout << setw(37) << "Question A B C D E" << endl << endl;

for(i=0; i<=19; i++)
{
for(j=0; j<5; j++)
{
fout << counter[i][j] << endl;
}
}
}
1
2
3
4
5
6
7
8
9
void print2darray(int array[][]){
    for(int i = 0; i < array.length(); i++){
        for(int j = 0; j < array.width(); j++{
            std::cout << array[i][j];
        }
            std::cout << '\n'; 
    }
}

you got your endl in the wrong place. move it outside the inner for loop and your good to go.
Last edited on
Thanks. That fixes my formatting where it outputs 20 rows and 5 columns like I need it to. However, I don't know what code to put in the section where I made a comment //PRETTY SURE SOMETHING GOES HERE BUT NOT SURE. I put counter[i][j]++; in that spot and got the output below. See PART3 above for a description of what I need. Here is my incorrect output:

Question    A    B    C    D    E

2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323
2323232323


I need it to be this:

Question    A     B     C     D     E
1           5     1     13*   3     1
2           4     7*    1     7     4
(etc etc)
20          2     0     1     1    19*
Another note about the above reply. As you see, the output is adding the numbers instead of separating them like it's suppose to. any help would be excellent.
Topic archived. No new replies allowed.