| | |
Functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
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...
-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...
>I am unsure as to what would be the easiest way to find the lowest amount.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int vals[] = {234, 3, 4, 45, 4}; int lowest = 10000; //some large value for ( int i = 0 ; i < 5; i++ ) { if ( vals[i] < lowest ) { lowest = vals[i]; } } cout << lowest << endl; cin.get(); }
*Voted best profile in the world*
Hi since..:
let imagine your code
:
good luck
•
•
•
•
I do not want to use an array in this program, because we have not gone over them in C++, only java...
: c++ Syntax (Toggle Plain Text)
... int main() { ...... for ( int i = 0 ; i < 5; i++ ) { int noAccidents = getNumAccidents(area); int areanumber = findlowest(noAccidents ); } switch(areanumber ) { case 1: cout<<"North area of town has the lowest no of accidents: "<<noAccidents ; case 2: cout<<"Southarea of town has the lowest no of accidents: "<<noAccidents ; ..... default: ..} ///functions implementations: ...... int getNumAccidents(const string& regionName) { //your implementation } int findLowest(const int& numberOfAccidents ) { int temp = 5000; //a very big number of accidents :D int saveIndex=0; if (numberOfAccidents < temp); { temp=numberOfAccidents ; saveIndex=i; } return saveIndex; } ..... return 0; }
good luck
"What we repeat defines what we are!!" Aristotle
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
-->
"looping.. defines Perfomance from O (n) ..up to O(n!)" Sidatra79 :D :D
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.
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- VB's Left, Right, Mid Functions in C++? (C++)
- User defined functions (C++)
- Double Linked Lists and Functions required (C++)
- How to write FNVAL functions (Java)
- I dont see any difference between these 2 functions, DO YOU? (C)
- access Digital Camera Functions (C++)
Other Threads in the C++ Forum
- Previous Thread: help with simple programing problem
- Next Thread: appending the end of a file
| Thread Tools | Search this Thread |
api application 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 desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






