| | |
C++ homework...Please help!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
please help me with this
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!!
please correct this for me!!
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!!
C++ Syntax (Toggle Plain Text)
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; }
Last edited by Ancient Dragon; Nov 18th, 2008 at 8:22 pm. Reason: add code tags
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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.
•
•
Join Date: Jul 2005
Posts: 1,705
Reputation:
Solved Threads: 274
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:
C++ Syntax (Toggle Plain Text)
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 }
Klatu Barada Nikto
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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:
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).
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:
C++ Syntax (Toggle Plain Text)
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).
Last edited by mrboolf; Nov 19th, 2008 at 3:00 pm.
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
is it something like this:
if its not like this then can correct it please.
C++ Syntax (Toggle Plain Text)
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; }
Last edited by Narue; Nov 19th, 2008 at 4:37 pm. Reason: Added code tags
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
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
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:
Try to work on 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:
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- We only give homework help to those who show effort (Computer Science)
- Need help with Computer Science homework (Computer Science)
- Dynamic memory allocation homework (C++)
- Homework Help!! Priority Queue ?? (Computer Science)
Other Threads in the C++ Forum
- Previous Thread: A bit of a problem with Arrays
- Next Thread: Unknown problem - Program exits during debugging
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






