| | |
finding the lowest value
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 1
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:
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
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)
#include <iostream> using namespace std; int getNumAccidents(); void findLowest(); int north, south, east, west, central int main() { cout << "There will be 5 major geographic regions within the major city.\n" << "Please answer the question for each region."; cout << "The firts region is the north"; getNumAccidents(); north = getNumAccidents(); cout << "The next region is the south."; getNumAccidents(); south = getNumAccidents(); cout << "The next region is the east."; getNumAccidents(); east = getNumAccidents(); cout << "The next region is the west."; getNumAccidents(); west = getNumAccidents(); cout << "The next region is the central."; getNumAccidents(); central = getNumAccidents(); findLowest(); } int getNumAccidents() { int accidents = 0; cout << "How many automobile accidents were reported\n" << "in the region during the last year?"; cin >> accidents; if (accidents < 0) { cout << "ERROR: please enter a positive number"; cin >> accidents: } return accidents; } void findLowest() {
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
consider this:
C++ Syntax (Toggle Plain Text)
int count = 0; int min = 32767; for (i=0;i<count;i++) { if (number[count]<min) min = number[count]; }//end of i cout<<"Minimum # is"<<min<<endl;
Last edited by zandiago; Oct 19th, 2007 at 10:42 pm. Reason: code tags
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
•
•
Join Date: Oct 2007
Posts: 49
Reputation:
Solved Threads: 1
I was planing on doing that which i know would make this all that more simple, but because of
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
C++ Syntax (Toggle Plain Text)
int main() { int smallest; cout << "The firts region is the north"; smallest = getNumAccidents(); cout << "The next region is the south"; south = getNumAccidents(); findLowest(smallest, south); cout << smallest; } void findLowest(int & smallest, int x) { //determine if x is smaller than smallest //if so assign x to smallest //since smallest is a reference, the current value f smallest will be retained back in main }
C++ Syntax (Toggle Plain Text)
int main() { const int MAX; int values[MAX]; //load data into values here int smallest; findLowest(smallest, values); cout << smallest; } void findLowest(int & smallest, int * values) { smallest = values[0]; for(int i = 1; i < MAX; ++i) { //determine if current element of values is smaller than smallest //if so, then assign current element of value to smallest } }
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: passing linked lists through functions??
- Next Thread: Selection sort on 2 different arrays by one column
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






