EvilZone

Programming and Scripting => C - C++ => Topic started by: Super_mario666 on September 26, 2012, 06:48:08 AM

Title: My code needs a tune up.
Post by: Super_mario666 on September 26, 2012, 06:48:08 AM
More homework, more fun :D . any ideas to make it better. i know it can be fixed up but im not that sure how.


Code: [Select]

 
#include <iostream>
using namespace std;
 
int accident[5]; // yes........yes i know
 
int getNumAccidents()///// Module to find number of Accidents
{    int n = 0; // Used to increment Accident array in For loop
     int count; // used to count the parts of town
     cout << "How many Accidents were in the north, south, east, west";
     cout << "\nand central part of town respectivly? \n";
     for(count = 1; count <= 5; count++) //Loop for take user input
      {
          cout << count << ".\t";
          cin >> accident[n];
          while (accident[n] < 0)// User valiation
               {
                cout << "Please enter a valid number.";
                cin >> accident[n];
               }
          cout << endl;
          n++;// incrementing array
      }
    }
 
 
void findLowest()//// Module to find the least number of accidents
{ int min = accident[0];// Set as point of reference
  for (int i = 1; i < 5; i++)// loop to find least amount of accidents
      {
        if(min > accident[i])// if any variable is least than min than it will become min
           min = accident[i];//it will loop until all variables pass and min will be the smallest
      }
  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Used to determine user output~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
   if (min = accident[0])
       cout << "The North side of town has the safest driving area with " << min << " accidents\n";
   
   else if (min = accident[1])
       cout << "The South side of town has the safest driving area with " << min << " accidents\n";
   
   else if (min = accident[2])
       cout << "The East side of town has the safest driving area with " << min << " accidents\n";
   
   else if (min = accident[3])
       cout << "The West side of town has the safest driving area with " << min << " accidents\n";
   
   else if (min = accident[4])
       cout << "The Central part of town has the safest driving area with " << min << " accidents\n"; 
  /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
}
 
 
int main()
{   getNumAccidents();// Calling Module
    findLowest();// Calling Module
    system ("pause");
    return 0;
}
Title: Re: My code needs a tune up.
Post by: Live Wire on September 26, 2012, 07:41:11 AM
in all of your if/else statements you need to change = to == . other than that, it looks okay. to me at anyway :)
Title: Re: My code needs a tune up.
Post by: Kulverstukas on September 26, 2012, 07:52:41 AM
Could use better style for comments. That applies everywhere - don't clobber the comments together with code. Make spaces and format them.
Title: Re: My code needs a tune up.
Post by: p_2001 on September 26, 2012, 08:00:13 AM
You did not actually run it :(... At least post a working code

Anyhow.. The first function seems ok
..
For second function.. Make use of passing parameters and returning a value by the function... Instead of using void as return type.


When you found min...
There is no need to compare it with them.

See.. When min is found add a line...
j = i;

Now use switch case...
..
No need to compare.

Alternately... Use a string array..

Then print str;
Where str[1] = North.
Title: Re: My code needs a tune up.
Post by: Xires on September 27, 2012, 10:52:31 AM

I'm too tired to go over everything right now.  Just study it.  I'll answer any questions on it later.  You might want to look up the 'switch' statement above all else.

Code: (cpp) [Select]
#include <iostream>

using namespace std;

int accident[5];

void getNumAccidents(void) {
    int n, count;

    cout << "How many accidents were in the North, South, East, West\n"
        << "and Central parts of town, respectively?"
        << endl;

    for (n = 0, count = 1; count <= 5; ++n, ++count) {
        cout << count << ".\t";
        cin >> accident[n];

        while (accident[n] < 0) {
            cout << "Please enter a valid number. "
            cin >> accident[n];
        }
    }

    return;
}

void findLowest(void) {
    int min = 0;

    for (int i = 1; i < 5; ++i)
        if (accident[min] > accident[n])
            min = i;

    cout << endl;

    switch (min) {
        case 0:
            cout << "The North side of town has the safest driving area with " << accident[min] << " accidents." << endl;
            break;
        case 1:
            cout << "The North side of town has the safest driving area with " << accident[min] << " accidents." << endl;
            break;
        case 2:
            cout << "The North side of town has the safest driving area with " << accident[min] << " accidents." << endl;
            break;
        case 3:
            cout << "The North side of town has the safest driving area with " << accident[min] << " accidents." << endl;
            break;
        default:
            cout << "The Central part of town has the safest driving area with " << accident[min] << " accidents." << endl;
    }

    return;
}

int main(void) {
    getNumAccidents();
    findLowest();

    return 0;
}