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

I\'m needing help figuring out how to change the following program so that globa

ID: 3553709 • Letter: I

Question

I'm needing help figuring out how to change the following program so that global variables are no longer used and only local variables are. This needs to be done in as few steps as possible. The struct will still need to be used but of course moved.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <climits>


using namespace std;

const int NUM_STUDENTS = 10;
const int NUM_SCORES = 5;

struct studentType{
int scoress[NUM_SCORES];
string name;
string grade;
float calculateAverage;
};
studentType student[NUM_STUDENTS];

void selectionSort(studentType[]);
void assignGrade(studentType[]);
void convertName(studentType[]);
void getStudentData(studentType student[]);
void calculateAverage(studentType[]);
void printTable(studentType[]);

int main(){


getStudentData(student);
convertName(student);
selectionSort(student);
calculateAverage(student);
assignGrade(student);
printTable(student);

return 0;
}

void getStudentData(studentType student[]){
ifstream fin;
string fileName;

cout << endl << "Enter file name: ";
cin >> fileName;
fin.open(fileName.c_str());
while (!fin){
  cout << "ERROR: Could not open filename " << fileName << endl;
  cout << "Please enter a different filename: ";
  cin >> fileName;
  fin.open(fileName.c_str());
}
for (int i = 0; i<NUM_STUDENTS; ++i)
{
  getline(fin, student[i].name);
  for (int k = 0; k<NUM_SCORES; ++k)
  {
   fin >> student[i].scoress[k];
  }
  fin.ignore(INT_MAX, ' ');
}
fin.close();
}

void convertName(studentType student[NUM_STUDENTS]){
int lmf;
for (int i = 0; i<NUM_STUDENTS; ++i){
  lmf = student[i].name.find(".");
  if (lmf != string::npos) {
   ++lmf;
  }
  else {
   lmf = student[i].name.find(" ");
  }
  student[i].name = student[i].name.substr
   (lmf + 1, student[i].name.size()) + ", " + student[i].name.substr(0, lmf);
}
}

void calculateAverage(studentType student[]){
int final;

for (int i = 0; i<NUM_STUDENTS; ++i){
  final = 0;
  for (int j = 0; j<NUM_SCORES; ++j){
   final += student[i].scoress[j];
  }
  student[i].calculateAverage = float(final) / NUM_SCORES;
}
}

void assignGrade(studentType student[]){

for (int i = 0; i<NUM_STUDENTS; i++){
  if (student[i].calculateAverage >= 90)     student[i].grade = "A";
  else if (student[i].calculateAverage >= 80) student[i].grade = "B";
  else if (student[i].calculateAverage >= 70) student[i].grade = "C";
  else if (student[i].calculateAverage >= 60) student[i].grade = "D";
  else student[i].grade = "F";
}
}

void selectionSort(studentType student[]){
int index, smallestIndex, location;

for (index = 0; index< NUM_STUDENTS - 1; ++index){

  smallestIndex = index;
  for (location = index + 1; location<NUM_STUDENTS; ++location)
  if (student[location].name < student[smallestIndex].name)
   smallestIndex = location;

  swap(student[index].name, student[smallestIndex].name);
  for (int j = 0; j < NUM_SCORES; ++j)
   swap(student[index].scoress[j], student[smallestIndex].scoress[j]);


}
}

void printTable(studentType student[]){
char fill;

cout << setprecision(2) << fixed << showpoint << endl;
cout << left << setw(24) << "Student Name" << right;
for (int j = 0; j < NUM_SCORES; ++j) {
  cout << setw(2) << "s" << j + 1 << ' ';
}
cout << setw(6) << " Avg " << ' ';
cout << setw(6) << "Grade" << endl;
fill = cout.fill('=');
cout << setw(59) << '=' << endl << setfill(fill);
for (int i = 0; i < NUM_STUDENTS; ++i){
  cout << left << setw(25) << setfill(' ') << student[i].name;
  for (int j = 0; j < NUM_SCORES; ++j)
   cout << setw(4) << setfill(' ') << student[i].scoress[j];
  cout << setw(9) << setfill(' ') << student[i].calculateAverage;
  cout << setw(7) << setfill(' ') << student[i].grade;
  cout << endl;
}


}

Below is the text from a sample text program I have been using to test the output.

George A. Wallace
45 99 78 42 80
Betty Bob
68 92 82 65 75
Walter G. Smith
99 91 97 81 90
Tim F. Frankfort
77 75 72 81 60
billy johnson
41 42 99 92 90
Jameson Lewis
89 95 91 60 50
Jill Jackson
14 39 67 89 96
Billy R. Weatherford
87 34 75 80 89
Rusty S. Shackleford
78 82 91 95 94
Marcus Miller
98 95 90 87 82

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <climits>
using namespace std;

struct studentType{
int scoress[5];
string name;
string grade;
float calculateAverage;
};

void getStudentData(studentType (&student)[10]){
ifstream fin;
string fileName;

cout << endl << "Enter file name: ";
cin >> fileName;
fin.open(fileName.c_str());
while (!fin){
cout << "ERROR: Could not open filename " << fileName << endl;
cout << "Please enter a different filename: ";
cin >> fileName;
fin.open(fileName.c_str());
}
for (int i = 0; i<10; ++i)
{
getline(fin, student[i].name);
for (int k = 0; k<5; ++k)
{
   fin >> student[i].scoress[k];
}
fin.ignore(INT_MAX, ' ');
}
fin.close();
}

void convertName(studentType (&student)[10]){
int lmf;
for (int i = 0; i<10; ++i){
lmf = student[i].name.find(".");
if (lmf != string::npos) {
   ++lmf;
}
else {
   lmf = student[i].name.find(" ");
}
student[i].name = student[i].name.substr
   (lmf + 1, student[i].name.size()) + ", " + student[i].name.substr(0, lmf);
}
}

void calculateAverage(studentType (&student)[10]){
int final;

for (int i = 0; i<10; ++i){
final = 0;
for (int j = 0; j<5; ++j){
   final += student[i].scoress[j];
}
student[i].calculateAverage = float(final) / 5;
}
}

void assignGrade(studentType (&student)[10]){

for (int i = 0; i<10; i++){
if (student[i].calculateAverage >= 90)     student[i].grade = "A";
else if (student[i].calculateAverage >= 80) student[i].grade = "B";
else if (student[i].calculateAverage >= 70) student[i].grade = "C";
else if (student[i].calculateAverage >= 60) student[i].grade = "D";
else student[i].grade = "F";
}
}

void selectionSort(studentType (&student)[10]){
int index, smallestIndex, location;

for (index = 0; index< 10 - 1; ++index){

smallestIndex = index;
for (location = index + 1; location<10; ++location)
if (student[location].name < student[smallestIndex].name)
   smallestIndex = location;

swap(student[index].name, student[smallestIndex].name);
for (int j = 0; j < 5; ++j)
   swap(student[index].scoress[j], student[smallestIndex].scoress[j]);


}
}

void printTable(studentType (&student)[10]){
char fill;

cout << setprecision(2) << fixed << showpoint << endl;
cout << left << setw(24) << "Student Name" << right;
for (int j = 0; j < 5; ++j) {
cout << setw(2) << "s" << j + 1 << ' ';
}
cout << setw(6) << " Avg " << ' ';
cout << setw(6) << "Grade" << endl;
fill = cout.fill('=');
cout << setw(59) << '=' << endl << setfill(fill);
for (int i = 0; i < 10; ++i){
cout << left << setw(25) << setfill(' ') << student[i].name;
for (int j = 0; j < 5; ++j)
   cout << setw(4) << setfill(' ') << student[i].scoress[j];
cout << setw(9) << setfill(' ') << student[i].calculateAverage;
cout << setw(7) << setfill(' ') << student[i].grade;
cout << endl;
}
}



int main(){

    studentType student[10];

getStudentData(student);
convertName(student);
selectionSort(student);
calculateAverage(student);
assignGrade(student);
printTable(student);



return 0;
}

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