Question

Define a structure called Date with month, day and year as its only data members, Time...

Define a structure called Date with month, day and year as its only data members, Time with hour and minute as its only members and Event with desc (a c-string for description of maximum size 80), date (of type Date) and time (of type Time).

Write a readEvents() function that will keep asking the user if he or she wants to add a new event. As long as the user answers with a 'y' or 'Y', allocate memory dynamically for an Event structure, storing the pointer returned by new in the array of Event pointers and then read the Event data from the keyboard and store them in the Event just created. Dates should be entered like 5/21/20 and times as 10:15. When finished entering the Events, the function will return the number of Events entered.

Write a sort-desc() function to sort the Events by their description by rearranging the pointers (rather than the structures), by passing it the array of pointers and the number of Events created. Use a swapPtrs() function to swap pointers.

Write another sort-date() function that takes the Events array and size and sorts them by date.

Write a linear search function that takes the array of sorted Event pointers, array size and a string and displays the first Event it finds whose description contains the specified string. Consider using strstr() function to do linear search for a c-string within another c-string:

if(strstr(events[i] -> desc, searchStr)) //search for searchStr string in events[i] -> desc

Write a binary search function that takes a month, as well as the array of pointers sorted by date and the array size and searches for and displays all Events found for the specified month. If not found, notify the user that it was not found.

Declare an array of Event pointers of MAX (50) size in main and pass it and the MAX size to readEvents function.

Call display() function from main that takes the array of Event pointers and number of Events created and prints the Events entered. Print Events like so:

Im having trouble with my code working. I have writeen it far enough to where it runs but crashes 1/2 way through can anyone help? Any help is appreciated!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string.h>

using namespace std;

struct Date{
int month;
int day;
int year;
};

struct Time{
int hour;
int min;
};


struct Event{
char desc[80];
Date myDate;
Time myTime;
};

int readEvents(Event* myEvents[], int max){
char choice = 'n';
int i= 0;

char slash_dummy;
char colon;

while(true && i < max){
cout << "Create an event [y/n]? ";
cin >> choice;

if( choice == 'y' || choice == 'Y'){
myEvents[i] = new Event;

cout << "Enter the description:\n";
cin.ignore();
cin.getline(myEvents[i]->desc,sizeof(myEvents[i]->desc));

cout << "Enter date (MM/DD/YY) \n";
cin >> myEvents[i]->myDate.month >> slash_dummy >> myEvents[i]->myDate.day >> slash_dummy >> myEvents[i]->myDate.year;

cout << "Enter time: \n";
cin >> myEvents[i]->myTime.min >> colon >> myEvents[i]->myTime.hour;


}
else{
break;
}


i++;

}

return i;
}

void display(Event* myEvents[], int eventCount){
for(int i=0; i<eventCount; i++){
cout << myEvents[i]->desc << endl;
cout << myEvents[i]->myDate.month << "/" << myEvents[i]->myDate.day << "/" << myEvents[i]->myDate.year << " " << myEvents[i]->myTime.min << ":" << myEvents[i]->myTime.hour << endl;
}
}

//sort by description
void sortByDesc(Event* myEvents[], int eventCount){
bool swapped;

do{
swapped = false;
for(int i=0; i<eventCount-1; i++){
if( strcmp(myEvents[i]->desc, myEvents[i+1]->desc) > 0){
swap(myEvents[i], myEvents[i+1]);
swapped = true;
}
}
}while (swapped);

}

//sort by description
void sortByDate(Event* myEvents[], int eventCount){
bool swapped;

do{
swapped = false;
for(int i=0; i<eventCount-1; i++){
if( (myEvents[i]->myDate.day > myEvents[i+1]->myDate.day) &&
(myEvents[i]->myDate.month > myEvents[i+1]->myDate.month) &&
(myEvents[i]->myDate.year > myEvents[i+1]->myDate.year) ){
swap(myEvents[i], myEvents[i+1]);
swapped = true;
}
}
}while (swapped);
}

void linSearch(Event* myEvents[], int eventCount, char* searchStr){
for (int i = 0; i < eventCount; i++)
if (strstr(myEvents[i]->desc, searchStr) != NULL){
cout << myEvents[i]->desc << endl;
cout << myEvents[i]->myDate.month << "/" << myEvents[i]->myDate.day << "/" << myEvents[i]->myDate.year << " " << myEvents[i]->myTime.min << ":" << myEvents[i]->myTime.hour << endl;
}

}

void binSearch(Event* myEvents[], int eventCount, int month){
int l = 0;
int r = eventCount-1;

while (l <= r)
{
int m = l + (r-l)/2;

// Check if month is present at mid
if (myEvents[m]->myDate.month == month){
cout << myEvents[m]->desc << endl;
cout << myEvents[m]->myDate.month << "/" << myEvents[m]->myDate.day << "/" << myEvents[m]->myDate.year << " " << myEvents[m]->myTime.min << ":" << myEvents[m]->myTime.hour << endl;
}


// If Month greater, ignore left half
if (myEvents[m]->myDate.month < month)
l = m + 1;

// If Month is smaller, ignore right half
else
r = m - 1;
}
}

int main()
{
Event* myEvents[50];
char str[200];
int total = readEvents(myEvents, 50);
int month;

cout << "Events entered:\n";
display(myEvents, total);

cout << "Events sorted by description:\n";
sortByDesc(myEvents, total);
display(myEvents, total);

cout << "Enter a search string: ";
cin.ignore();
cin.getline(str,sizeof(str));
linSearch(myEvents, total, str);

cout << "Events sorted by Date:\n";
sortByDate(myEvents, total);
display(myEvents, total);

cout << "Enter a month to list Events for: ";
cin >> month;
binSearch(myEvents, total, month);
return 0;
}Create an event [y/n]? y Enter the description: Submit 839 assignment by Enter date (MM/DD/YY) 11/30/2018 Enter time: 23:59 C

Hope this may help you

Thank you ??

Add a comment
Know the answer?
Add Answer to:
Define a structure called Date with month, day and year as its only data members, Time...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • An ordinal date consists of a year and a day within it, both of which are...

    An ordinal date consists of a year and a day within it, both of which are integers. The year can be any year in the Gregorian calendar while the day within the year ranges from one, which represents January 1, through to 365 (or 366 if the year is a leap year) which represents December 31. Ordinal dates are convenient when computing differences between dates that are a specific number of days (rather than months). For example, ordinal dates can...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Please post screenshots only File Tools View CMSC 140 Common Projects Spring 2019(1) E - 3...

    Please post screenshots only File Tools View CMSC 140 Common Projects Spring 2019(1) E - 3 x Project Description The Powerball game consists of 5 white balls and a red Powerball. The white balls represent a number in the range of 1 to 69. The red Powerball represents a number in the range of 1 to 26. To play the game, you need to pick a number for each ball in the range mentioned earlier. The prize of winning the...

  • Design a bank account class named Account that has the following private member variables:

    Need help please!.. C++ programDesign a bank account class named Account that has the following private member variables: accountNumber of type int ownerName of type string balance of type double transactionHistory of type pointer to Transaction structure (structure is defined below) numberTransactions of type int totalNetDeposits of type static double.The totalNetDeposits is the cumulative sum of all deposits (at account creation and at deposits) minus the cumulative sum of all withdrawals. numberAccounts of type static intand the following public member...

  • Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment...

    Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of 100 accounts. Use an array of pointers to objects for this. However, your...

  • Please help, provide source code in c++(photos attached) develop a class called Student with the following protected...

    Please help, provide source code in c++(photos attached) develop a class called Student with the following protected data: name (string), id (string), units (int), gpa (double). Public functions include: default constructor, copy constructor, overloaded = operator, destructor, read() to read data into it, print() to print its data, get_name() which returns name and get_gpa() which returns the gpa. Derive a class called StuWorker (for student worker) from Student with the following added data: hours (int for hours assigned per week),...

  • Tasks A. (20 po ints) In Lab 6, you defined and implemented a class called Date....

    Tasks A. (20 po ints) In Lab 6, you defined and implemented a class called Date. You will make some modific ation to the Date class so that it mccts the folowing specific ations: The Date class consists of three private member variables: Member Variable year month day Description An int variable that hokls the value of a year. An int variable that hokds the value of a month An int variable that hokis the value of a day. The...

  • In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a tes...

    In C++ Assignment 8 - Test Scores Be sure to read through Chapter 10 before starting this assignment. Your job is to write a program to process test scores for a class. Input Data You will input a test grade (integer value) for each student in a class. Validation Tests are graded on a 100 point scale with a 5 point bonus question. So a valid grade should be 0 through 105, inclusive. Processing Your program should work for any...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT