Author Topic: This piece of code works but.....i dunno it could have been better.  (Read 1259 times)

Bazoukas

  • Member
  • **
  • Posts: 866
  • Kudos: 140
    • http://whitehouse.com
This program will categorize each salesperson according to the sales he made: Enter the number of how many SalesPersons salary status you want to review.
Enter -1 ANYTIME to exit the program,
and to display and review the data you entered up to that point:3
Enter the amount of dollars in sales:   9000
Enter the amount of dollars in sales:   15000
Enter the amount of dollars in sales:   590
200_299            | 1
300_399            | 0
400_499            | 0
500_599            | 0
600_699            | 0
700_799            | 0
800_899            | 0
900_999            | 0
1000_1099            | 2


 And here is the code  


#include <iostream.h>
#include <iomanip.h>

int main () {
   
   int Number_Of_Workers;
   int ComisionRange1=200;
   int ComisionRange2=300-1;
   int Array_Comision[9];
   const int Base_Salary=200;
   int Comision, sales;
   
   for (int Array_loop=0; Array_loop<9; Array_loop++)
   {
      
      Array_Comision[Array_loop]=0;
   }
   
   cout<<"This program will categorize each salesperson according to the sales he made: ";
   cout<<"Enter the number of how many SalesPersons salary status you want to review.\n";
   cout<<"Enter -1 ANYTIME to exit the program,\n";
   cout<<"and to display and review the data you entered up to that point";
   cout<<":";
   cin>>Number_Of_Workers;
   
   for (int loop=1; loop<=Number_Of_Workers; loop++)
   {
      
      cout<<"Enter the amount of dollars in sales:\t";
      cin>>sales;
      if (sales==-1)
      {
         cout<<"This program will end now.\n";
         cout<<"--------------------------\n";
         cout<<"Comision Range------Grouping\n";
         cout<<"----------------------------\n";
         break;
         
      }
      
      Comision=Base_Salary+(9*sales/100);
      
      
      
      if (Comision >=200 && Comision<=299)
         ++Array_Comision[0];
       else if(Comision >=300 && Comision<=399)
         ++Array_Comision[1];
       else if  (Comision >=400 && Comision<=499)
         ++Array_Comision[2];
       else if  (Comision >=500 && Comision<=599)
         ++Array_Comision[3];
       else if  (Comision >=600 && Comision<=699)
         ++Array_Comision[4];
       else if  (Comision >=700 && Comision<=799)
         ++Array_Comision[5];
       else if  (Comision >=800 && Comision<=899)
         ++Array_Comision[6];
       else if  (Comision >=900 && Comision<=999)
         ++Array_Comision[7];
       else if (Comision >=1000)
         ++Array_Comision[8];
      
   }
   
   
   
   while (ComisionRange1<=900 && ComisionRange2<=1000)
      
{
   
   for (int Display_Array=0; Display_Array<9; Display_Array++)
   {
      
      cout<<ComisionRange1<<"_"<<ComisionRange2<<setw(14)<<" | " << Array_Comision[Display_Array]<<endl;
      ComisionRange1+=100;
      ComisionRange2+=100;
      
   }
   
}

return 0;

}

 So how would you write a program that goes Far beyond 999 - 1000 and it went all the way to 10,000 - 10,999

  Doing it my way, it would take A LOOOOOOOT of If else statements.

[ November 04, 2002: Message edited by: bazoukas ]

[ November 04, 2002: Message edited by: bazoukas ]

Yeah

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Replace that big if else block with:

Array_Comision[(int)((Comision - 200) / 100)]++;

e.g. if your commission is 770:
(770 - 200) / 100 = 5.7

The (int) casts this result to an integer, thus truncating it to 5 (the index of the array you want to write to.)
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


Bazoukas

  • Member
  • **
  • Posts: 866
  • Kudos: 140
    • http://whitehouse.com
quote:
Originally posted by flap:
Replace that big if else block with:

Array_Comision[(int)((Comision - 200) / 100)]++;

e.g. if your commission is 770:
(770 - 200) / 100 = 5.7

The (int) casts this result to an integer, thus truncating it to 5 (the index of the array you want to write to.)



Thats it?

 I'll be damed.
Yeah

Bazoukas

  • Member
  • **
  • Posts: 866
  • Kudos: 140
    • http://whitehouse.com
Holly shit dude. I never thought of that.  That was very clever indeed.

 Thank you man.
Yeah