Homework Assignment 2 Due on Sunday 18 March 2018 Write a C++ program that start
ID: 3727185 • Letter: H
Question
Homework Assignment 2 Due on Sunday 18 March 2018 Write a C++ program that starts by reading student information from an input file student.txt organized as follows: each student has four fields of information on one line of the input file: name at most 25 characters, terminated by acharacter), ID (integer), cohort (integer), and three scores (each score is an integer between 0 and 100)·The program must store this information in two parallel arrays. The first array i array is a two-dimensional array of integers with 5 columns for the ID, the cohort and the 3 scores. The program must calculate for each student a letter grade (and store it a third parallel array of characters) based on the average of his/her three scores according to the following criteria: I array of strings for the names. The second s a o Average of 90 80 andExplanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
int compare(const void *a, const void *b) {
int *arr1 = (int*)a;
int *arr2 = (int*)b;
if (arr1[0] < arr2[0])
return 1;
else
return 0;
}
int **arr;
string *name;
char* grade;
int compare1(const void * a, const void * b) {
double diff = arr[*(int*)a] - arr[*(int*)b];
return (0 < diff) - (diff < 0);
}
void SortInfo(int NUM)
{
qsort(arr, NUM, sizeof(*arr), compare);
qsort(name, NUM, sizeof(name), compare1);
qsort(grade, NUM, sizeof(grade), compare1);
}
int SeqSearch(int ID,int NUM)
{
for (int i = 0; i < NUM; i++)
{
if (arr[i][0] == ID)
return i;
}
return -1;
}
int BinSearch(int ID, int NUM)
{
int l = 0, h = NUM - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid][0] == ID)
return mid;
else if (arr[mid][0] > ID)
h = mid - 1;
else
l = mid + 1;
}
return -1;
}
void DisplayStudent(int ID, int NUM)
{
for (int i = 0; i < NUM; i++)
{
if (arr[i][0] == ID)
{
cout << name[i] << " " << arr[i][0] << " " << arr[i][1] << " " << arr[i][2] << " " << arr[i][3] << " " << arr[i][4] << " " << grade[i] << endl;
break;
}
}
}
void PrintReport(int NUM)
{
cout << "NAME ID COHORT GRADE ";
cout << "-------- -------- ----- -- ";
for (int i = 0; i < NUM; i++)
{
cout << name[i] << " " << arr[i][0] << " " << arr[i][1] << " " << arr[i][2] << " " << arr[i][3] << " " << arr[i][4] << " " << grade[i] << endl;
}
}
void ReadInfo()
{
std::string line;
std::ifstream myfile("student.txt");
int r = 0, c = 0;
while (std::getline(myfile, line))
{
string nameValue;
int i = 0;
while (line[i] != '/')
i++;
nameValue = line.substr(0, i);
name[r] = nameValue;
i++;
string ID, cohort, score1, score2, score3;
int j = i;
while (line[i] != ' ')
i++;
ID = line.substr(j, i - 1);
i++;
j = i;
while (line[i] != ' ')
i++;
cohort = line.substr(j, i - 1);
i++;
j = i;
while (line[i] != ' ')
i++;
score1 = line.substr(j, i - 1);
i++;
j = i;
while (line[i] != ' ')
i++;
score2 = line.substr(j, i - 1);
i++;
j = i;
while (line[i] != ' ')
i++;
score3 = line.substr(j, i - 1);
c = 0;
arr[r][c++] = atoi(ID.c_str());
arr[r][c++] = atoi(cohort.c_str());
arr[r][c++] = atoi(score1.c_str());
arr[r][c++] = atoi(score2.c_str());
arr[r][c++] = atoi(score3.c_str());
r++;
}
}
int main()
{
int NUM = 0;
std::string line;
std::ifstream myfile("student.txt");
while (std::getline(myfile, line))
++NUM;
std::cout << "Number of lines in text file: " << NUM;
name = (string*)malloc(sizeof(25)*NUM);
arr = (int **)malloc(NUM * sizeof(int *));
for (int i = 0; i<NUM; i++)
arr[i] = (int *)malloc(5 * sizeof(int));
ReadInfo();
grade = (char*)malloc(sizeof(char)*NUM);
double avg;
for (int i = 0; i < NUM; i++)
{
int sum = 0;
for (int j = 2; j < 4; j++)
{
sum += arr[i][j];
}
avg = sum / 3;
if (avg >= 90)
grade[i] = 'A';
else if (avg >= 80)
grade[i] = 'B';
else if (avg >= 70)
grade[i] = 'C';
else if (avg >= 60)
grade[i] = 'D';
else
grade[i] = 'F';
}
SortInfo(NUM);
PrintReport(NUM);
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.