Author Topic: My code needs a tune up.  (Read 2159 times)

0 Members and 1 Guest are viewing this topic.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
My code needs a tune up.
« 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;
}
« Last Edit: September 26, 2012, 06:50:45 am by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline Live Wire

  • Knight
  • **
  • Posts: 189
  • Cookies: 4
  • Up on your Net
    • View Profile
Re: My code needs a tune up.
« Reply #1 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 :)
"There is no right or wrong, there is only fun and boring."

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: My code needs a tune up.
« Reply #2 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.

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: My code needs a tune up.
« Reply #3 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.
"Always have a plan"

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: My code needs a tune up.
« Reply #4 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;
}
-Xires