Functions

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

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Functions

 
0
  #1
Oct 28th, 2008
The assignment calls for me to find the area with the fewest accidents (north, south, east, west, central) in a city. I am to write two functions:

-getNumAccidents() is passed the name of the region. It asks user for the number of auto accidents in the region, validates info, and returns it. It should be called once for each region.

-void findLowest() is passed the five accident total. It determines which is the smallest and prints the name of the region, along with accident figure.

I wrote first function in a cout/ cin line asking for user input of the region, as well as the accident total from that region. I am unsure as to what would be the easiest way to find the lowest amount. I do not want to use an array in this program, because we have not gone over them in C++, only java...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Functions

 
0
  #2
Oct 28th, 2008
>I am unsure as to what would be the easiest way to find the lowest amount.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int vals[] = {234, 3, 4, 45, 4};
  8.  
  9. int lowest = 10000; //some large value
  10.  
  11. for ( int i = 0 ; i < 5; i++ )
  12. {
  13. if ( vals[i] < lowest )
  14. {
  15. lowest = vals[i];
  16. }
  17. }
  18.  
  19. cout << lowest << endl;
  20.  
  21. cin.get();
  22. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: Functions

 
0
  #3
Oct 28th, 2008
Hi since..:
I do not want to use an array in this program, because we have not gone over them in C++, only java...
let imagine your code :
  1. ...
  2. int main()
  3. {
  4. ......
  5. for ( int i = 0 ; i < 5; i++ )
  6. {
  7. int noAccidents = getNumAccidents(area);
  8. int areanumber = findlowest(noAccidents );
  9. }
  10. switch(areanumber )
  11. {
  12. case 1:
  13. cout<<"North area of town has the lowest no of accidents: "<<noAccidents ;
  14. case 2:
  15. cout<<"Southarea of town has the lowest no of accidents: "<<noAccidents ;
  16. .....
  17. default:
  18. ..}
  19. ///functions implementations:
  20. ......
  21. int getNumAccidents(const string& regionName)
  22. {
  23. //your implementation
  24. }
  25.  
  26. int findLowest(const int& numberOfAccidents )
  27. {
  28. int temp = 5000; //a very big number of accidents :D
  29. int saveIndex=0;
  30. if (numberOfAccidents < temp);
  31. {
  32. temp=numberOfAccidents ;
  33. saveIndex=i;
  34. }
  35. return saveIndex;
  36. }
  37. .....
  38. return 0;
  39. }

good luck
"What we repeat defines what we are!!" Aristotle
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Functions

 
0
  #4
Oct 29th, 2008
So much help, so little correct

To find the lowest value, store the first value read in a variable, say lowest. Then for each successive read, test the value read against lowest and replace if necessary.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 114
Reputation: sidatra79 is an unknown quantity at this point 
Solved Threads: 8
sidatra79's Avatar
sidatra79 sidatra79 is offline Offline
Junior Poster

Re: Functions

 
0
  #5
Oct 29th, 2008
So much help, so little correct
Maybe u should post at 2 am too, to see how correct your solution would be.....
"What we repeat defines what we are!!" Aristotle
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
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