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

Write a program that will read the input file and check the first letter in the

ID: 3706827 • Letter: W

Question

Write a program that will read the input file and check the first letter in the student's name, if the student's name starts between the letter 'A' or 'a' - 'I' or 'I', you will insert the student's first letter of the name and grade into a single linked list called "FirstList". If the student's name starts between the leter 'J' or 'j' -'Q' or 'q', you will insert the student's first letter in the name and grade into single linked list called "SecondList". If the the student's name starts between 'R' or 'r' - 'Z' or 'z', you will insert the student's first letter in the name and grade is a single linked list called "ThirdList". you will then calculate the average grade of each one of the lists, and print the first letter of the student's names with their grade stored in each one of the lists.

Explanation / Answer

#include<iostream>
#include<string>
#include<ctype.h>
#include<fstream>

using namespace std;

struct node {
   char letter;
   double grade;
   node *next;
};

node *add(node *head, char a, double gr){
     node *q = new node;
     q->letter = a;
     q->grade = gr;
     q->next = NULL;

     if (head == NULL){
        head = q;
     }
     else {
        node *p = head;
        while (p->next!=NULL)
             p = p->next;
        p->next = q;
     }
}

double calcAvg(node *head){

    double sum = 0;
    if (head == NULL){
       return -1;
    }
    else {
       node *p = head;
       int count = 0;
       while(p != NULL){
           sum = sum + p->grade;
           p = p->next;
           count++;
       }
       sum = sum /count;
    }
    return sum;
}

void print(node *head){

    double sum = 0;
    if (head == NULL){
       return;
    }
    else {
       node *p = head;
       int count = 0;
       while(p != NULL){
          cout << p->letter << " " << p->grade << endl;
          p = p->next;
       }
      
    }
   
}


int main(){

    ifstream fin;
    char name[30];
    string nm;
    double gr;
    node *first;
    node *second;
    node *third;

    first = NULL;
    second = NULL;
    third = NULL;

    cout << "Enter filename:";
    cin >> name;
    fin.open(name);
    if (!fin){
       cout << "Error opening file ";
       return 0;
    }
    while (fin >> nm >> gr){
         if (tolower(nm[0]) >= 'a' && tolower(nm[0]) <= 'i'){
             add(first,nm[0],gr);
         }
         if (tolower(nm[0]) >= 'j' && tolower(nm[0]) <= 'q'){
             add(second,nm[0],gr);
         }
         if (tolower(nm[0]) >= 'r' && tolower(nm[0]) <= 'z'){
             add(third,nm[0],gr);
         }

    }
    fin.close();
    print(first);
    cout << "First list average :" << calcAvg(first) << endl;

    print(second);
    cout << "Second list average :" << calcAvg(second) << endl;

    print(third);
    cout << "Third list average :" << calcAvg(third) << endl;

}

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