C++ homework...Please help!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 5
Reputation: Tausif214 is an unknown quantity at this point 
Solved Threads: 0
Tausif214 Tausif214 is offline Offline
Newbie Poster

C++ homework...Please help!!

 
0
  #1
Nov 18th, 2008
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 2these 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!!
  1. int eventDays(int& n)
  2. {
  3. int events[MAX], numDays[MAX];
  4. int m;
  5. cout << "Please input an event number: ";
  6. cin >> n;
  7.  
  8. m = events[n];
  9.  
  10. cout << "answer: " << m << endl;
  11.  
  12. return n;
  13. }
please correct this for me!!
Last edited by Ancient Dragon; Nov 18th, 2008 at 8:22 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: C++ homework...Please help!!

 
0
  #2
Nov 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: Tausif214 is an unknown quantity at this point 
Solved Threads: 0
Tausif214 Tausif214 is offline Offline
Newbie Poster

Re: C++ homework...Please help!!

 
0
  #3
Nov 18th, 2008
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....
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,705
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: C++ homework...Please help!!

 
0
  #4
Nov 18th, 2008
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:
  1. void eventDays(string events[], int numDays[], int size)
  2. {
  3. //display elements of events in a menu
  4. //have user input event number from menu
  5. //display number of days event takes
  6. }
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: Tausif214 is an unknown quantity at this point 
Solved Threads: 0
Tausif214 Tausif214 is offline Offline
Newbie Poster

Re: C++ homework...Please help!!

 
0
  #5
Nov 19th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: C++ homework...Please help!!

 
0
  #6
Nov 19th, 2008
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.  
  2. 1. Ask the user for the event index.
  3. 2. User inputs x.
  4. 3. Check if 0 <= x < events array size.
  5. 4. If not, repeat from step 1.
  6. 5. You know that event[x] corresponds to numDays[y], so store numDays[y] in a temp variable
  7. 6. Print on screen that temp variable's content.
  8.  

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: Tausif214 is an unknown quantity at this point 
Solved Threads: 0
Tausif214 Tausif214 is offline Offline
Newbie Poster

Re: C++ homework...Please help!!

 
0
  #7
Nov 19th, 2008
is it something like this:
  1. void eventDays(int events[], int numDays[], int n)
  2. {
  3. int temp, k;
  4. ifstream infile;
  5. infile.open("ProjectInfo.txt");
  6. cout << "Please input an event number: ";
  7. cin >> n;
  8.  
  9. events[n]=numDays[k];
  10. temp=numDays[k];
  11.  
  12. cout << "answer= " << temp << endl;
  13.  
  14. return;
  15. }
if its not like this then can correct it please.
Last edited by Narue; Nov 19th, 2008 at 4:37 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: C++ homework...Please help!!

 
0
  #8
Nov 19th, 2008
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:
  1. void eventDays(int events[], int numDays[], int dimension) {
  2. int temp = 0, n = 0;
  3. cout << "Please input an event number: ";
  4. cin >> n;
  5. /* 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*/
  6. /*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: */
  7. // int y = expr_to_n_corresponding_index;
  8. /* now you are ready to print the result */
  9. temp = numDays[n]; // or numDays[y], if you needed the previous instruction
  10. cout << "answer = " << temp << endl;
  11. return;
  12. }

Try to work on this.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: Tausif214 is an unknown quantity at this point 
Solved Threads: 0
Tausif214 Tausif214 is offline Offline
Newbie Poster

Re: C++ homework...Please help!!

 
0
  #9
Nov 19th, 2008
thank you for your help!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC