So far, I have the program to read in the max number of points, students name, a
ID: 673215 • Letter: S
Question
So far, I have the program to read in the max number of points, students name, and total grade. I have inserted the names and grades into a link list. The names and grades are sorted in named order currently. Now, I need to have a link print the grades in sorted order. Also, I need a 3rd link in the same link list to of all students who have a grade below 200.
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream inFile; //declare file stream
ofstream outputFile;
//define struct
class LList
{
public:
string Name;
int Number;
LList *next;
};
typedef LList *Node;
Node ListHead;
//define a newNode
LList *newNameNode()
{
LList * temp;
temp = new LList();
temp -> Name = 'a';
temp -> next = NULL;
return temp;
}
LList *newGradeNode()
{
LList * temp;
temp = new LList();
temp -> Number = 0;
temp -> next = NULL;
return temp;
}
// print list function
void printList(int max)
{
Node c;
c = ListHead; //assign c to head
int num;
int answer;
while ( c )
{
outputFile << left << c -> Name << setw(18);
outputFile << left << c -> Number * 100 / max << setw(15);
outputFile << left << c -> Number << setw(15);
outputFile << endl;
c = c -> next;
}
}
//insert a node to link list
void insert(string v, int n1)
{
Node c, p, temp;
c = ListHead;
p = NULL;
string s; //declare string variables
int g;
s = v;
g = n1;
//place items in ordered link list on item Name
while ( c != NULL && c -> Name <= s)
{
p = c;
c = c -> next;
}
if(p == NULL)
{
temp = newNameNode();
temp -> Name = s;
temp -> Number = g;
temp -> next = c;
ListHead = temp;
}
else if(s != p->Name)
{
temp = newNameNode();
temp -> Name = s;
temp -> Number = g;
p -> next = temp;
temp -> next = c;
}
}
int main()
{
//open input and output files
inFile.open("LinkLnamesAndGrades.txt");
outputFile.open("answer.txt");
int n1;
string v;
int max;
inFile >> max;
while(inFile >> v )
{
inFile >> n1;
insert(v, n1);
}
printList(max); //call print list function
//close the files
inFile.close();
outputFile.close();
return 0;
}
Input:
300
Taylor 287
Thomas 197
Brown 250
Lee 273
Reynolds 282
Lion 192
Mack 265
Lewis 176
Output:
Brown 83 250
Lee 91 273
Lewis 58 176
Lion 64 192
Mack 88 265
Reynolds 94 282
Taylor 95 287
Thomas 65 197
Explanation / Answer
Take thiis as Example program:
public class MyInsertionSort {
public static void main(String[] args) {
int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
insertionSort(input);
}
private static void printNumbers(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println(" ");
}
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.