## Tausif214

Assignment:
Write a program that will read the critical path information from the attached file
(ProjectInfo.txt) into 3 arrays, events, tasks and numDays and defines a 4th array called
eventNumDays in which you will calculate the number of days needed for each event to
complete, the index of the array will represent the event number.

Part 2:(these are seperate functions that i have to write)
Create and implement a user driven menu with the following options:
1) Number of days for a given event
2) List tasks in given event
3) Sort events by number of days
0) Exit
Each of the menu options should behave as follows:
1. Number of days for a given event
Prompts the user to enter an event number and displays the number of days needed for
completion of that specific event.
2. List tasks in given event
Prompts the user to enter an event number and lists the tasks associated with the given event.
3. Sort events by number of days
Displays a list of events sorted in descending order of the number of days needed to complete
the event. This requires creating 2 new arrays: a copy of eventNumDays called
sortedEventNumDays and a sortedEvents array which is initialized with event numbers
sorted in descending order. Using a Selection sort that you learned in class, sort the
sortedEventNumDays array and move/switch element in the sortedEvents array
accordingly. Remember that the index of eventNumDays represent the event number.
Finally you display the sortedEvents array.

THIS IS WHAT I GOT SO FAR FOR NUMBER 1 BUT IT IS NOT WORKING OUT PLEASE HELP!!

``````int eventDays(int& n)
{
int events[MAX], numDays[MAX];
int m;
cout << "Please input an event number: ";
cin >> n;

m = events[n];

cout << "answer: " << m << endl;

return n;
}``````

## mrboolf 122

Apart from useless variables (numDays array and m) I think your main problem is that the events array just contains garbage data: it is not initialised anywhere. If you intended to use another events array initialised somewhere else in your code you should pass it to the function, or it'll work just with an array with the same name (and NOT the same info). Also, you need not return n.

## Tausif214

yes i know
ive been changing around the function alot and its still not working....i need the function to work and give me the rite output i need....

## Lerner 582

As mrboolf said you probable want to send events and numDays to eventDays, not declare them in eventDays. It may well end up looking something like this:

``````void eventDays(string events[], int numDays[], int size)
{
//display elements of events in a menu
//have user input event number from menu
//display number of days event takes
}``````

## Tausif214

yeah that what im suppose to do for the function...but im having trouble writing the function...i ask the user to input an event number....but then i dont know how to write the code so that it gives the answer from numDays

## mrboolf 122

Post an attempt and we will help you fix the errors, if you encounter any.

Try to take one step at a time towars the ultimate goal of a complete and working program.
You managed to get input from the user? Good, now you should check if the input is valid (is it a valid index for the events array?). Then you should focus on accessing the array in which is stored the info on how many days are required for every event at the right index. After that, you only have to print on screen the number of days that is contained there.

Example:

``````1. Ask the user for the event index.
2. User inputs x.
3. Check if 0 <= x < events array size.
4. If not, repeat from step 1.
5. You know that event[x] corresponds to numDays[y], so store numDays[y] in a temp variable
6. Print on screen that temp variable's content.``````

Obviously you can merge step 5 into step 6 just printing on screen numDays[y] without storing its content in a variable (if you don't need to keep track of the info for some reason).

## Tausif214

is it something like this:

``````void eventDays(int events[], int numDays[], int n)
{
int temp, k;
ifstream infile;
infile.open("ProjectInfo.txt");
cout << "Please input an event number: ";
cin >> n;

events[n]=numDays[k];
temp=numDays[k];

cout << "answer= " << temp << endl;

return;
}``````

if its not like this then can correct it please.

## mrboolf 122

No, I fear it's not like this.

First, why do you open "ProjectInfo.txt" ? If you open it to actually load the info in the numDays array then you should do it in another function and pass to this function the array already filled and ready to go.

Second, there's no purpose in `events[n]=numDays[k]` as this would just overwrite the info in the events array with the info in the numDays array.

Third, I think you took my example too literally. What does k represents in your code? As long as you don't initialize it, it will contain just garbage data, hence trying to access numDays[k] with k as a random int would 99% of times result in an out-of-boundaries invalid access and 100% of times result in not working code.

Here is a start, assuming that both events and numDays arrays are correctly loaded before you call the function:

``````void eventDays(int events[], int numDays[], int dimension) {
int temp = 0, n = 0;
cout << "Please input an event number: ";
cin >> n;
/* here you should check that events[n] won't result in an out-of-boundaries invalid access, I suggest you to put all the input in a while loop*/
/*now you know that n is a valid event index. now ask yourself one question: are the arrays sorted so that # of days for event[x] is to be found in numDays[x] ? If the answer is yes then you can use the same index for the numDays array, otherwise you could do something like: */
// int y = expr_to_n_corresponding_index;
/* now you are ready to print the result */
temp = numDays[n]; // or numDays[y], if you needed the previous instruction
cout << "answer = " << temp << endl;
return;
}``````

Try to work on this.