Sie sind auf Seite 1von 5

CSC415 CHAPTER - REPETITION

Exercise 1:

A student’s council at a particular university held a fund-raising campaign for MAKNA (Cancer
Association). A total of 100 students participate in this campaign.

You need to write a program that will input the student’s faculty code and he/her contribution
amount to MAKNA. The program then will calculate the number of contributors, the total
amount contributed and the average contribution for each faculty.

There are only three faculties involved which are: (C) - Computer, (B) - Business and (A) -
Accounting. Your program also needs to take care if the user enters a wrong faculty name.

The sample output as follows:-

Faculty Total Contribution Amount Number of contributions Average


C – Computer RM99999.99 999 RM99999.99
B – Business RM99999.99 999 RM99999.99
A – Accounting RM99999.99 999 RM99999.99

I = faculty code and contribution amount (100 times)

P = 1) Repeat for 100 times


- Count the number of contributors for each faculty code
- Sum the contribution amount for each faculty code
2) find the average contribution for each faculty code = sum/count for each faculty code

O = sum of contribution for each faculty code


Number of contributors for each faculty code
Average for each faculty code

It is a counter-controlled loop (100 students participate the program)


- Identify the counter which is a LCV – let say we name it i
- Initial value for LCV is  i = 0
- Evaluation - condition to stop the loop – the stop value is 100, therefore the LCV
must be less than 100  i < 100 (it is < than since our initial value is 0)
- Update value for LCV  i = i + 1

Since we have identified all those info above, then we are ready to code in C++ (I am
using Dev C++ 5.4.0)
CSC415 CHAPTER - REPETITION

#include <iostream.h>

int main()
{
//declaration all variables
char faculty_code;
float contribution_amt;
int totalC, totalB, totalA; //variable to hold value for total number of contributors for each faculty code
float sumC, sumB, sumA; //variable to hold value for sum of contribution amount for each faculty code
float avgC, avgB, avgA; //variable to hold value for average amount for each faculty code
int i; //LCV - counter since this is counter-controlled loop

//initialize the necessary variables such as the accumulators (totalC, totalB, totalA, sumC,
sumB, sumA)
totalC = 0;
totalB = 0;
totalA = 0;
sumC = 0;
sumB = 0;
sumA = 0;

//initialize LCV
i = 0;

while (i < 5)
{
//input faculty code and contribution amount

cout << "Enter the faculty code (C, B or A) ? ";


cin >> faculty_code;

cout << "Enter the contribution amount : ";


cin >> contribution_amt;

//check the faculty code entered to count and sum accordingly

if (faculty_code == 'C')
{
totalC = totalC + 1; //count the contributor for faculty code C
sumC = sumC + contribution_amt; //sum the contribution amount for faculty code C
}
else
if (faculty_code == 'B')
{
totalB = totalB + 1; //count the contributor for faculty code B
sumB = sumB + contribution_amt; //sum the contribution amount for faculty code B
}
else
if (faculty_code == 'A')
{
totalA = totalA + 1; //count the contributor for faculty code A
sumA = sumA + contribution_amt; //sum the contribution amount for faculty code A
}
else
cout << "Error faculty code entered!!";
CSC415 CHAPTER - REPETITION

//update LCV

i = i + 1;
}

//calculate the average after exit the loop

avgC = sumC / totalC;


avgB = sumB / totalB;
avgA = sumA / totalA;

//output all the neccessary information

cout << "FACULTY\t\t\tTOTAL CONTRIBUTION AMOUT\tNUMBER OF


CONTRIBUTORS\tAVERAGE\n";
cout << "-------\t\t\t------------------------\t----------------------\t-------\n";
cout << "C - Computer\t\t\tRM" << sumC << "\t\t\t" << totalC << "\t\t\tRM" << avgC <<
endl;
cout << "B - Business\t\t\tRM" << sumB << "\t\t\t" << totalB << "\t\t\tRM" << avgB << endl;
cout << "A - Accounting\t\t\tRM" << sumA << "\t\t\t" << totalB << "\t\t\tRM" << avgB <<
endl;

return 0;
}
CSC415 CHAPTER - REPETITION

Exercise 2:

Write a C++ program to help a manager in calculating the total amount of salary that he has
to pay and the average amount of salary.. The program will input the payment rate per hour
and the number of hours that employee works.

The sample is as below :

Enter hours worked (-1 to end) : 40


Enter hourly rate : 4.00

Enter hours worked (-1 to end) : 50


Enter hourly rate : 5.00

Enter hours worked (-1 to end) : -1

The number of workers is 2


The total amount to be paid is RM4100
The average amount per worker is RM205

For the sample output, to end/exit the loop by entering -1 for hours worked. So hours
worked is controlling the loop and the sentinel value is -1.

Therefore, this is sentinel-controlled loop.

I = hours worked and rate per hour (for each employee)

P = 1) repeat until hours worked is -1


- Count the number of workers entered
- Payment paid for each employee = hours worked * rate per hour
- Sum of the payment paid
2) calculate the average payment = sum payment / number of workers

O = number of workers, total amount paid, average amount paid

Sentinel-controlled loop
- Identify LCV = hours worked
- Initial value of LCV = input hours worked
- Evaluation (exit the loop until user enters -1)  hours worked != -1
- Update value of LCV = input hours worked

Since we have identified all those info above, then we are ready to code in C++ (I am
using Dev C++ 5.4.0)
CSC415 CHAPTER - REPETITION

#include <iostream.h>

int main()
{
//declaration
int hours_worked;
float rate_per_hour;
int total_worker; //variable to count how many workers entered
float payment; //to calculate payment for each worker
float sumPayment; //variable to sum all the payment paid
float avgPayment; //variable to hold average payment

//initialize accumulators (total_worker and sumPayment)


total_worker = 0;
sumPayment = 0;

//initialize the LCV - input hours worked

cout << "Enter hours worked for each worker, -1 to exit : ";
cin >> hours_worked;

while (hours_worked != -1)


{
cout << "Enter rate per hour for each worker : ";
cin >> rate_per_hour;

payment = hours_worked * rate_per_hour; //calculate payment for each worker

total_worker = total_worker + 1; //count how many workers entered

sumPayment = sumPayment + payment; //sum all the payment made

//update LCV - input hours worked


cout << "Enter hours worked for each worker, -1 to exit : ";
cin >> hours_worked;
}

//calculate average after exit loop


avgPayment = sumPayment / total_worker;

//output the information needed


cout << endl << endl << "Number of workers is " << total_worker << endl;
cout << "Total amount to be paid is RM" << sumPayment << endl;
cout << "Average amount per worker is RM" << avgPayment << endl;

return 0;
}

Das könnte Ihnen auch gefallen