Question

In: Computer Science

***IN C++*** Create student structure with the following fields:  Name (cstring or null-terminated character array)...

***IN C++***

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/2000 to 12/31/2005)

Home Town (string)

Create an array of pointers to students of size 10.

Example: Student *stuPtr[10];

Write a function that populates the array with 10 students.

Example: populate(stuPtr);

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

Student ID

Grade

Birthday

Home Town

Tom Thumb

1002

C

January 1, 2002

Small Ville

Fred Flintstone

1995

D

February 3, 2003

Bedrock

Sponge Bob

2987

B

June 3, 2001

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:

You must create a function that returns a date between a range of 2 dates.

You will use the myDate class in this program – you will not create any other class. The Student

structure is NOT a class.

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( );

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

myDate.cpp

#include "myDate.h"
#include <iostream>
#include <sstream>
#include <cstdlib>

using namespace std;

//convert gregorian date to julian date
int Greg2Julian(int month, int day, int year)
{
   //declare local variables
   int I = year;
   int J = month;
   int K = day;
   int JD= K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4;
   //return julian
   return JD;
}

//convert julian date to gregorian date
void Julian2Greg(int JD, int & month, int & day, int & year)
{
   //declare local variables
   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;

   year = I;
   month = J;
   day = K;
}

//constructor
myDate::myDate()
{
   month = 5;
   day = 11;
   year = 1959;
}
//myDate constructor with parameters
myDate::myDate(int M, int D, int Y)
{
   // calling functions
   JD = Greg2Julian(M, D, Y);
   Julian2Greg(JD, month, day, year);

   //using if loop
   if (month == M && day == D && year == Y);
   else
   {
       month = 5;
       day = 11;
       year = 1959;
       JD = Greg2Julian(month, day, year);
   }
}
//display method used to display month, day and year
void myDate::display()
{
   //declare month usinng array
   string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   //display month, day and year
   cout << months[month - 1] << " " << day << ", " << year;
}

//randomly displaying start date and end date
void myDate::random()
{
   myDate start = myDate(01, 01, 1999);
   myDate end = myDate(12, 31, 2004);
   this->JD = (rand() % start.daysBetween(end)) + start.JD;
   Julian2Greg(JD, month, day, year);
}

//display month, day and year
string myDate::toString()
{
   string months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
   stringstream buffer;
   buffer << months[month - 1] << " " << day << ", " << year;
   return buffer.str();
}

//increasing date
void myDate::increaseDate(int N)
{
   JD += N;
   Julian2Greg(JD, month, day, year);
}

//decreasing date
void myDate::decreaseDate(int N)
{
   JD -= N;
   Julian2Greg(JD, month, day, year);
}

//calculating inbetween day, month and year
int myDate::daysBetween(myDate D)
{
   JD = Greg2Julian(month, day, year);
   return Greg2Julian(D.month, D.day, D.year) - JD;
}

//getting month
int myDate::getMonth()
{
   return month;
}
//getting day
int myDate::getDay()
{
   return day;
}

//getting year
int myDate::getYear()
{
   return year;
}

int myDate::dayOfYear()
{
   return JD - Greg2Julian(1, 1, year) + 1;
}

//display day name
string myDate::dayName()
{
   string days[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
   return days[JD % 7];
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

myDate.h

#ifndef MyDate_h
#define MyDate_h

#include <string>

using namespace std;

// class name as myDate
class myDate
{
public:
  
   //declare functions as public
   myDate();
   myDate(int M, int D, int Y);

   void display();
   void random();
   string toString();
   void increaseDate(int N);
   void decreaseDate(int N);
   int daysBetween(myDate D);
   int getMonth();
   int getDay();
   int getYear();
   int dayOfYear();
   string dayName();

private:
   //declare function as private
   int month, day, year;
   int JD;
};
#endif

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

main.cpp

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include "myDate.h"
#include <string.h>
#include <cstdlib>
#include <cstring>
using namespace std;

//creating a structure called student
struct Student
{
   //declare variables
   char name[20];
   int id;
   char grade;
   myDate bday;
   string homeTown;
};

// function prototypes
void populateStudents(Student *students[]);
void sortName(Student *students[]);
void sortID(Student *students[]);
void sortGrade(Student *students[]);
void sortBday(Student *students[]);
void sortTown(Student *students[]);

//main function
int main()
{
   //declare pointer array variable
   Student *students[10];
   //calling function
   populateStudents(students);

   //display following details using while loop
   while (true)
   {
       cout << setw(20) << left << "Name";
       cout << setw(13) << left << "Student ID";
       cout << setw(10) << left << "Grade";
       cout << setw(20) << left << "Birthday";
       cout << setw(10) << left << "Home Town" << endl;

       for (int i = 0; i < 10; i++)
       {
          
           cout << setw(20) << left << students[i]->name;
           cout << setw(13) << left << students[i]->id;
           cout << setw(10) << left << students[i]->grade;
           cout << setw(20) << left << students[i]->bday.toString();
           cout << setw(10) << left << students[i]->homeTown << endl;
       }

       //display menu options
       cout << "\nWelcome to Solitaire Prime!\n"
           << "1) Display list sorted by Name\n"
           << "2) Display list sorted by Student ID\n"
           << "3) Display list sorted by Grade\n"
           << "4) Display list sorted by Birthday\n"
           << "5) Display list sorted by Home Town\n"
           << "6) Exit\n";
      
       //getting menu option from user
       int i;
       cin >> i;

       //checking the condition using switch loop
       switch (i)
       {
       case 1:
           sortName(students);
           break;
       case 2:
           sortID(students);
           break;
       case 3:
           sortGrade(students);
           break;
       case 4:
           sortBday(students);
           break;
       case 5:
           sortTown(students);
           break;
       case 6:
           exit(0);

       default:
           cout << "Invalid input";
           break;
       }
   }
    return 0;
}


void populateStudents(Student *students[])
{
   char grades[] = { 'A', 'B', 'C', 'D', 'F' };
   string names[] = { "Clark Kent", "Fred Flintstone", "Sponge Bob", "John Smith", "Jeff Bezos", "Bill Gates", "Bernie Sanders", "Gabe Newell", "Nathanael Gastelum", "Hank Hill" };
   string towns[] = { "Los Angeles", "Barcelona", "San Francisco", "New York", "London", "Jakarta", "Hong Kong", "Singapore", "Long Beach", "Chicago" };

   for (int i = 0; i < 10; i++)
   {
       students[i] = new Student;

       strcpy(students[i]->name, names[i].c_str());
       students[i]->id = rand() % 9000 + 1000;
       students[i]->grade = grades[rand() % 5];

       students[i]->bday = myDate();
       students[i]->bday.random();

       students[i]->homeTown = towns[i];
   }
}

//sorting student name
void sortName(Student *students[])
{
   for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 10; j++)
       {
           if (strcmp(students[i]->name, students[j]->name) < 0)
           {
               Student *temp = students[i];
               students[i] = students[j];
               students[j] = temp;
           }
       }
   }
}
//sorting student id
void sortID(Student *students[])
{
   for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 10; j++)
       {
           if (students[i]->id < students[j]->id)
           {
               Student *temp = students[i];
               students[i] = students[j];
               students[j] = temp;
           }
       }
   }
}

//sorting student grade
void sortGrade(Student *students[])
{
   for (int i = 0; i < 10; i++)
   {
       for (int j = 0; j < 10; j++)
       {
           if (students[i]->grade < students[j]->grade)
           {
               Student *temp = students[i];
               students[i] = students[j];
               students[j] = temp;
           }
       }
   }
}
//sorting birthday
void sortBday(Student *students[])
{
   for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 10; j++)
       {
           if (students[i]->bday.daysBetween(students[j]->bday) > 0)
           {
               Student *temp = students[i];
               students[i] = students[j];
               students[j] = temp;
           }
       }
   }
}
//sorting town
void sortTown(Student *students[])
{
   for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 10; j++)
       {
           if (students[i]->homeTown.compare(students[j]->homeTown) < 0)
           {
               Student *temp = students[i];
               students[i] = students[j];
               students[j] = temp;
           }
       }
   }
}

Related Solutions

Create a structure array that contains the following information fields concerning the road bridges in a town
Create a structure array that contains the following information fields concerning the road bridges in a town: bridge location, maximum load (tons), year built, year due for maintenance. Then enter the following data into the array:
Part 1: Create a character array and save your first and last name in it
PROGRAMMING IN C:Part 1:Create a character array and save your first and last name in itNote: You can assign the name directly or you can use the scanf function.Display your name on the screen.Display the address (memory location) in hexadecimal notation of the array. (hint: use %p)Use a for loop to display each letter of your name on a separate line.Part 2:Create a one dimensional array and initialize it with 10 integers of your choice.Create a function and pass the...
You need a data structure that contains fields for name (char array), color (char array), age...
You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it. Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following: What is your name? George What is your favorite color? Green What is your age? 111 What is your height in inches? 72...
Student Structure Assignment Create a Student Structure globally consisting of student ID, name, completed credits, and...
Student Structure Assignment Create a Student Structure globally consisting of student ID, name, completed credits, and GPA. Define one student in main() and initialize the student with data. Display all of the student’s data on one line separated by tabs. Create another student in main(). Assign values to your second student (do not get input from the user). Display the second student’s data on one line separated by tabs. Create a third student in main(). Use a series of prompts...
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the...
Using cs Cstring and c character as arguments in functiuon strfind(cs,c). The function should return the index of wanted letter in string. For example, strfind("hello world", 'o') would return 4. If the character is not found, the funtion returns -1. Please code in c++
language c++(Data structure) You have to read a file and store a data in character array...
language c++(Data structure) You have to read a file and store a data in character array ( you cant initialize a character array, you have to code generically) code must be generic u must read a file onece u cant use built in function etc string, code in classes if u initialized a char array or ur code doesn't run i will dislike and report u you can use link list to store data
C# (Thank you in advance) Create an Employee class with five fields: first name, last name,...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT