SECJ1013-10 Programming Technique 1

Assignment 2

The program will determine the winner of a sales contest. The salesperson who sold the most units wins the contest. Read the salesperson ID number and the number of units sold by each salesperson. Determine and display the winner of the contest. If the winner sold more than or equal to 1000 units, display “excellent”. If less than 1000 but greater than 749 units, display “very good”, otherwise display “good”. The program should provide a mechanism to control the loop of reading input. For example, the program will keep reading input until the user enters sentinel number. Another example is that, the user is firstly asked for the number of person he or she wants to enter. User-defined Functions: 

The program should provide the following functions:
a. getWinner to determine and display the winner (the salesperson who sold the most units).
b. displayStatus to display excellent, good or very good
Note: You may want to define other functions if necessary. Use appropriate parameters/ arguments, pass by value or pass by reference. You are not allowed to use global variables.

Code

#include <iostream>
using namespace std;

int getWinner (int [], int [], int);
void displayStatus (int);

int main()
{
          int i,numPeople,highestSales;

          cout<<"Enter the number of salesperson: "<<endl;
          cin>>numPeople;

          int personID[numPeople];
          int sales[numPeople];

     for (i=0;i<numPeople;i++)
     {
          cout<<"\nEnter salesperson ID: "<<endl;
          cin>>personID[i];
          cout<<"Enter sales: "<<endl;
          cin>>sales[i];
     }

          highestSales=getWinner(personID, sales, numPeople);
          displayStatus(highestSales);
}

int getWinner (int ID [], int sold [], int people)
{
          int highest=0, winner, x;
     for (x=0;x<people;x++)
     {
          if (sold[x]>highest)
          {
               highest=sold[x];
               winner=ID[x];
          }
     }
     cout<<"\nSalesperson who sold the most units: \n"<<winner<<endl;
     return highest;
}

void displayStatus (int salesNumber)
{
     if (salesNumber>=1000)
          cout<<"\nExcellent";
     else if (salesNumber>749)
          cout<<"\nVery good";
     else
          cout<<"\nGood";
}

Code (.cpp)