I really need help! This is in C++. I have tried everything to my knowledge and
ID: 3757650 • Letter: I
Question
I really need help! This is in C++. I have tried everything to my knowledge and am having no luck. Arrays are not supposed to be used.
A critical path analysis is a technique used to determine the schedule for a project. This information is important in the planning stages before a project is begun, and it is also useful to evaluate the progress of a project that is partially completed. One method for this analysis starts by dividing a project into sequential events and then dividing each event into various tasks. Although one event must be completed before the next one is started, various tasks within an event can occur simultaneously. The time it takes to complete an event, therefore, depends on the number of days required to finish its longest task. Similarly, the total time it takes to finish a project is the sum of time it takes to finish each event. Assume that the critical path information for a major construction project has been stored in a data file. Each line of the data file contains an event number, and the number of days required to complete the task. The data have been stored such that all the task data for event 1 are followed by all the task data for event 2, and so on.
Input file:
Event Task Number of Days
1 15 3
1 27 6
1 36 4
2 15 5
3 18 4
3 26 1
4 15 2
4 26 7
4 27 7
5 16 4
Write a program using above data file (must prompt user for file names- input and output) to read in the critical path information and write reports. Print reports (3 reports) to an output file as below:
Print a project completion timetable that lists each event number, the maximum number of days for a task within the event, and the total number of days for the project completion.
Print the event number and task number for all tasks requiring more than 5 days.
Print a report that lists the number of each event and a count of the number of tasks within the event.
** use only one input file and one output file
SAMPLE OUTPUT:
Enter input file name : cri.dat
Enter output file name: criout.dat
(This is a sample of output file)
Event Maximum Number Days for a Task
1 6
2 5
3 4
4 7
5 4
Project Duration : 26 days
Tasks requiring more than 5 days
Event Task Days
1 27 6
4 26 7
4 27 7
Event and Number of Tasks
1 3
2 1
3 2
4 3
5 1
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
bool readLine(ifstream &inFile, int &eventId, int &taskId, int &noOfDays) {
string line;
if(getline (inFile,line)) {
if(line.empty())
return false;
string word;
istringstream iss(line, istringstream::in);
int count = 0;
try {
while( iss >> word )
{
//cout << word << endl;
if(count == 0)
istringstream(word) >> eventId;
if(count == 1) {
istringstream(word) >> taskId;
}
if(count == 2) {
istringstream(word) >> noOfDays;
}
count++;
}
//cout << eventId << " "<<taskId << " "<<noOfDays <<endl;
return true;
} catch (int e)
{
return false;
}
}
return false;
}
int main() {
int MAX_SIZE = 1000;
string infile, outfile;
// cout << "Enter input file name :" <<endl;
// cin >>infile;
// cout << "Enter output file name" <<endl;
// cin >> outfile;
char *file = "D:/ravi/Cheg/eventtasks.txt";
ifstream myfile (file);
int eventIds[MAX_SIZE];
int taskIds[MAX_SIZE];
int days[MAX_SIZE];
int nEntries = 0;
if (myfile.is_open())
{
bool isValid = false;
string name;
int eventId, taskId, noOfDays;
//ignore first line which is header
readLine(myfile, eventId, taskId, noOfDays);
while ( myfile.good())
{
isValid = readLine(myfile, eventId, taskId, noOfDays);
if(isValid) {
cout << eventId << " "<<taskId << " "<<noOfDays <<endl;
eventIds[nEntries] = eventId;
taskIds[nEntries] = taskId;
days[nEntries] = noOfDays;
nEntries++;
}
}
myfile.close();
} else
cout << "Unable to open file";
int index = 0;
int eventTaskMaxDays[nEntries][3]; // [ eventid, max no of days for a task, total no of tasks]
int tasksGt5days[nEntries][3]; // [[eventId, taskId, Max No of days]
//nEntries is max, but uniqueEntities gives actual distinct entities
int uniqueEntities = 0;
int taskGt5daysIndex = 0;
int i = 0;
bool found = false;
for(i = 0; i < nEntries; i++) {
eventTaskMaxDays[i][0] = 0;
eventTaskMaxDays[i][1] = 0;
eventTaskMaxDays[i][2] = 0;
tasksGt5days[i][0] = 0;
tasksGt5days[i][1] = 0;
tasksGt5days[i][2] = 0;
}
index = 0;
while(index < nEntries) {
//formulate table for Event - Maximum Number Days for a Task
//check event already exist or not
found = false;
//cout << eventIds[index] << " ----- "<<taskIds[index] << " "<<days[index] <<endl;
for(i = 0; i < uniqueEntities; i++) {
if(eventIds[index] == eventTaskMaxDays[i][0]) {
if(days[index] > eventTaskMaxDays[i][1]) {
eventTaskMaxDays[i][1] = days[index];
}
//no of tasks related to a event
eventTaskMaxDays[i][2] += 1;
found = true;
break;
}
}
if(!found || uniqueEntities == 0) {
eventTaskMaxDays[uniqueEntities][0] = eventIds[index];
eventTaskMaxDays[uniqueEntities][1] = days[index];
eventTaskMaxDays[uniqueEntities][2] += 1;
uniqueEntities++;
}
//this is to idenfity tasks which have no of days more than 5
if(days[index] > 5) {
tasksGt5days[taskGt5daysIndex][0] = eventIds[index];
tasksGt5days[taskGt5daysIndex][1] = taskIds[index];
tasksGt5days[taskGt5daysIndex][2] = days[index];
taskGt5daysIndex++;
}
//this is for tasks
index++;
}
cout << " Event Maximum Number Days for a Task"<<endl;
for(i = 0; i <uniqueEntities; i++ )
cout << eventTaskMaxDays[i][0] << " " << eventTaskMaxDays[i][1] <<endl;
cout << " Tasks requiring more than 5 days Event Task Days"<<endl;
for(i = 0; i <taskGt5daysIndex; i++ )
cout << tasksGt5days[i][0] << " " << tasksGt5days[i][1] << " " << tasksGt5days[i][2]<<endl;
cout << " Event Number of Tasks" <<endl;
for(i = 0; i <uniqueEntities; i++ )
cout << eventTaskMaxDays[i][0] << " " << eventTaskMaxDays[i][2] <<endl;
}
-----output------------------------
1 15 3
1 27 6
1 36 4
2 15 5
3 18 4
3 26 1
4 15 2
4 26 7
4 27 7
5 16 4
Event Maximum Number Days for a Task
1 6
2 5
3 4
4 7
5 4
Tasks requiring more than 5 days
Event Task Days
1 27 6
4 26 7
4 27 7
Event Number of Tasks
1 3
2 1
3 2
4 3
5 1