Beruflich Dokumente
Kultur Dokumente
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.
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
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;
}
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.
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.
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
cout << "Enter hours worked for each worker, -1 to exit : ";
cin >> hours_worked;
return 0;
}