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

Write a program that reads from the “studentData2.txt” file and puts the data in

ID: 3573774 • Letter: W

Question

Write a program that reads from the “studentData2.txt” file and puts the data into these parallel arrays. The “studentData2.txt” file is attached bellow.

? the students’ names in an array called nameArray.

? the students’ majors in an array called majorArray

? the students’ GPA in an array called gpaArray

The arrays are populated in the order the data is read from the file. For example, the first name read from the file is assigned to the first element of the nameArray, the second name read is assigned to the second element of the nameArray, and so on. The other arrays are populated in the same fashion. Note the file contains a student’s name, followed by the student’s major, followed by the student’s GPA (you do not have to type this data, the file will be posted bellow):

Kai CS 3.8
Alice ME 3.9
Seppo CE 3.2
Ken BME 3.0
Anna EE 3.8

Make sure when you read from the file, you store the items in the arrays in the proper order. The program then uses a loop to display the students names, another loop to display the students majors, and another loop to display the students’ GPAs. All the data is displayed in the order they are in the arrays. Then the program displays a menu of choices. As long as the user does not choose to quit, the program loops over the menu display

1) Search for the lowest GPA (the program then displays the GPA, name and major)

2) Search for the highest GPA (the program then displays the GPA, name and major)

3) Search on a student’s name (the program displays the name, GPA and major if found, or “Not found”)

4) Quit

Additional requirements – Make sure you meet all the requirements to avoid losing points

. You are required to implement your program with at least 3 functions that are called in the main() function.

Below are suggested functions and main function pseudocode:

? getUserChoice: displays the menu, prompts the user to enter the choice, performs input validation, displays the menu again if the input is invalid. Returns the user’s choice if it is valid

? searchLowestGPA: takes as arguments the gpaArray and its size, and returns the index of the lowest GPA. For example, if gpaArray[2] is the lowest element of the gpaArray, the function returns 2.

? searchHighestGPA: takes as argument the gpaArray and its size, and returns the index of the highest GPA

? searchName: takes as arguments a name, the nameArray and its size, and returns the index of the nameArray element that matches the name argument. Returns -1 if no match

? display: takes as argument an index and the nameArray, majorArray and gpaArray, and displays the elements of nameArray, majorArray and gpaArray at the index

?? Your main() function implements the following pseudocode Open file Read from the file and populate the arrays Display the nameArray Display the majorArray Display the gpaArray While user does not quit call getUserChoice depending on the user’s choice, call the appropriate function, and display the result

Example Output Names array name Array C01 Kai name Array C 11 Alice name Array [2] Seppo name Array [3] Ken name Array C4] Anna Major array major Array C01 CS ma Array[1] ME major Array C21 CE major Array C3] BME major Array[4] EE GPA. array gpaArray CO] 3.8 gpaArray C11 3.9 gpaArray C21 3.2 gpaArray[3] 3 gpaArray[4] 3,8 Enter your choice 1 Search for the lowest GPA 2 Search for the highest GPA Search on a student's name Quit Name is Ken major is BME GPA 3 Enter your choice Search for the lowest GPA Search for the highest GPA 3 Search on a student 's name Quit Name is Alice major is ME, GPA 3.9 Entege FR01ERe lowest GPA Search for the highest GPA Search on a student's name Quit Input name sep Name not found lowest GPA Enter your choice Search for the highest GPA 3 Search on a student 's name Quit Input name Seppo Name is Seppo major is CE, GPA 3.2 your choice owest GPA. Search for the 3 Search on a student 's name Quit Invalid choice, reenter Press any key to continue

Explanation / Answer

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstring>
using namespace std;
//Reads a file and returns number of record
int readFile(char nA[][10], char mA[][10], double gpa[])
{
ifstream Rfile;
int n = 0;
//Opens studentData2.txt file for reading
Rfile.open("StudentData.txt");
//Check file can be open or not
if (Rfile.is_open())
{
//Reads data till end of file
while (!Rfile.eof()) //Checks end of file
{
//Reads and stores data in array
Rfile >> nA[n];
Rfile>>mA[n];
Rfile>>gpa[n];
//Increases the record count
n++;
}
}
//File not found error
else
{
cout<<"ERROR: Data file not found.";
exit(0);
}
//Close file
Rfile.close();
return n;
}
//Displays the student information
void Show(char nA[][10], char mA[][10], double gpa[], int no)
{
cout<<" STUDENT INFORMATION ";
cout<<" Names Array ";
cout<<" --------------";
for(int r = 0; r < no - 1; r++)
cout<<" nameArray["<<r<<"] = "<<nA[r];

cout<<" Major Array ";
cout<<" --------------";
for(int r = 0; r < no - 1; r++)
cout<<" majorArray["<<r<<"] = "<<mA[r];

cout<<" GPA Array ";
cout<<" --------------";
for(int r = 0; r < no - 1; r++)
cout<<" gpaArray["<<r<<"] = "<<gpa[r];
}
//Returns the user choice
int getUserChoice()
{
int ch;
cout<<" Enter Your Choice: ";
cout<<" 1 - Search for lowest GPA";
cout<<" 2 - Search for highest GPA";
cout<<" 3 - Search on a Student's name";
cout<<" 4 - Quit ";
cin>>ch;
return ch;
}
//Returns the index position of the lowest GPa
int searchLowestGPA(double gpa[], int n)
{
int pos = -1;
double small = gpa[0];
for(int x = 1; x < n - 1; x++)
{
if(gpa[x] < small)
{
small = gpa[x];
pos = x;
}
}
return pos;
}
//Returns the index position of the highest GPa
int searchHighestGPA(double gpa[], int n)
{
int pos = -1;
double big = gpa[0];
for(int x = 1; x < n - 1; x++)
{
if(gpa[x] > big)
{
big = gpa[x];
pos = x;
}
}
return pos;
}
//Returns the index position of the name found otherwise -1
int searchName(char sN[], char nA[][10], int no)
{
int po = -1;
for(int x = 0; x < no - 1; x++)
{
if(strcmp(sN, nA[x]) == 0)
{
po = x;
break;
}
}
return po;
}
int main()
{
char nameArray[10][10], majorArray[10][10], searName[10];
double gpaArray[10];
int noOfRecord, ch, po;
//Call to read file
noOfRecord = readFile(nameArray, majorArray, gpaArray);
//call to display method
Show(nameArray, majorArray, gpaArray, noOfRecord);
//Loops till user wish
do
{
//accepts the user choice
ch = getUserChoice();
switch(ch)
{
case 1:
po = searchLowestGPA(gpaArray, noOfRecord);
cout<<" Name is "<<nameArray[po]<<". Major is "<<majorArray[po]<<". GPA = "<<gpaArray[po];
break;
case 2:
po = searchHighestGPA(gpaArray, noOfRecord);
cout<<" Name is "<<nameArray[po]<<". Major is "<<majorArray[po]<<". GPA = "<<gpaArray[po];
break;
case 3:
cout<<" Input Name: ";
cin>>searName;
po = searchName(searName, nameArray, noOfRecord);
if(po == -1)
cout<<" Name not found";
else
cout<<" Name is "<<nameArray[po]<<". Major is "<<majorArray[po]<<". GPA = "<<gpaArray[po];
break;
case 4:
exit(0);
break;
default:
cout<<" Invalid Choice - Re - Enter ";
}
}while(1);
}

Output:

STUDENT INFORMATION


Names Array
--------------
nameArray[0] = Kai
nameArray[1] = Alice
nameArray[2] = Seppo
nameArray[3] = Ken
nameArray[4] = Anna

Major Array
--------------
majorArray[0] = CS
majorArray[1] = ME
majorArray[2] = CE
majorArray[3] = BME
majorArray[4] = EE

GPA Array
--------------
gpaArray[0] = 3.8
gpaArray[1] = 3.9
gpaArray[2] = 3.2
gpaArray[3] = 3
gpaArray[4] = 3.8
Enter Your Choice:
1 - Search for lowest GPA
2 - Search for highest GPA
3 - Search on a Student's name
4 - Quit
1

Name is Ken. Major is BME. GPA = 3
Enter Your Choice:
1 - Search for lowest GPA
2 - Search for highest GPA
3 - Search on a Student's name
4 - Quit
2

Name is Alice. Major is ME. GPA = 3.9
Enter Your Choice:
1 - Search for lowest GPA
2 - Search for highest GPA
3 - Search on a Student's name
4 - Quit
3

Input Name: sop

Name not found
Enter Your Choice:
1 - Search for lowest GPA
2 - Search for highest GPA
3 - Search on a Student's name
4 - Quit
3

Input Name: Seppo

Name is Seppo. Major is CE. GPA = 3.2
Enter Your Choice:
1 - Search for lowest GPA
2 - Search for highest GPA
3 - Search on a Student's name
4 - Quit
9

Invalid Choice - Re - Enter
Enter Your Choice:
1 - Search for lowest GPA
2 - Search for highest GPA
3 - Search on a Student's name
4 - Quit
4

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