pass by help

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

Join Date: Nov 2006
Posts: 6
Reputation: Senekha is an unknown quantity at this point 
Solved Threads: 0
Senekha Senekha is offline Offline
Newbie Poster

pass by help

 
0
  #1
Nov 3rd, 2006
Hello, this is my first post on these forums. I'm taking a CPSC 100 course to supplement the science part of my degree, and I've been having a lot of trouble since the midterm, when the profs for the class switched (they have completely different teaching methods). Since then, I've been lost.

I've got some questions that are due within the next 24 hours, and I've given up tampering with them myself, and I can't find anyone around here for help. Thus I find myself on these forums.

Anyways, I'll cut the crap. I'm having problems with simple things like pass by values - I can't seem to get my head wrapped around them. The questions on my current assignment are similar to this:

"Write a program that determines which of 5 geographic regions within a major city (north, south, east, west, 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 the region. It asks the user for the number of automobile accidents reported in that region during the year, validates the input, then returns it. It should be called once for each 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 figure."

Now my problem is I can't figure out the "is passed the name" part of the thing. I don't know how to write something that can be used all five times, for the five sectors etc...this is such an elementary this, which is why I'm so vexed that I can't get it.

All I've got so far is some experimental code that makes no sense...If I make any progress, I'll post it on here.

Any help is *greatly* appreciated. I want to pass this course, and the lectures aren't doing anything for me - I've been learning the last bit by reading the book, which is a very bad way for me to retain knowledge.

Thanks!
.Sen.
Last edited by Senekha; Nov 3rd, 2006 at 7:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: pass by help

 
0
  #2
Nov 3rd, 2006
Break it into parts.

How I would start is with the main function, and it should do the following:
  • Loop through each reigon, passing it as a parameter to getNumAccidents() (hint: enums will come in handy). Store the return value of getNumAccidents() in a variable.
  • Call findLowest(), which will then determine the lowest number of accidents.

Then you can go on to writing each individual function. For example, getNumAccidents must:
  1. First of all recieve the reigon passed as a parameter.
  2. Ask the user for the number of accidents, and store it in a variable.
  3. Validate the input (you may want to write a subfunction for this).
  4. Once the input is validated, return the value.

And findLowest is just a matter of figuring out which reigon is the lowest and printing it out.

Hope this makes sense!
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Senekha is an unknown quantity at this point 
Solved Threads: 0
Senekha Senekha is offline Offline
Newbie Poster

Re: pass by help

 
0
  #3
Nov 3rd, 2006
Thanks I'm starting to understand. So if I did something like:

[php] int getNumAccidents(int region_name){
cout << "Please enter the number of accidents in this area.";
cin >> numA;
return numA;
}//getNumAccidents

void findLowest(int North, int south...){
int lowest;
lowest = 0;
lowest = North;
if (lowest > East) {
lowest = South;}
else if (lowest > East){
lowest = East;}
else if ....etc etc

cout << "The area with the lowest number of accidents is: " << lowest << endl;

int main(){
string area, central, south, east, west, north;



North = getNumAccidents(1);
cout << "The number of accidents in this area is " << North << endl;

findLowest(Noth, South, East, West, Center);[/php]
but expanded, obviously. I started out, and that's what I have so far, though it's far from complete.
Last edited by Senekha; Nov 3rd, 2006 at 9:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: pass by help

 
0
  #4
Nov 3rd, 2006
Looking good! You're well on your way. Several things:

You still need to implement the loop when you call getNumAccidents(). You should merely feed it the iterator variable from the loop as its parameter. getNumAccidents() can then figure out which reigon is meant by the code.

Not quite sure why you declared area, central, north, etc. as strings. After all, they're supposed to hold the number of accidents (so use int).

A place where you might want to have strings is the for printing out text. For example, you might want to store the names of the reigons in a string array that can be accessed by any function. The reason for doing this is so that in a loop you can quickly access the name of the reigon meant by the reigon's code.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Senekha is an unknown quantity at this point 
Solved Threads: 0
Senekha Senekha is offline Offline
Newbie Poster

Re: pass by help

 
0
  #5
Nov 3rd, 2006
Okay, I have one last glitch with this program: I can't get it to print the name of the region with the least amount of accidents; it just prints the lowest number. How can I fix this?

Btw, I'm using DevC++

This what I have:

[PHP]#include <iostream>
#include <cstdlib>

using namespace std;

int getNumAccidents(int numA){
cout << "Please enter the number of accidents in this area.";
cin >> numA;
return numA;
}//getNumAccidents

void findLowest(int north, int south, int east, int west, int center){
int lowest;
lowest = 0;
lowest = north;
if (lowest > east) {
lowest = south;}
else if (lowest > east){
lowest = east;}
else if (lowest > center){
lowest = center;}

cout << "The area with the lowest number of accidents is: " << lowest << endl;
}
int main(){

int north, south, east, west, center;

cout << "When prompted, please enter the number of accidents in each sector,\n";
cout << "in this order: North, South, East, West, and Center.\n\n\n";

north = getNumAccidents(1);
south = getNumAccidents(2);
east = getNumAccidents(3);
west = getNumAccidents(4);
center = getNumAccidents(5);
cout << "\n\n\n";
cout << "The number of accidents in the North sector is " << north << endl;
cout << "The number of accidents in the South sector is " << south << endl;
cout << "The number of accidents in the East sector is " << east << endl;
cout << "The number of accidents in the West sector is " << west << endl;
cout << "The number of accidents in the Center sector is " << center << endl;

findLowest(north, south, east, west, center);

system("PAUSE");
return 0;
}
[/PHP]
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: pass by help

 
0
  #6
Nov 4th, 2006
Make a string in findLowest that will hold the reigon's name.

Then, when you are searching for the smallest one, have the string ready to store the reigon with the smallest number of accidents. Once you've found the reigon, add something like:
  1. lowest_reigon = "North";
if it were the north reigon that has the smallest number of accidents. Do you understand? (May not be the best way of doing it, but it'll work.)

Then, when you're annoucing the smallest number, you just have to insert the variable into the text.

"The area with the lowest number of accidents is $lowest_reigon with $lowest number of accidents."
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Senekha is an unknown quantity at this point 
Solved Threads: 0
Senekha Senekha is offline Offline
Newbie Poster

Re: pass by help

 
0
  #7
Nov 4th, 2006
All right, I finaly got it to do what I wanted:

[PHP]#include <iostream>
#include <cstdlib>

using namespace std;

int getNumAccidents(int numA){
cout << "Please enter the number of accidents in this area.";
cin >> numA;
return numA;
}//getNumAccidents

void findLowest(int north, int south, int east, int west, int center){
int lowest;
lowest = 0;
lowest = south;
if (lowest > north){
lowest = north;}
if (lowest > south){
lowest = south;}
if (lowest > east){
lowest = east;}
if (lowest > west){
lowest = west;}
if (lowest > center){
lowest = center;}

if (lowest == north)
cout << "The area with the lowest number of accidents is the North\n\n";
else if (lowest == south)
cout << "The area with the lowest number of accidents is the South\n\n";
else if (lowest == west)
cout << "The area with the lowest number of accidents is the West\n\n";
else if (lowest == east)
cout << "The area with the lowest number of accidents is the East\n\n";
else if (lowest == center)
cout << "The area with the lowest number of accidents is the Center\n\n";
}//void

int main(){

int north, south, east, west, center;

cout << "When prompted, please enter the number of accidents in each sector,\n";
cout << "in this order: North, South, East, West, and Center.\n\n\n";

north = getNumAccidents(1);
south = getNumAccidents(2);
east = getNumAccidents(3);
west = getNumAccidents(4);
center = getNumAccidents(5);
cout << "\n\n\n";
cout << "The number of accidents in the North sector is " << north << endl;
cout << "The number of accidents in the South sector is " << south << endl;
cout << "The number of accidents in the East sector is " << east << endl;
cout << "The number of accidents in the West sector is " << west << endl;
cout << "The number of accidents in the Center sector is " << center << "\n\n\n";

findLowest(north, south, east, west, center);

system("PAUSE");
return 0;
}//main
[/PHP]
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