- Create an array of structure which will be used to store student information.
ID: 3854982 • Letter: #
Question
- Create an array of structure which will be used to store student information.
- The size of array of structure should be 15, but will only be partially filled.
- Structure to be used
struct StudentInfo
{
string firstName;
int id;
unsigned long int phoneNum;
string homeState;
int birthYear;
double gpa;
};
- Read in student information from an input file (file name: lab2_input.txt) and store the information in the array of structure.
- Input file format:
Bob 12345 4193215984 OH 1988 3.60 Will 32158 8052584455 FL 1998 3.66 …
- Assume that we don't know the number of students shown in the file. (But it will be less than 15.)
- After reading in all the information, create and call a function (function name: showStudentInfo( )) that displays the student information as shown below.
(Hint: function prototype to be used
void showStudentInfo(const StudentInfo [], int numEle); ) NAME ID PHONE STATE Birth Year GPA Bob 12345 4193215984 OH 1988 3.60 Will 32158 8052584455 FL 1998 3.66 … … … … … … - Create and call a function (function name: findOldestStudent( )) that returns the oldest student information. The function should return a structure. The return structure information should be displayed on the screen.
(Hint: function prototype to be used StudentInfo findOldestStudent(const StudentInfo [], int numEle); )
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct StudentInfo
{
string firstName;
int id;
unsigned long int phoneNum;
string homeState;
int birthYear;
double gpa;
};
void showStudentInfo(const StudentInfo []);
StudentInfo findOldestStudent(const StudentInfo []);
int main();
{
const int STUDENT_SIZE = 15;
StudentInfo stuList[STUDENT_SIZE];
int num_students = 0;
ifstream File;
File.open("lab02_input.txt");
while (!File.eof())
{
File >> stuList[num_students].firstName;
File >> stuList[num_students].id;
File >> stuList[num_students].phoneNum;
File >> stuList[num_students].homeState;
File >> stuList[num_students].birthYear;
File >> stuList[num_students].gpa;
num_students++;
}
showStudentInfo(StudentInfo, num_students);
}
void showStudentInfo(const StudentInfo[], int num_students)
{
int num = 0;
cout << "----" << "Name" << "----" << "ID" << "----" << "Phone State" << "----" << "Birth Year" << "----" << "GPA" << endl;
for (num = 0; num < num_students; num++);
{
cout << "----" << stuList[STUDENT_SIZE].firstName << "----" << stuList[STUDENT_SIZE].id << "----" << stuList[STUDENT_SIZE].phoneNum << "----" << stuList[STUDENT_SIZE].homeState << "----" << stuList[STUDENT_SIZE].birthYear << "----"
<< stuList[STUDENT_SIZE].gpa << "----" << endl;
}
}
struct findOldestStudent(const StudentInfo[], const int STUDENT_SIZE, int num_students)
{
Please tell me how should I go futher that this.
And then:-
* After the oldest student information is displayed on the screen, create and call a function (function name: sortStudentID( )) that sort the student information by the student ID (i.e., in increasing order).
* Hint: function prototype to be used --> void sortStudentID(StudentInfo [ ], int);
* Then, display the contents of the student information using showStudentInfo( ) (which was a part of the Lab 02 assignment).
THE ABOVE ONE IS LAB 2
Explanation / Answer
The answer is as follows:
The code is as follows:
Function for finding the oldest student is as follows:
StudentInfo findOldestStudent(const StudenInfo list[], int num_students){
int birth_year;
int index;
birth_year = list[0].birthYear;
for(int i = 0; i<num_students; i++){
if (birth_year > list[i].birthYear){
birth_year = list[i].birthYear;
index = i;
}
}
return list[index];
}
Function for sorting is as follows:
void sortStudentID(StudentInfo list[], int num_students){
StudentInfo temp;
for(int i = 0; i<num_students; i++){
for(int j = 0; j<num_students - i -1; j++){
if (list[j].id > list[j+1].id){
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
}
Function for showing the content of student list is as follows:
void showStudentInfo(const StudentInfo stuList[], int num_students)
{
int num = 0;
cout << "----" << "Name" << "----" << "ID" << "----" << "Phone" << "----" << "State" << "----" << "Birth Year" << "----" << "GPA" << endl;
for (num = 0; num < num_students; num++)
{
cout << "----" << stuList[num].firstName << "----" << stuList[num].id << "----" << stuList[num].phoneNum << "----" << stuList[num].homeState << "----" << stuList[num].birthYear << "----" << stuList[num].gpa << "----" << endl;
}
}
Main function modification is as follows:
int main();
{
const int STUDENT_SIZE = 15;
StudentInfo stuList[STUDENT_SIZE];
int num_students = 0;
StudentInfo stu;
ifstream File;
File.open("lab02_input.txt");
while (!File.eof())
{
File >> stuList[num_students].firstName;
File >> stuList[num_students].id;
File >> stuList[num_students].phoneNum;
File >> stuList[num_students].homeState;
File >> stuList[num_students].birthYear;
File >> stuList[num_students].gpa;
num_students++;
}
stu = findOldestStudent(stulist, num_students);
cout << "----" << "Name" << "----" << "ID" << "----" << "Phone" << "----" << "State" << "----" << "Birth Year" << "----" << "GPA" << endl;
cout << "----" << stu.firstName << "----" << stu.id << "----" << stu.phoneNum << "----" << stu.homeState << "----" << stu.birthYear << "----"<< stu.gpa << "----" << endl;
sortStudentID(stuList, num_students);
showStudentInfo(stuList, num_students);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.