finding the lowest value

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

Join Date: Oct 2007
Posts: 49
Reputation: helixkod is an unknown quantity at this point 
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

finding the lowest value

 
0
  #1
Oct 19th, 2007
This is the current assignment:
Write a program that determines which of 5 geographic regions within a major city (north, south, east, west, and central) had the fewest reported automobile accidents last year. It should have the following two functions, which are called by main

int getNumAccidents() is passed the name of a region. It asks the user for the number of automobile accidents reported in that region during the last year, validates the input. then returns it. It should be called once for every city region.

void findLowest() is passed the five accident totals. It determines which is the smallest and prints the name of the region, along with its accident figures.


my current code:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int getNumAccidents();
  5. void findLowest();
  6. int north, south, east, west, central
  7.  
  8. int main()
  9. {
  10. cout << "There will be 5 major geographic regions within the major city.\n"
  11. << "Please answer the question for each region.";
  12. cout << "The firts region is the north";
  13. getNumAccidents();
  14. north = getNumAccidents();
  15. cout << "The next region is the south.";
  16. getNumAccidents();
  17. south = getNumAccidents();
  18. cout << "The next region is the east.";
  19. getNumAccidents();
  20. east = getNumAccidents();
  21. cout << "The next region is the west.";
  22. getNumAccidents();
  23. west = getNumAccidents();
  24. cout << "The next region is the central.";
  25. getNumAccidents();
  26. central = getNumAccidents();
  27. findLowest();
  28.  
  29.  
  30. }
  31.  
  32. int getNumAccidents()
  33. {
  34. int accidents = 0;
  35.  
  36. cout << "How many automobile accidents were reported\n"
  37. << "in the region during the last year?";
  38. cin >> accidents;
  39.  
  40. if (accidents < 0)
  41. {
  42. cout << "ERROR: please enter a positive number";
  43. cin >> accidents:
  44. }
  45. return accidents;
  46. }
  47.  
  48. void findLowest()
  49. {


Right now i'm stuck on the method of finding the lowest value and how i would associate that value with one of the regions. so pretty much the void findLowest() section
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2,462
Reputation: zandiago is on a distinguished road 
Solved Threads: 25
Featured Poster
zandiago's Avatar
zandiago zandiago is offline Offline
Nearly a Posting Maven

Re: finding the lowest value

 
0
  #2
Oct 19th, 2007
consider this:
  1. int count = 0;
  2. int min = 32767;
  3. for (i=0;i<count;i++)
  4. {
  5. if (number[count]<min)
  6. min = number[count];
  7. }//end of i
  8. cout<<"Minimum # is"<<min<<endl;
Last edited by zandiago; Oct 19th, 2007 at 10:42 pm. Reason: code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: helixkod is an unknown quantity at this point 
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

Re: finding the lowest value

 
0
  #3
Oct 19th, 2007
i got the two errors:
error C2065: 'i' : undeclared identifier
error C2065: 'number' : undeclared identifier

i added:
int number[100];

to get rid of the first error, but i thought:
for (i=0;i<count;i++)

would declare the identifier. i havent tested the actual code yet.
Last edited by helixkod; Oct 19th, 2007 at 10:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
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: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: finding the lowest value

 
0
  #4
Oct 19th, 2007
An alternate method that doesn't rely on using an array to hold each value read in and doesn't use some large number for the initial value of min would be to assign the first value entered to the variable holding smallest value entered. Then with each subsequent value entered compare it with the current smallest value and current value is smaller than current smallest value assign current value to smallest value. If you are going to use the values read in for other analyses then holding them in a container (like an array) is a good idea.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
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: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: finding the lowest value

 
0
  #5
Oct 19th, 2007
for (i=0;i<count;i++)

i needs to be declared before it is used in this statement.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 49
Reputation: helixkod is an unknown quantity at this point 
Solved Threads: 1
helixkod helixkod is offline Offline
Light Poster

Re: finding the lowest value

 
0
  #6
Oct 19th, 2007
I was planing on doing that which i know would make this all that more simple, but because of
Originally Posted by helixkod View Post
void findLowest() is passed the five accident totals.
i don't believe i can do that..
so im guessing the array may be the way to go with this. im just not too sure yet how.
Last edited by helixkod; Oct 19th, 2007 at 11:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
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: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: finding the lowest value

 
0
  #7
Oct 21st, 2007
  1. int main()
  2. {
  3. int smallest;
  4. cout << "The firts region is the north";
  5. smallest = getNumAccidents();
  6.  
  7.  
  8. cout << "The next region is the south";
  9. south = getNumAccidents();
  10. findLowest(smallest, south);
  11.  
  12. cout << smallest;
  13. }
  14.  
  15. void findLowest(int & smallest, int x)
  16. {
  17. //determine if x is smaller than smallest
  18. //if so assign x to smallest
  19. //since smallest is a reference, the current value f smallest will be retained back in main
  20. }

  1. int main()
  2. {
  3. const int MAX;
  4. int values[MAX];
  5. //load data into values here
  6.  
  7. int smallest;
  8. findLowest(smallest, values);
  9.  
  10. cout << smallest;
  11. }
  12.  
  13. void findLowest(int & smallest, int * values)
  14. {
  15. smallest = values[0];
  16. for(int i = 1; i < MAX; ++i)
  17. {
  18. //determine if current element of values is smaller than smallest
  19. //if so, then assign current element of value to smallest
  20. }
  21. }

Given the description of findLowest() using an array is probably the desired. Also, given the description of findLowest() you can declare smallest in findLowest() and not bother passing it to findLowest() since findLowest() will print it.
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