944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3362
  • C++ RSS
Oct 19th, 2007
0

finding the lowest value

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 21
Solved Threads: 1
Light Poster
helixkod is offline Offline
49 posts
since Oct 2007
Oct 19th, 2007
0

Re: finding the lowest value

consider this:
C++ Syntax (Toggle Plain Text)
  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
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 19th, 2007
0

Re: finding the lowest value

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.
Reputation Points: 21
Solved Threads: 1
Light Poster
helixkod is offline Offline
49 posts
since Oct 2007
Oct 19th, 2007
0

Re: finding the lowest value

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 19th, 2007
0

Re: finding the lowest value

for (i=0;i<count;i++)

i needs to be declared before it is used in this statement.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 19th, 2007
0

Re: finding the lowest value

I was planing on doing that which i know would make this all that more simple, but because of
Click to Expand / Collapse  Quote originally posted by helixkod ...
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.
Reputation Points: 21
Solved Threads: 1
Light Poster
helixkod is offline Offline
49 posts
since Oct 2007
Oct 21st, 2007
0

Re: finding the lowest value

C++ Syntax (Toggle Plain Text)
  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. }

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: passing linked lists through functions??
Next Thread in C++ Forum Timeline: Selection sort on 2 different arrays by one column





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC