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.

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!

Thanks :) I'm starting to understand. So if I did something like:

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);

but expanded, obviously. I started out, and that's what I have so far, though it's far from complete.

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.

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:

#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;
}

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:

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."

All right, I finaly got it to do what I wanted:

#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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.