2D/3D arrays with files

Need help understanding how to use 2d arrays with 3d arrays. First part is the program and below that is my confusing attempt. If I could get a small example or tips, anything would be very helpful, thanks.


/*
Create two and three dimensional arrays to store all input and output data and fill these arrays by the data read from the input file and by the computed data. The data of all students will be stored in these arrays before writing it to the output file.

As you know, all elements of an array must be of same type. Therefore, you will be creating different arrays to store different types of data. For an example you could have the following array declarations:



o A two dimensional array nonNumeric1 of type string to store headings, Student name, Student ID, Student Telephone, Student Address, Student Social Security Number, title of course 1, title of course 2, title of course 3. For three students it may look like nonNumeric1 [3] [9]

o A two dimensional array numeric 1 of type int to store the age and the number of years at university. For three students it may look like
numeric1 [3] [2].

o A three dimensional array numeric2 of type double to store input data (read from input file) for three students, three courses and five exams, and computed Numerical Grades. For three students it may look like

Numeric2 [3] [3] [6]

o A two dimensional array letterGrade of type char to store letter grades of three students and three courses. For three students and three courses it may look like letterGrade [3] [3].

· Read the input from the Input File. Report should be based on three students each taking three courses. It means that you will be using a nested “for” loop. The outer loop will be on students, the middle loop will be on courses and the inner loop will be on five Exams. Use the Computed Numerical grade to determine commend/warning comments as follows:



o If the CNG is < 70, Warn: Your grade is too low and needs improvements

o If the CNG is >= 95, Commend: Congratulations, Your performance is Excellent

· In your loops, you should also check that arrays do not get out of bound.

· At the end when all the input and output data has been stored in arrays, you will write all the data from arrays to the output file.
*/


////////////////////Below is what I have come up with so far//////////////////

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

int main()

{

const int stdntrow = 3;

const int stdntcolumn = 9;

const int Age = 3;

const int Numyrsatuniversity = 2;

string nonNumeric1 [stdntrow][studntcolumn];

int numeric1 [Age][Numyrsaruniversity];

int NUM_student;


double stdnt = 3;

double stdntcourses = 3;

double stdntexams = 6;

double numeric2 [stdntdata][stdntcourses][stdntexams];

//

cout << "Enter the number of students: " << endl;

cin >> NUM_student;

cout << endl;


//Validate the input

while (NUM_student < 3 || NUM_student > 3)

{

cout << "Invalid." << endl;

cout << "Number should be exactly 3. Enter number again: ";

cin >> NUMstudent;

cout << endl;

}

ofstream outputFile;

outputFile.open ("Outputfile.txt");

ifstream inputFile;

inputFile.open("Inputfile.txt");


for (int studentd =0; studentd < 3; studentd++)

{

//Outer Loop

for(int student = 1; student <= NUMstudent; student++){

for (int m =0; m < studntrow; m++){

for (int n = 0; n < stdntcolumn; n++){

getline(inputFile, nonNumeric1[m][n]);

for (int ag =0; ag < Age; ag++){

for (int yrs = 0; yrstxst < Numyrsatuni; yrs++){

inputFile >> numeric1[ag][yrs];

inputFile.ignore();

outputFile << setw(40) << "Grade Sheet for " << nonNumeric1[m][n] << endl;

outputFile << setw(37) << "Name of Student:\t" << nonNumeric1[m][n] << endl;

outputFile << setw(37) << "Student ID:\t" << nonNumeric1[m][n]<< endl;

outputFile << setw(37) << "Address:\t" << nonNumeric1[m][n] << endl;

outputFile << setw(37) <<"Telephone Number:\t" << nonNumeric1[m][n] << endl;

outputFile << setw(37) <<"Student Soc. Security#:\t" << nonNumeric1[m][n]<< endl;

outputFile << setw(37) << "Age:\t" << numeric1[ag][yrstxst] << endl;

outputFile << setw(37) << "Number of years at univerity:\t" << numeric1[ag][uni] << endl;

}

}

}

}

for (int coursed = 0; coursed < stdntcourse; coursed++)

{

//Middle Loop

for (int numcourse = 1; numcourse < 4; numcourse++){

outputFile << setw(37) << "Course Number:\t" << endl;

for(int examd = 0; examd < stdntexam; examd++)

{

//Inner Loop

for(int numtest =1; numtest < 6; numtest++)

//Calculate numerical grade

grade1 = test1 * 0.10;

grade2 = test2 * 0.15;

grade3 = test3 * 0.15;

grade4 = test4 * 0.20;

grade5 = test5 * 0.40;

CNG = grade1 + grade2 + grade3 + grade4 + grade5;

//Determine letter grade

if (CNG >= 90)

letterGrade = A_score;

else if (CNG >= 80)

letterGrade = B_score;

else if (CNG >= 70)

letterGrade = C_score;

else if (CNG >= 60)

letterGrade = D_score;

else

letterGrade = F_score;

//Set output format

outputFile << fixed << showpoint << setprecision(2);

//Display student grades

outputFile << setw(37) << "Exam #1:\t" << test1 << endl;

outputFile << setw(37) << "Exam #2:\t" << test2 << endl;

outputFile << setw(37) << "Exam #3:\t" << test3 << endl;

outputFile << setw(37) << "Exam #4:\t" << test4 << endl;

outputFile << setw(37) << "Exam #5:\t" << test5 << endl;

outputFile << setw(28) << "Numerical Grade for " << course << ":\t" << CNG << endl;

outputFile << setw(28) << "Letter Grade for " << course << ":\t" << letterGrade << endl;

//Display appreciation or warning comment if applicable

if (CNG < 70)

{

outputFile << setw(37) << "Warning Note:\t"

<< "Warning Note: Your grade is too low and needs improvements\n"<< endl;

}

else if (CNG >=95)

{

outputFile << setw(37) << "Commend Note:\t"

<< "CONGRATULATIONS! Your performance is Excellent\n"<< endl;

}

else

outputFile << endl;

}

}


inputFile >> Numeric2[studentd][coursed][examd];

inputFile.ignore();

}

}

}
Topic archived. No new replies allowed.