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

Hello All, I\'m having trouble with Computer Science class and we have a program

ID: 3693297 • Letter: H

Question

Hello All,

I'm having trouble with Computer Science class and we have a program that we must create using classes, linked lists, c-strings, and dynamic arrays. I understand the concepts on paper when going over them, and when we do the programs together in class I understand as we go along. However, when we get assigned homework, it's always code that we haven't worked on together and I unfortunately don't learn that way. If someone is looking for a challenge and willing to write the program below so that I may study it to figure out how all the concepts work together I would greatly appreciate

The program is written in C++

This is the first time I have used Chegg to post a question/ask for help so I'm not exactly sure how this is done.

********************************************************************************************************

This program covers: flow control (branch and loops); input and output (formatting); arrays,
dynamic array and C-strings; classes and inheritance and linked lists. Refer to the respective
chapter for details.

A student is enrolled in two or more courses during a typical semester. Each course final grade is
computed based on different number of tasks like assignments, quizzes and exams. Assume that
all exams represent 60% of the final grade, homeworks 30% and all quizzes 10%.

All the grades are stored in a text file named grades.txt. The format is:
<TYPE> <COURSE_PREFIX_NUMBER> <GRADE>

Where:
       TYPE: is an integer representing an enum with values EXAM=0, HOMEWORK=1, QUIZ
       = 2, FINAL_GRADE=3;
       COURSE PREFIX AND NUMBER: is a string, representing the class id for each course.
       i.e. CS1337
       GRADE: a double value [0 to 100], representing the grade for the task.
For example:
           0 CS1337 89
           1 CS2305 95
           1 CS1337 100
           2 PHY1305 67
The above represents the grades of one exam and one homework for CS1337; one homework for
CS2305 and one quiz for PHY1305.

Based on the information collected for each course, the averages (exams, homeworks and quizzes)
will be used to compute the final grade (60% exams, 30% homeworks and 10% quizzes)

You will write a program to read the input file, store the information in the proper data structure,
display a menu and perform the corresponding action.

i) (5 points) Define a base class with the following data members, including constructor,
destructor (to release dynamically allocated memory) and accessors and mutators:
      o char * courseName; // To store class prefix and number
      o TYPE task:// Enum type to represent EXAM, HW, QUIZ or FINAL
      o double grade; // A double value from 0 to 100

ii) (5 points) Derive a class from i) and call it “node”, adding the data members, accessors
and mutators needed to be used as a node in a doubly linked list.

iii) (15 points) Implement a doubly linked list, and name it “grades”, using the class in ii).
Include (at least) the following operation:
      o Append, remove, find node:
      o Any other method needed

iv) (25 points) Read the input file “grades.txt” and store this information in the linked list
grades.

v) (10 points) Define another doubly linked list, and name it “courses”. Each node in this
linked list represent one course. This list is used to collect all the final grades; therefore the
task data member is always set to FINAL.

vi) Display a menu
     a) Display final grades
     b) Display course grades
     c) Semester Average
     d) Exit

(20 points) When option a is selected, for each different course, compute the averages (exams,
homeworks and quizzes); using the percentages given before (60%, 30% and 10%), compute the
final grade, and use a table format to display final grades, indicating with – (minus) the smallest
grade and +(plus) the highest grade. For example
        COURSE No.                 FINAL GRADE NOTE
        CS1337                          89.0                                      -
        CS2305                         100.0                                     +
        PHY1305                        99.0


(10 points) When option b is selected, you first ask for the course prefix and number (i.e.
CS1337), if the course prefix is not found, your program should display a message indicating it.
CS1337– Programming Project Page: 3
Otherwise, using the table format below, display all the information about the course. For example, say that class prefix and number CS1337 was entered:
            COURSE             No.              GRADE TYPE
            CS1337               98.5              EXAM
            CS1337               100.0            HW
            CS1337                99.0             QUIZ

(5 points) When option c is selected, the program will compute the semester average as the sum of the final grades, divided by the number of courses taken, a display a message like:
               THIS SEMESTER YOU AVERAGE WAS 97.9/100!

(2 points) When option d is selected, the program will terminate.

(2 points) When any other option is selected, the program should display a message, indicating that the entered option was invalid.

(1 points) Continue displaying the menu, until option d is chosen.

**********************************************************************************************************

Explanation / Answer

1. course.h:

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

/*
* course.h
*
* course class declaration
*/

#ifndef COURSE_H_
#define COURSE_H_

#include <iostream>

using namespace std;

enum TYPE{EXAM,HW,QUIZ,FINAL}; //enum type for task type

class course {
private:
   char* courseName; //To store class prefix and number
   TYPE task; //Enum type to represent EXAM, HW, QUIZ or FINAL
   double grade; // A double value from 0 to 100
public:
   course(); //constructor
   virtual ~course(); //destructor

   //accessors and mutators
   char* getCourseName() const;
   void setCourseName(char* courseName);
   double getGrade() const;
   void setGrade(double grade);
   TYPE getTask() const;
   void setTask(TYPE task);
};

#endif /* COURSE_H_ */

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

2. course.cpp:

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

/*
* course.cpp
* Implementation of course class
*/

#include "course.h"

course::course() {
   // TODO Auto-generated constructor stub
   courseName=NULL;
   task=EXAM;
   grade=-1;
}

char* course::getCourseName() const {
   return courseName;
}

void course::setCourseName(char* courseName) {
   this->courseName = courseName;
}

double course::getGrade() const {
   return grade;
}

void course::setGrade(double grade) {
   this->grade = grade;
}

TYPE course::getTask() const {
   return task;
}

void course::setTask(TYPE task) {
   this->task = task;
}

course::~course() {

delete this;

}

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

3. node.h:

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

/*
* node.h
*
* node class declaration
*/

#ifndef NODE_H_
#define NODE_H_

#include "course.h"

class node: public course {
private:
   node* prev; //link to previous node
   node* next; //link to next node
public:
   node();
   virtual ~node();

   //accessors and mutators
   node* getNext();
   void setNext(node* next);
   node* getPrev();
   void setPrev(node* prev);
};

#endif /* NODE_H_ */

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

4. node.cpp:

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

/*
* node.cpp
*
* implementation of node class
*/

#include "node.h"

node::node():course() {
   // TODO Auto-generated constructor stub
   prev=NULL;
   next=NULL;
}

node* node::getNext() {
   return next;
}

void node::setNext(node* next) {
   this->next = next;
}

node* node::getPrev() {
   return prev;
}

void node::setPrev(node* prev) {
   this->prev = prev;
}

node::~node() {
   // TODO Auto-generated destructor stub
   delete this;
}

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

5. grades.h:

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

/*
* grades.h
*
* declaration of grades class representing a doubly linked list of grades
*/

#ifndef GRADES_H_
#define GRADES_H_

#include "node.h"

class grades {
   node* head; //head node of doubly linked list of grades
   node* tail; //tail node of doubly linked list of grades
public:
   grades(); //constructor
   virtual ~grades(); //destructor

   //utility methods
   bool isEmpty(); //tells whether list is empty
   void append(node*); //append a node to the tail of the list
   void remove(node*); //remove a node from the list
   node* findNode(node*); //find a node in the list
};

#endif /* GRADES_H_ */

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

6. grades.cpp:

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

/*
* grades.cpp
*
* implementation of grades class
*/

#include "grades.h"

//constructor
grades::grades() {
   // TODO Auto-generated constructor stub
   head=NULL;
   tail=NULL;
}

//destructor
grades::~grades() {
   // TODO Auto-generated destructor stub
   delete this;
}

//returns whether list empty
bool grades::isEmpty() {
   return (head==NULL);
}

//appends a node to the tail of the list
void grades::append(node* n) {
   if(isEmpty())
   {
       head=node;
       tail=node;
   }
   else
   {
       n->setPrev(tail);
       tail->setNext(n);
       tail=n;
   }
}

//removes a node containing a given grade from the list
void grades::remove(node* n) {
   node* nodeToRemove=findNode(n);
   if(nodeToRemove!=NULL)
   {
       node* prevToNodeToRemove=nodeToRemove->getPrev(); //previous node of nodeToRemove
       node* nextToNodeToRemove=nodeToRemove->getNext(); //next node of nodeToRemove

       //updates pointers to detach nodeToRemove from list
       nextToNodeToRemove->setPrev(prevToNodeToRemove); //prevToNodeToRemove becomes the previous node of nextToNodeToRemove
       prevToNodeToRemove->setNext(nextToNodeToRemove); //nextToNodeToRemove becomes the next node of prevToNodeToRemove

       //deletes the node
       delete nodeToRemove;
   }
   else
       cerr<<"Error deleting node!"<<endl;
}

//returns a node if it exists in the list
node* grades::findNode(node* n) {
   node* curr=head; //search will start from head node
   node* foundNode=NULL; //pointer to found node
   while(curr!=NULL)
   {
       if(curr->getGrade()==n->getGrade())
       {
           foundNode=n;
           break;
       }
       curr=curr->getNext();
   }
   return foundNode;
}


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

7. main funciton:

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

#include <iostream>
#include <fstream>
#include <sstream>
#include "grades.h"

using namespace std;

//reads grades from file and loads them into linked list
void readAndLoadGrades(string gradeFileName,grades gradeList)
{
   ifstream infile(gradeFileName.c_str());

   //check if file exists or open
   if(!infile)
   {
       cerr<<"Error opening file!"<<endl;
       return;
   }

   //reads file line by line
   string line;
   while(getline(infile,line))
   {
       //check whether line contains any data
       if(line.empty())
           continue;

       istringstream iss(line); //string stream to read data from line
       int task;
       string course;
       double grade;
       iss>>task; //reads task
       iss>>course; //reads course name
       iss>>grade; //reads grade

       //create a new node to store course information
       node* newNode=new node;
       const char* crse=course.c_str();
       newNode->setCourseName((char*)crse);
       newNode->setGrade(grade);
       //sets TYPE
       switch(task)
       {
       case 0:
           newNode->setTask(EXAM);
           break;
       case 1:
           newNode->setTask(HW);
           break;
       case 2:
           newNode->setTask(QUIZ);
           break;
       case 3:
           newNode->setTask(FINAL);
           break;
       }

       //appends the node to gradeList;
       gradeList.append(newNode);
   }
}

//main function to read and load grades from grade.txt
int main() {
   string gradesFilePath="./src/grades.txt"; //modify this as per path on your system
   grades gradeList;
   readAndLoadGrades(gradesFilePath,gradeList);
   return 0;
}

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

8. grades.txt:

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

0 CS1337 89
1 CS2305 95
1 CS1337 100
2 PHY1305 67

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

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