Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two Dimensional Arrays C++ PROBLEM STATEMENT: You must use a 2-dimensional array

ID: 3663355 • Letter: T

Question

Two Dimensional Arrays C++

PROBLEM STATEMENT:

You must use a 2-dimensional array for all of the data.

Your solution must include at least 5 functions. And ...

At least one function must include a two dimensional array argument.

At least one function must include an argument that consists of one row of a two dimensional array.

At least one function must include an argument that consists of one element of a two dimensional array.

All files must be checked for a successful open.

----------------------------------------------------------

MY SOURCE CODE AND ATTEMPT USING ONE DIMENSIONAL ARRAYS:

How is this changed to fit the problem statement?

-----------------------------------------------------------

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;


void Total(int& a,int& b,int& c,int& d,int& e,int& f,int& g,int& h,int& i, int& j,int& k, int& l,int& x);
void percentage(int& a, double& perc);
void grade(double m,int& counA,int& counB,int& counC,int& counD,int& counF);

int main()
{

    const int student=50;
    const int ass=450;
    int studentid[student],exerc[student],assi[ass],mid[student],fin[student],cl[student],i,x,y,counA,counB,counC,counD,counF;
    double perc;
    y=400;
    counA=0;
    counB=0;
    counC=0;
    counD=0;
    counF=0;


    //OPPENING THE FILE
    ifstream data;
    data.open("data.txt");
    ofstream OutputData;
            OutputData.open ("OUTPUT.txt");

    //FILE OPEN FAILURE
    if(data.fail()||OutputData.fail())
        {
        cerr<<"THE FILE COULD NOT BE OPENED"<<endl;
        }
    else
    {

        cout<<"Student ID:"<<endl;
        for (i = 0; i < student; i++)
            {
            data>>studentid[i]>>exerc[i]>>assi[i]>>assi[(i+1)]>>assi[(i+2)]>>assi[(i+3)]>>assi[(i+4)]>>assi[(i+5)]>>assi[(i+6)]>>assi[(i+7)]>>mid[i]>>fin[i]>>cl[i];

            Total(exerc[i],assi[i],assi[(i+1)],assi[(i+2)],assi[(i+3)],assi[(i+4)],assi[(i+5)],assi[(i+6)],assi[(i+7)],mid[i],fin[i],cl[i],x);

            percentage(x, perc);
            //grade(perc,counA,counB,counC,counD,counF)
            cout<<studentid[i]<<"    Ex:"<<exerc[i]
            <<" Assignments:"<<assi[i]<<" "<<assi[(i+1)]<<" "<<assi[(i+2)]<<" "<<assi[(i+3)]<<" "<<assi[(i+4)]<<" "<<assi[(i+5)]<<" "<<assi[(i+6)]<<" "<<assi[(i+7)]
            <<" "<<"Midterm: "<<mid[i]
            <<" "<<" Final:"<<fin[i]<<endl;
            cout<<"           "<<" CodeLabs:"<<cl[i]
            <<" "<<"Total:"<<x
            <<" "<<"Percentage: "<<perc<<"%"
            <<" "<<"Grade:";
            grade(perc,counA,counB,counC,counD,counF);
            cout<<endl;
            //----------------------------------FILEOUT
            OutputData<<studentid[i]<<"    Ex:"<<exerc[i]
            <<" Assignments:"<<assi[i]<<" "<<assi[(i+1)]<<" "<<assi[(i+2)]<<" "<<assi[(i+3)]<<" "<<assi[(i+4)]<<" "<<assi[(i+5)]<<" "<<assi[(i+6)]<<" "<<assi[(i+7)]
            <<" "<<"Midterm: "<<mid[i]
            <<" "<<" Final:"<<fin[i]<<endl;
            OutputData<<"           "<<" CodeLabs:"<<cl[i]
            <<" "<<"Total:"<<x
            <<" "<<"Percentage: "<<perc<<"%"
            <<" "<<"Grade:";
            grade(perc,counA,counB,counC,counD,counF);
            OutputData<<endl;

            }
            OutputData<<"Number of A Grades: "<<counA<<endl;
            OutputData<<"Number of B Grades: "<<counB<<endl;
            OutputData<<"Number of C Grades: "<<counC<<endl;
            OutputData<<"Number of D Grades: "<<counD<<endl;
            OutputData<<"Number of F Grades: "<<counF<<endl;


    }

    return 0;
}
void Total(int& a,int& b,int& c,int& d,int& e,int& f,int& g,int& h,int& i, int& j,int& k, int& l,int& x)

{
x=a+b+c+d+e+f+g+h+i+j+k+l;
}

void percentage(int& a, double& perc)
{// int y=400;
    //int xy

perc=100*(a/400.0);
}

void grade(double m,int& counA,int& counB,int& counC,int& counD,int& counF)
{
   if(m>=98)
    {
       cout<<"A+"<<endl;
       counA++;
    }
    if(m<98&&m>=90)
    {
        cout<<"A"<<endl;
        counA++;
    }
    if (m>=88&&m<90)
    {
        cout<<"B+"<<endl;
        counB++;
    }
    if (m<88&&m>=80)
    {
        cout<<"B"<<endl;
        counB++;
    }
    if (m>=78&&m<80)
    {
        cout<<"C+"<<endl;
        counC++;
    }
    if (m<78&&m>=70)
    {
        cout<<"C"<<endl;
        counC++;
    }
    if (m<70&&m>=68)
    {
        cout<<"D+"<<endl;
        counD++;
    }
    if (m<68&&m>=60)
    {
        cout<<"D"<<endl;
        counD++;
    }
    if (m<60)
    {
        cout<<"F"<<endl;
        counF++;
    }
}


--------------------------------------------------------------------------------------------

The data being read is as so:

-------------------------------------------------------------------------------------------

33339883 55 20 17 12 20 20 20 11 13 40 113 16
77418770 46 18 11 14 19 18 11 9 9 23 124 15
83233929 24 15 13 18 11 14 15 16 15 62 97 20
90076387 57 20 16 9 9 19 17 20 17 67 97 18
47032460 60 18 18 14 11 17 18 19 6 61 101 20
87305446 57 20 19 19 17 14 16 17 20 66 98 19
01234567 57 16 14 20 8 10 19 11 9 53 21 16
94239962 60 15 16 14 19 13 17 14 3 62 89 8
70079096 23 20 8 20 17 9 5 14 19 65 74 11
79604055 40 15 11 5 20 17 15 13 15 46 102 13
55115616 16 20 16 20 19 7 17 16 13 71 78 7
61269380 26 19 15 13 11 15 13 19 19 47 93 15
75386679 60 11 13 5 17 10 18 17 17 69 84 19
93709582 32 11 18 19 18 19 15 19 10 37 84 13
44576634 49 20 6 20 20 20 19 18 20 52 53 19
53239850 41 11 10 18 14 17 18 17 11 40 84 16
99055483 54 11 20 19 19 15 19 20 16 33 108 16
90564385 30 20 19 20 18 18 15 13 15 51 65 20
22645995 60 13 15 17 18 17 10 12 14 74 122 17
34029990 56 8 18 3 10 16 17 12 7 56 113 18
63898722 39 17 12 13 20 14 9 13 18 66 107 18
56476561 59 20 9 17 13 16 6 13 16 37 74 20
92597454 46 11 17 4 20 15 14 17 12 60 48 20
50896682 59 8 18 13 18 15 14 15 13 26 120 13
61682337 51 19 7 13 12 13 20 18 3 22 121 19
93214790 45 19 19 12 18 13 5 17 10 59 98 17
89168351 52 15 16 11 14 10 17 20 15 59 120 14
80762865 40 12 20 15 11 2 14 17 14 35 124 19
54825116 30 15 17 12 8 20 12 17 9 35 111 18
80165967 34 18 13 11 19 19 8 12 14 71 97 17
75598921 38 17 15 17 6 8 9 18 19 66 60 16
87499254 54 20 12 18 9 18 17 9 9 56 111 20
99684437 22 16 20 18 9 18 17 19 12 69 78 20
92067579 45 19 16 15 17 19 19 8 9 51 122 20
12745220 55 20 17 16 18 19 15 20 4 22 64 19
67799141 27 12 14 18 15 10 10 7 14 66 78 20
92607296 51 20 15 14 14 16 13 19 16 54 61 13
46479276 39 20 7 13 16 7 15 18 10 26 52 18
88378754 46 17 18 17 10 17 17 15 13 63 108 20
96726299 44 20 8 17 9 20 15 16 14 49 124 11
99875647 49 13 20 13 19 17 18 15 16 56 53 19
94958157 60 20 20 20 20 16 18 17 15 75 94 17
84618065 53 17 14 13 17 20 10 19 17 28 100 14
87858806 23 9 17 17 8 20 20 20 10 70 117 18
79949725 46 11 16 17 13 12 12 20 10 47 64 11
73528933 48 19 18 18 17 16 17 16 18 70 119 19
88258087 33 18 18 15 19 10 20 19 20 64 113 20
91869832 35 19 18 20 16 9 19 11 17 62 116 13
71347292 46 10 17 15 19 15 19 13 18 47 95 13
94488759 57 17 5 16 4 12 20 20 10 43 114 15

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

struct student{
   public:
       string ID;
       int ex_mrk;
       int* ass_mrk;
       int mid_mrk;
       int fin_mrk;
       int cod_mrk;
       student(string id,int ex,int* l,int mid,int fin,int cod){
           ID = id;
           ex_mrk = ex;
           ass_mrk = l;
           mid_mrk = mid;
           fin_mrk = fin;
           cod_mrk = cod;
       }
};

int best_seven(int* l,int n){
   sort(l, l + n);
   int sum = 0;
   for (int i = 1; i < n; i++)
       sum += l[i];
   return sum;
}

int total_per(int n){
   if (n % 4 == 0 || n % 4 == 1) return n/4;
   return n/4 + 1;
}

string grade(int n){
   if (n >= 90) return "A";
   if (n >= 80 && n < 88) return "B";
   if (n == 88 || n == 89) return "B+";
   if (n >= 70 && n < 78) return "C";
   if (n == 78 || n == 79) return "C+";
   if (n >= 60 && n < 68) return "D";
   if (n == 68 || n == 69) return "D+";
   if (n >= 50 && n < 58) return "E";
   if (n == 58 || n == 59) return "E+";
   return "F";
}

struct compare_ID{
   inline bool operator() (const student* s1, const student* s2){
       return (s1->ID < s2->ID);
   }
};

struct compare_Total{
   inline bool operator() (const student* s1, const student* s2){
       int best_1 = best_seven(s1->ass_mrk,8);
       int total_mrk_1 = s1->ex_mrk + best_1 +s1->mid_mrk+s1->fin_mrk+s1->cod_mrk;

       int best_2 = best_seven(s2->ass_mrk,8);
       int total_mrk_2 = s2->ex_mrk + best_2 + s2->mid_mrk + s2->fin_mrk + s2->cod_mrk;

       return (total_mrk_1 < total_mrk_2);
   }
};

int main(){
   ifstream infile;
   string filename = "input.txt";
   infile.open(filename.c_str());
   string ID;
   int ex_mrk;
   int* ass_mrk;
   int mid_mrk;
   int fin_mrk;
   int cod_mrk;
  
   vector<student*> std;
   while (!infile.eof()){
       infile >> ID >> ex_mrk;
       ass_mrk = new int[8];
       for (int i = 0; i < 8; i++)
           infile >> ass_mrk[i];
       infile >> mid_mrk >> fin_mrk >> cod_mrk;
       student* temp_std = new student(ID,ex_mrk,ass_mrk,mid_mrk,fin_mrk,cod_mrk);
       std.push_back(temp_std);
   }
   infile.close();
  
   sort(std.begin(), std.end(), compare_ID());
   ofstream outfile;
   filename = "output_1.txt";
   outfile.open(filename.c_str());
   outfile << "Stdnt Id Ex -------- Assignments ------- Tot Mi Fin CL Pts Pct Gr" << endl;
   for (int i = 0; i < std.size(); i++){
       outfile << std[i]->ID << ' ' << std[i]->ex_mrk << ' ';
       for (int j = 0; j < 8; j++)
           outfile << std[i]->ass_mrk[j] << ' ';
       int best = best_seven(std[i]->ass_mrk,8);
       outfile << ' ' << best << ' ' << std[i]->mid_mrk << ' ' << std[i]->fin_mrk << ' ' << std[i]->cod_mrk << ' ';
       int total_mrk = std[i]->ex_mrk+best+std[i]->mid_mrk+std[i]->fin_mrk+std[i]->cod_mrk;
       int per = total_per(total_mrk);
       outfile << total_mrk << ' ' << per << ' ' << grade(per) << endl;
   }
   outfile.close();

   sort(std.begin(), std.end(), compare_Total());
   filename = "output_2.txt";
   outfile.open(filename.c_str());
   outfile << "Stdnt Id Ex -------- Assignments ------- Tot Mi Fin CL Pts Pct Gr" << endl;
   for (int i = 0; i < std.size(); i++){
       outfile << std[i]->ID << ' ' << std[i]->ex_mrk << ' ';
       for (int j = 0; j < 8; j++)
           outfile << std[i]->ass_mrk[j] << ' ';
       int best = best_seven(std[i]->ass_mrk,8);
       outfile << ' ' << best << ' ' << std[i]->mid_mrk << ' ' << std[i]->fin_mrk << ' ' << std[i]->cod_mrk << ' ';
       int total_mrk = std[i]->ex_mrk+best+std[i]->mid_mrk+std[i]->fin_mrk+std[i]->cod_mrk;
       int per = total_per(total_mrk);
       outfile << total_mrk << ' ' << per << ' ' << grade(per) << endl;
   }
   outfile.close();

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote