Вы находитесь на странице: 1из 4

Laboratory Exercises : 092208 1.

Ask the students to encode the payroll program in their prefinals exam and they must be able to incorporate any necessary corrections. The project name is Prefinals and the cpp file name is Prefinals. Save both the project file and cpp file in my documents under the folder BCS301-LPT. The following questions must also be answered using the corrected program. a. Given a payrate of 250.00, number of hours worked 45, Employee code = a, and state code = y: i. What is the Overtime Pay value? ii. What is the Tax value? iii. How much is the Net Pay? b. After the program calculates the first entries, make the program process another employee with a payrate of 300.00, number of hours worked 30, Employee code = b, and state code = j: i. How much is the regular pay? ii. Why is there no tax value? List the line numbers in the program indicated in your exam that causes a tax value of 0.00 to be generated. iii. What program structure causes the program to repeat the entire process on a different employee? c. Answer no to the question if you want to process another employee and list down the required values: i. How much is the Total Gross Pay? ii. Why are the values Total Gross Pay, Total Number of Employees, Total Tax, and Total net pay displayed at the end of the program rather than at the end of every employee payroll calculation? List the line numbers that cause this program behavior. iii. In line 51, the formula total_gross_salary += gross_salary is a shortened version of the expression total_gross_salary = total_gross_salary + gross_salary. How can this expression accumulate the total gross salary? Explain by using the entries stated in questions a and b.

2.

Lecture : (To be photocopied by each student as additional reference) const = a keyword used a special type of variable called a CONSTANT that contains a value that cannot be changed. The difference between a constant and a normal variable is that the value in the constant is protected from changes that are normally done during the run of a program. Syntax: const data_type constant_name = value; Example : const double inchesToCentimeters = 2.54; Functions in C++ A function is a group of statements that together perform a task. Functions implement the concept of modularity wherein a program is composed of modules or groups of codes rather than just a single massive list of codes. Each group of code is focused on performing one task only. Implementing any function in addition to main involves two steps: 1. Defining the function a. The function header consists of a return type, a function name, and an argument list. A function header always is followed by an open curly brace, which begins the function body. The function body ends with a close curly brace. The function body consists of one or more statements. The function body must contain a return statement unless the return type is void, in which case the return statement is optional. A function cannot execute until it is first defined. Once defined, a function executes when it is called. Syntax : return_type functionName (arguments list) { // body of the function } Example1 : void displayMsg1() { cout << This message will be displayed when the function DisplayMsg1 is called << endl ; } Example2 : string displayMsg2() { return This message will be displayed when the function DisplayMsg2 is called.; } Example3 : string displayMsg3(string part1msg, string part2msg) { string whole_msg; whole_msg = part1msg + + part2msg; return whole_msg; } 2. Calling the function a. Normally, a function is called through code. The main function is the exception. The main function is called automatically when your program begins to run. Example: // program calling all 3 functions declared above #include <cstdlib> #include <iostream> #include<string> using namespace std; void displayMsg1() { cout << "This message will be displayed when the function DisplayMsg1 is called" << endl ; } string displayMsg2() { return "This message will be displayed when the function DisplayMsg2 is called."; } string displayMsg3(string part1msg, string part2msg) {

string whole_msg; whole_msg = part1msg + " " + part2msg; return whole_msg; } int main(int argc, char *argv[]) { char optionSelected, repeatProgram; string txtpart1, txtpart2; repeatProgram = 'Y'; while ((repeatProgram == 'Y') || (repeatProgram == 'y')) { cout << "**************************************** \n"; cout << "Enter an option: \n"; cout << "Press 1 to run Function displayMsg1. \n" ; cout << "Press 2 to run Function displayMsg2. \n" ; cout << "Press 3 to run Function displayMsg3. \n" ; cout << "Press any other key to end the program. \n"; cout << "**************************************** " <<endl; cin >> optionSelected; switch (optionSelected) { case '1': cout << "\n" << "\n"; displayMsg1(); cout << "\n" << "\n"; break; case '2': cout << "\n" << "\n"; cout << displayMsg2(); cout << "\n" << "\n"; break; case '3': cout << "\n" << "\n"; txtpart1 = "A sample function "; txtpart2 = " that accepts arguments"; cout << displayMsg3(txtpart1, txtpart2); cout << "\n" << "\n"; break; } if (optionSelected == '1' || optionSelected == '2' || optionSelected == '3') { cout << "Would you like to run the program again?"; cin >> repeatProgram; } else { repeatProgram = 'n' ; } } cout << "\n Exiting Program \n"; system("PAUSE"); return EXIT_SUCCESS; } b. A function can be called several ways depending on what type of function you are calling: i. A function declared with a void return type and no arguments can be called by coding the function name followed by an open and close parenthesis then a semicolon. functionName();

ii.

A function declared with a non void return type and no arguments can be called by first declaring a variable to contain the return value of the function and then assigning the function to the variable: variable = functionName(); A function declared either void or non void return type but with arguments can be called by inserting the parameters inside the open and close parenthesis after the function name; functionName(variableName, actualValue, 3); // function sample with void return type variable = functionName(variableName, actualValue, 3); // function sample with non void // return type

iii.

Example #include <cstdlib> #include <iostream> #include<string> using namespace std; int Calc_Grade(int midterm, int final) { const double MIDTERM_WEIGHT = 0.40; const double FINAL_WEIGHT = 0.60; double grade; int rounded_grade; grade = MIDTERM_WEIGHT * midterm + FINAL_WEIGHT * final; rounded_grade = grade + 0.5; return rounded_grade; } int main() { int midterm_exam, final_exam, final_grade; cout << "Enter the Midterm Exam score: "; cin >> midterm_exam; cout << endl; cout << "Enter the Final Exam score: "; cin >> final_exam; final_grade = Calc_Grade(midterm_exam, final_exam); cout << endl; cout << "The Final Grade is " << final_grade << endl; cout << endl; return 0; }

Вам также может понравиться