Thread: Basic Function Problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    Basic Function Problem

    my code works fine but it seems like it took an awfully large amount of code to complete this task. is there any way i could shorten this code?

    the code is supposed to have 2 functions. one is suposed to ask the user to enter the data for their zone. it is supposed to be called 5 times, once for each zone. the other function is supposed to figure out which zone has least amount and display that. i especially think there is a shorter way to code around all my if statements that i can't think of. please let me know.

    here is code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int getNumAccidents(string);
    void findLowest(int, int, int, int, int);
    
    int main()
    {
    	string north = "north";
    	string east = "east";
    	string south = "south";
    	string west = "west";
    	string central = "central";
    	int n, e, s, w ,c;
    
    	n = getNumAccidents(north);
    	e = getNumAccidents(east);
    	s = getNumAccidents(south);
    	w = getNumAccidents(west);
    	c = getNumAccidents(central);
    
    	findLowest(n, e, s, w, c);
    
    
    	return 0;
    }
    
    int getNumAccidents(string area)
    {
    	int accidents;
    	cout << "Enter the number of automobile accidents for " << area << ": ";
    	cin >> accidents;
    	return accidents;
    }
    
    void findLowest(int north, int east, int south, int west, int central)
    {
    
    	if (north < east && north < south && north < west && north < central)
    	{
    		cout << "North has least amount of accidents with " << north << endl;
    	}
    	if (east < north && east < south && east < west && east < central)
    	{
    		cout << "East has least amount of accidents with " << east << endl;
    	}
    	if (south < east && south < north && south < west && south < central)
    	{
    		cout << "South has least amount of accidents with " << south << endl;
    	}
    	if (west < east && west < south && west < north && west < central)
    	{
    		cout << "West has least amount of accidents with " << west << endl;
    	}
    	if (central < east && central < south && central < west && central < north)
    	{
    		cout << "North has least amount of accidents with " << north << endl;
    	}
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    think arrays.

    have n, e, s, w, and c elements of an array, and have constants (or enums) of the same name to index it. then you can just loop through the array in findLowest to find the lowest element.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Yea those if statements are kind of a space hog, even though it does the trick.

    consider something like this...

    Code:
    // return value is the index of the array that has the lowest accidents, or number
    int findLowest(int* accidentsPerArea, int sizeOfParamOne)
    {
        int lowestIndex = 0xFFFFFFFF; // One big arse number
        for(int i = 0; i < sizeOfParamOne; i++){
            //if the number in the next index point is lower than lowestIndex then assign it
        }
        //return the index pertaining to the lowest accident count;
    }
    edit: robwhit beat me.
    Last edited by scwizzo; 08-16-2009 at 05:25 PM.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by scwizzo View Post
    Yea those if statements are kind of a space hog, even though it does the trick.

    consider something like this...

    Code:
    // return value is the index of the array that has the lowest accidents, or number
    int findLowest(int* accidentsPerArea, int sizeOfParamOne)
    {
        int lowestIndex = 0xFFFFFFFF; // One big arse number
        for(int i = 0; i < sizeOfParamOne; i++){
            //if the number in the next index point is lower than lowestIndex then assign it
        }
        //return the index pertaining to the lowest accident count;
    }
    edit: robwhit beat me.
    There's no need to set lowestIndex to a high value - just plain 0 works fine.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    30
    if it's set to 0 then the number it's compared to won't fit the condition of being less than it so it will never be assigned to the lowest value. or will it? do you get what i'm saying though?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by AJOHNZ View Post
    if it's set to 0 then the number it's compared to won't fit the condition of being less than it so it will never be assigned to the lowest value. or will it? do you get what i'm saying though?
    No, in the example he posted the return value is an index into the array. If it's the value itself you want to return, simply initialize the return value to the first element in the array (perhaps checking that the array size is not zero, beforehand).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. DLL Function / Load Library Problem
    By cboard_member in forum Windows Programming
    Replies: 5
    Last Post: 12-10-2005, 10:11 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM