Edit: The max number of activities is arbitrary. As written in question; program
ID: 3776177 • Letter: E
Question
Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered. It is needed to select activities from these activities. Max number is up to us.
323as1) Could you please help me to solve this problem? (ONLY USING C++)
Problem:
You are requested to create a class called “Activity”.
You will use ordered link list to hold Activity’s class information.
In order to do that you will use Standard Template Library (STL) to make your codes more generic.
In the main method, you will create an ordered linked list object and add the information, which are entered by the user (id, start time, finish time), to the linked list object.
Id should be greater than or equal 0. Unless the user enters a negative number, the program will continue to ask activity information, else the program will terminate and if the first id number is a negative number, program will give an error message like EMPTY
Sort the linked list according to the finish time.
According to the entered a set of activities, select the activities up to maximum number of activities and print the total duration of selected activities.
Sample runs are below.
Edit: The max number of activities is arbitrary. As written in question; program should ask us to input the id of activities unless a negative number is entered. It is needed to select activities from these activities. Max number is up to us.
Reminder:
- It is needed to include STL for Linked list.
Enter the id Enter the start time 1 Enter the finish time 2 Enter the id 1 Enter the start time: 3 Enter the finish time 4 Enter the id: 2 Enter the start time 0 Enter the finish time: 6 Enter the id: 3 Enter the start time 5 Enter the finish time Enter the id: 4 Enter the start time 8 Enter the finish time 9 Enter the id 5 Enter the start time 5 Enter the finish time 9 Enter the id -1 Selected activities: 0 1 3 4 Duration of activities 5 Enter the id: 1 Enter the start time 3 Enter the finish time 5 Enter the id: 2 Enter the start time 6 Enter the finish time 8 Enter the id: 3 Enter the start time 1 Enter the finish time: 4 Enter the id: 4 Enter the start time: 4 Enter the finish time Enter the id: 5 Enter the start time Enter the finish time: 10 Enter the id -1 Selected activities 3 4 5 Duration of activities 9Explanation / Answer
#include <iostream>
#include <list>
using namespace std;
int main( )
{
list<int> intlist;
list<int>::iterator ptr;
int i;
//checking whether the list is empty
if (intlist.empty( )= =true)
{
cout<<endl<<"The list is empty";
}
//add 5 values to the list (0 1 2 3 4)
for( i = 0; i<5; i++)
{
intlist.push_back( i );
}
//add another element (4) to the list
intlist.push_back(4);
//initializ the iterator to first node
//display the value held in first node
ptr=intlist.begin( );
cout<<endl<<"First element has value: "<<*ptr;
//traversing through the list by incrementing the iterator
cout<<endl<<"Initial list contents are : ";
for (i = 0; ptr != intlist.end( ); i++)
{
cout<<" "<<*ptr; //displaying the value at that node
ptr++;
}
//removing duplicate elements from the list
intlist.unique( );
cout<<endl<<"Modified list contents are: ";
ptr = intlist.begin( );
for (i=0; ptr!=intlist.end( ); i++)
{
cout<<" "<<*ptr;
ptr++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.