Create student structure with the following fields: Name (cstring or null-termin
ID: 3698831 • Letter: C
Question
Create student structure with the following fields: Name (cstring or null-terminated character array) Student ID (int- unique random value between 1000 and 9999) grade (char - Values A thru F) birthday (myDate - random value: range 1/1/1998 to 12/31/2001) Home Town (string) Create an array of pointers to students of size 10. Write a function that populates the array with 10 students Write a display function that displays the contents of the array on the screen as shown below - nicely formatted and left justified. The displayed list should be nicely formatted with column names like this: All columns should be left-justified. Name Tom Thumb Fred Flintstone Sponge Bob Student ID Grade Birthday 1002 1995 2987 January 1, 1999 February 3, 1996 June 3, 1997 Home Town Small Ville Bedrock Bikini Bottom Create a menu that shows the following options: 1) Display list sorted by Name 2) Display list sorted by Student ID 3) Display list sorted by Grade 4) Display list sorted by Birthday 5) Display list sorted by Home Town 6) Exit You need to write a sorting function for each of the menu items-5 options needs 5 functions Note: Take advantage of your myDate class that you just wrote. Also, it might be helpful to create a new function that returns a string for the date format: string myDate::toString): To help with formatting, you may want to use the library siomanip> which includes the setw0 function.Explanation / Answer
Solution:
code:
//main.cpp
#include <iostream>
#include "student.h"
#include "studentList.h"
void handleMainMenuInput(int result, StudentList &sList)
{
switch (result) {
case 1:
sList.displayByOriginalValues();
break;
case 2:
sList.displayByStudentId();
break;
case 3:
sList.displayByAge();
break;
case 4:
sList.displayByName();
break;
case 5:
sList.displayByGrade();
break;
case 6:
sList.displayByHomeTown();
break;
case 7:
cout << "Goodbye!" << endl;
exit(0);
break;
default:
break;
}
}
int displayMainMenu(StudentList &sList)
{
int result;
bool resultIsInt = false;
cout << " 1) Display original list" << endl;
cout << "2) Display list sorted by Student ID" << endl;
cout << "3) Display list sorted by Student Age" << endl;
cout << "4) Display list sorted by Name" << endl;
cout << "5) Display list sorted by Grade" << endl;
cout << "6) Display list sorted by Home Town" << endl;
cout << "7) Exit " << endl;
cout << "Choose menu option (1-7): " << endl;
while(!resultIsInt){
resultIsInt = true;
cin >> result;
if(cin.fail() || result > 7){
cin.clear();
cin.ignore();
cout << "Please enter an Integer (1-7) only: " << endl;
resultIsInt = false;
}
}
handleMainMenuInput(result, sList);
return result;
}
int main()
{
StudentList students;
int result = displayMainMenu(students);
while(result < 7){
result = displayMainMenu(students);
}
return 0;
}
-------------------------------------------------------------------------------------------------------------
//studentlist.cpp
#include <ctime>
#include <iostream>
#include <iomanip>
#include <cstring>
#include "studentlist.h"
using namespace std;
StudentList::StudentList()
{
srand ( (unsigned) time(NULL) );
createStudents(cecs282);
for (int i = 0; i < 10; ++i) {
idPtr[i] = &cecs282[i];
namePtr[i] = &cecs282[i];
gradePtr[i] = &cecs282[i];
birthdayPtr[i] = &cecs282[i];
homeTownPtr[i] = &cecs282[i];
};
bubbleSortById(idPtr, 10);
bubbleSortByName(namePtr, 10);
bubbleSortByAge(birthdayPtr, 10);
bubbleSortByGrade(gradePtr, 10);
bubbleSortByHomeTown(homeTownPtr, 10);
}
void StudentList::createStudents(Student students[]){
char sNames[10][11] = {"Peter P.", "Dieter S.", "Steffen A.", "Allen B.", "Steve C.", "Daniel K.", "Chris S.", "Bastian I.", "Leon U.", "Rene N."};
string sHomeTowns[10] = {"Berlin", "Long Beach", "Compton", "Hamburg", "Los Angeles", "Bremen", "Madrid", "Rom", "Paris", "Bikini Bottom"};
int ids[10];
createIds(ids);
for (int i = 0; i < 10; i++) {
students[i].id = ids[i];
strcpy(students[i].name, sNames[i]);
students[i].bDay = createRandomBirthday();
students[i].grade = createRandomGrade();
students[i].homeTown = sHomeTowns[i];
};
}
bool StudentList::contains(int ids[], int value){
for(int i = 0; i < 10; i++){
if(ids[i] == value){
return true;
}
}
return false;
}
void StudentList::createIds(int ids[]){
for (int i = 0; i < 10; ++i) {
int randomNumber = 1000 + ( rand() % ( 9999 - 1000 + 1 ) );
while(contains(ids, randomNumber)){
randomNumber = 1000 + ( rand() % ( 9999 - 1000 + 1 ) );
};
ids[i]= randomNumber;
};
}
MyDate StudentList::createRandomBirthday(){
int year;
int month;
int day;
int j1 = MyDate::jD(1994, 1, 1);
int j2 = MyDate::jD(1998,12, 31);
int randomJd = j1 + ( rand() % ( j2 - j1 + 1 ) );
MyDate::gDate(randomJd, year, month, day);
MyDate result = MyDate(month, day, year);
return result;
}
char StudentList::createRandomGrade(){
char const grades[6] = "ABCDF";
return grades[rand() % 4];
}
void StudentList::displayByHomeTown(){
cout << left << setw(20) << "Student ID" << setw(20) << "Name" << setw(30) << "Birthday" << setw(20) << "Grade" << setw(20) << "Home Town" << endl;
cout << left << setw(20) << "----------" << setw(20) << "----" << setw(30) << "--------" << setw(20) << "-----" << setw(20) << "---------" << endl;
for (int j = 0; j < 10; ++j) {
cout << left << setw(20)<< homeTownPtr[j]->id << setw(20) << homeTownPtr[j]->name << setw(30) << homeTownPtr[j]->bDay.getValue()<< setw(20)<< homeTownPtr[j]->grade << setw(20) << homeTownPtr[j]->homeTown << endl;
}
}
void StudentList::displayByGrade(){
cout << left << setw(20) << "Student ID" << setw(20) << "Name" << setw(30) << "Birthday" << setw(20) << "Grade" << setw(20) << "Home Town" << endl;
cout << left << setw(20) << "----------" << setw(20) << "----" << setw(30) << "--------" << setw(20) << "-----" << setw(20) << "---------" << endl;
for (int j = 0; j < 10; ++j) {
cout << left << setw(20)<< gradePtr[j]->id << setw(20) << gradePtr[j]->name << setw(30) << gradePtr[j]->bDay.getValue()<< setw(20)<< gradePtr[j]->grade << setw(20) << gradePtr[j]->homeTown << endl;
}
}
void StudentList::displayByAge(){
cout << left << setw(20) << "Student ID" << setw(20) << "Name" << setw(30) << "Birthday" << setw(20) << "Grade" << setw(20) << "Home Town" << endl;
cout << left << setw(20) << "----------" << setw(20) << "----" << setw(30) << "--------" << setw(20) << "-----" << setw(20) << "---------" << endl;
for (int j = 0; j < 10; ++j) {
cout << left << setw(20)<< birthdayPtr[j]->id << setw(20) << birthdayPtr[j]->name << setw(30) << birthdayPtr[j]->bDay.getValue()<< setw(20)<< birthdayPtr[j]->grade << setw(20) << birthdayPtr[j]->homeTown << endl;
}
}
void StudentList::displayByName(){
cout << left << setw(20) << "Student ID" << setw(20) << "Name" << setw(30) << "Birthday" << setw(20) << "Grade" << setw(20) << "Home Town" << endl;
cout << left << setw(20) << "----------" << setw(20) << "----" << setw(30) << "--------" << setw(20) << "-----" << setw(20) << "---------" << endl;
for (int j = 0; j < 10; ++j) {
cout << left << setw(20)<< namePtr[j]->id << setw(20) << namePtr[j]->name << setw(30) << namePtr[j]->bDay.getValue()<< setw(20)<< namePtr[j]->grade << setw(20) << namePtr[j]->homeTown << endl;
}
}
void StudentList::displayByStudentId(){
cout << left << setw(20) << "Student ID" << setw(20) << "Name" << setw(30) << "Birthday" << setw(20) << "Grade" << setw(20) << "Home Town" << endl;
cout << left << setw(20) << "----------" << setw(20) << "----" << setw(30) << "--------" << setw(20) << "-----" << setw(20) << "---------" << endl;
for (int j = 0; j < 10; ++j) {
cout << left << setw(20)<< idPtr[j]->id << setw(20) << idPtr[j]->name << setw(30) << idPtr[j]->bDay.getValue()<< setw(20)<< idPtr[j]->grade << setw(20) << idPtr[j]->homeTown << endl;
}
}
void StudentList::displayByOriginalValues(){
cout << left << setw(20) << "Student ID" << setw(20) << "Name" << setw(30) << "Birthday" << setw(20) << "Grade" << setw(20) << "Home Town" << endl;
cout << left << setw(20) << "----------" << setw(20) << "----" << setw(30) << "--------" << setw(20) << "-----" << setw(20) << "---------" << endl;
for (int j = 0; j < 10; ++j) {
cout << left << setw(20)<< cecs282[j].id << setw(20) << cecs282[j].name << setw(30) << cecs282[j].bDay.getValue()<< setw(20)<< cecs282[j].grade << setw(20) << cecs282[j].homeTown << endl;
}
}
void StudentList::swap(Student **x, Student **y){
Student *temp = *x;
*x = *y;
*y = temp;
}
void StudentList::bubbleSortById( Student *values[10], int lenght )
{
int i, j; // counter variables
// interate over the while array beginning at the 1st element
for( i = 0; i < lenght; i++ )
{
// interate over the while array beginning at the last element, looking for the lowest item
for( j = lenght - 1; j > i; j-- )
{
// checks if element is smaller as the previous one
if( values[j]->id < values[j - 1]->id )
{
// swap references
this->swap(&values[j], &values[j-1]);
}
}
}
}
void StudentList::bubbleSortByName( Student *values[10], int lenght )
{
int i, j;
for( i = 0; i < lenght; i++ )
{
for( j = lenght - 1; j > i; j-- )
{
if( strcmp (values[j]->name, values[j-1]->name) < 0)
{
this->swap(&values[j], &values[j-1]);
}
}
}
}
void StudentList::bubbleSortByAge( Student *values[10], int lenght )
{
int i, j;
for( i = 0; i < lenght; i++ )
{
for( j = lenght - 1; j > i; j-- )
{
int j1 = values[j]->bDay.toJd();
int j2 = values[j-1]->bDay.toJd();
if( j1 < j2)
{
this->swap(&values[j], &values[j-1]);
}
}
}
}
void StudentList::bubbleSortByGrade( Student *values[10], int lenght )
{
int i, j;
for( i = 0; i < lenght; i++ )
{
for( j = lenght - 1; j > i; j-- )
{
if(values[j]->grade < values[j-1]->grade)
{
this->swap(&values[j], &values[j-1]);
}
}
}
}
void StudentList::bubbleSortByHomeTown( Student *values[10], int lenght )
{
int i, j;
for( i = 0; i < lenght; i++ )
{
for( j = lenght - 1; j > i; j-- )
{
if(values[j]->homeTown < values[j-1]->homeTown)
{
this->swap(&values[j], &values[j-1]);
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------
//studentList.h
#ifndef STUDENTLIST_H
#define STUDENTLIST_H
#include "student.h"
class StudentList
{
public:
StudentList();
void displayByStudentId();
void displayByName();
void displayByOriginalValues();
void displayByAge();
void displayByGrade();
void displayByHomeTown();
private:
Student cecs282[10];
Student *idPtr[10];
Student *namePtr[10];
Student *gradePtr[10];
Student *homeTownPtr[10];
Student *birthdayPtr[10];
void createStudents(Student students[]);
void createIds(int ids[]);
void swap(Student **x, Student **y);
void bubbleSortById(Student *values[10], int lenght);
void bubbleSortByName(Student *values[10], int lenght);
void bubbleSortByAge(Student *values[], int lenght);
void bubbleSortByGrade(Student *values[], int lenght);
void bubbleSortByHomeTown(Student *values[], int lenght);
bool contains(int ids[], int value);
char createRandomGrade();
MyDate createRandomBirthday();
};
#endif // STUDENTLIST_H
--------------------------------------------------------------------------------------------------
/myDate.cpp
#include<iostream>
#include <string>
#include <sstream>
using namespace std;
MyDate::MyDate()
{
day = 11;
month = 5;
year = 1959;
}
MyDate::MyDate(int m, int d, int y): MyDate()
{
if(isValidDate(m,d,y)){
day = d;
month = m;
year = y;
}
}
void MyDate::display()
{
cout << getMonthName(month) << ' ' << getDay() << ", " << getYear();
}
/*
* Iincrement the date by N days
*/
void MyDate::incrDate(int n)
{
int jd = jD(year, month, day);
jd += n;
gDate(jd, year, month, day);
}
/*
* Decrement the date by N days
*/
void MyDate::decrDate(int n)
{
int jd = jD(year, month, day);
jd -= n;
gDate(jd, year, month, day);
}
/*
* return the number of days between this date and the date D.
*/
int MyDate::daysBetween(MyDate d)
{
int jD1 = jD(year, month, day);
int jD2 = jD(d.year, d.month, d.day);
return jD2 - jD1;
}
/*
*return the month in integer form
*/
int MyDate::getMonth()
{
return month;
}
/*
* return the day of the month
*/
int MyDate::getDay()
{
return day;
}
/*
* return the year
*/
int MyDate::getYear()
{
return year;
}
/*
* return the number of days since the current year began. Example Jan 1 is 1, Feb 1 is 32
*/
int MyDate::dayOfYear()
{
int days = 0;
MyDate temp(month, day, year);
int jd = jD(temp.year,temp.month, temp.day);
while(temp.year == year){
jd--;
temp.gDate(jd, temp.year, temp.month, temp.day);
days++;
}
return days;
}
/*
* returns Monday, Tuesday, etc according to the day of the week.
*/
string MyDate::dayOfWeek()
{
string dayName = "unknown";
int jd = jD(year, month, day);
int modDay = jd % 7;
switch (modDay) {
case 0:
dayName = "Monday";
break;
case 1:
dayName = "Tuesday";
break;
case 2:
dayName = "Wednesday";
break;
case 3:
dayName = "Thursday";
break;
case 4:
dayName = "Friday";
break;
case 5:
dayName = "Saturday";
break;
case 6:
dayName = "Sunday";
break;
default:
break;
}
return dayName;
}
void MyDate::gDate(int jd, int &y, int &m, int &d)
{
int L= jd+68569;
int N= 4*L/146097;
L= L-(146097*N+3)/4 ;
int I= 4000*(L+1)/1461001;
L= L-1461*I/4+31;
int J= 80*L/2447;
int K= L-2447*J/80;
L= J/11;
J= J+2-12*L;
I= 100*(N-49)+I+L;
y= I;
m= J;
d= K;
}
int MyDate::jD(int y, int m, int d)
{
return d-32075+1461*(y+4800+(m-14)/12)/4+367*(m-2-(m-14)/12*12)/12-3*((y+4900+(m-14)/12)/100)/4;
}
int MyDate::toJd(){
return MyDate::jD(year,month, day);
}
/*
* return true or false if date is valid or not
*/
bool MyDate::isValidDate(int m, int d, int y)
{ bool validDay = false;
bool validMonth = false;
bool validYear = false;
if(d >= 1 && d <= 31)validDay = true;
if(m >= 1 && m <= 12)validMonth = true;
if(y >= 1801 && y <= 2099) validYear = true;
return validDay && validMonth && validYear;
}
/*
* return the name of the month
*/
string MyDate::getMonthName(int m)
{
string result = "unknown";
switch (m) {
case 1:
result = "January";
break;
case 2:
result = "February";
break;
case 3:
result = "March";
break;
case 4:
result = "April";
break;
case 5:
result = "May";
break;
case 6:
result = "June";
break;
case 7:
result = "July";
break;
case 8:
result = "August";
break;
case 9:
result = "September";
break;
case 10:
result = "October";
break;
case 11:
result = "November";
break;
case 12:
result = "December";
break;
default:
break;
}
return result;
}
string MyDate::getValue(){
ostringstream result;
result << getMonthName(month) << ' ' << getDay() << ", " << getYear();
return result.str();
}
---------------------------------------------------------------------------------------
//myDate.h
#ifndef MYDATE_H
#define MYDATE_H
#include<string>
#include<iostream>
using namespace std;
class MyDate
{
public:
MyDate();
MyDate(int m, int d, int y);
void display();
void incrDate(int n);
void decrDate(int n);
int daysBetween(MyDate d);
int getMonth();
int getDay();
int getYear();
int dayOfYear();
string dayOfWeek();
static void gDate(int jd, int &y, int &m, int &d);
static int jD(int y, int m, int d);
string getValue();
int toJd();
private:
int day;
int month;
int year;
bool isValidDate(int y, int m, int d);
string getMonthName(int m);
};
#endif // MYDATE_H
------------------------------------------------------------------------------------------------
//student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<string>
#include "myDate.h"
struct Student
{
char name[11];
char grade;
int id;
MyDate bDay;
string homeTown;
};
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.