Goal: Write a program that will make use of your knowledge of pointers to build
ID: 3738434 • Letter: G
Question
Goal: Write a program that will make use of your knowledge of pointers to build a sorted linked list. The program should include the following: 1. 2. Create a struct with the following items: name (string), score (int) and grade (char). Create a node and fill it by doing the following: a. read names from in.dat data file (attached) b. Generate a score for each node from 0 to 100. This field of the struct should be randomly generated by using the rand function. Assign a grade to each node as the following: 0-59: F; 60-69: D; 70-79: C; 80-89: B and 90-100: A. c. 3. Build a linked list of the nodes according to ascending order of score. 4. Print the listing of students/score/grade to the screen in row/column format. There should be proper t so that reader can tell what each column representing for Program Specifications The program you develop must have the following characteristics: It must create the class definition provided with this assignment . It must make used of linked lists based on the assignment specification . It must declare any additional necessary variables required in the program It must be commented adequately o Label the class members o Label each function call o Before each function, give a brief description of what the function will doExplanation / Answer
#include <iostream.h>
using namespace std;
// Structure defination
struct node
{
char *name;
int score;
char grade;
};
// class defination
class list
{
node *head; //declaring start pointer of link list
//constructor function
list()
{
head=NULL;
}
//function will create linked list in ascending order by reading the from in.dat, calculating score by using rand() functionand grade by given condition.
void createlist()
{
ifstream fin(“in.dat”); // Open in.dat file for reading
node *current,*temp;
while(fin) // while end-of-file not been reached
{ temp=new node; // create new node
fin>>temp->name; // read the name from in.dat file
// Generate random number between 0 to 100
temp->score=rand()%100+1;
//calculate grade
if(temp->score>=90)
temp->grade=’A’;
else if(temp->score>=80)
temp->grade=’B’;
else if(temp->score>=70)
temp->grade=’C’;
else if(temp->score>=60)
temp->grade=’D’;
else
temp->grade=’D’;
temp->next=NULL;
//Insert the temp node in ascending order of score
if (head == NULL || (head->score >= temp->score)
{
//when linked list is empty or new node to be inserted before first node
temp->next = head;
head = temp;
}
else
{
/* Locate the node before the point of insertion */
current = head;
while (current->next!=NULL &&
current->next->data < temp->data)
{
current = current->next;
}
temp->next = current->next;
current->next = temp;
delete temp; // free temp node;
}
} // End of while
} // end of function
// Printing the listing of students name,score and grade on screen in row and column format
void display()
{
node *temp=new node;
temp=start;
while(temp!=NULL)
{
cout<<”NAME”<<" "<<”SCORE”<<" "<<”Grade”<<" ";
cout<<temp->name<<" "<<temp->score<<" "<<temp->grade<<" ";
temp=temp->next;
}
} // end of display() function
}// end class defination
void main()
{
list l;
l.createlist(); // calling function for list creation
l.display(); // calling function for display
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.