Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Q1: Write a program that has a declaration in main() to store the following numbers into an array

named rates: 6, 2, 4, 7, 1, 9. There should be a function called avg_range() that accepts the
rates array and its size and then returns the average and the range of the array. Display the average
and range of the array in main().

Q2: Write a function called larger() that returns the latter of any two dates passed to it. For
example, if the dates 10/9/2005 and 11/3/2005 were passed to larger(), the second date would be
returned. Date structure:
struct Date
{
int month;
int day;
int year;
};

Q3. A text file containing the following data without headings:

Names
Callaway
Hanson
Lasard
Stillman

Hourly Rate
6.00
5.00
5.50
8.00

Hours Worked
40
48
35
50

Write a program that produces the following pay report for each employee:
Name, Pay Rate, Hours, Regular Pay, Overtime Pay, Gross Pay
Regular pay is to be computed as any hours worked up to and including 40 hours times the pay rate.
Overtime pay is to be computed as any hours worked above 40 hours times a pay rate of 1.5 times the
regular rate, and the gross pay is the sum of regular and overtime pay. At the end of the report, the
program should display the totals of the regular, overtime and gross pay columns.

// Q2
#include <iostream>
#include <iomanip>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
Date larger(Date date1, Date date2);
int main()
{
Date a = {10, 9, 2006};
Date b = {11, 3, 2006};
cout << "Date a: " << a.month << '/';
cout << a.day << '/' << a.year << endl << endl;
cout << "Date b: " << b.month << '/';
cout << b.day << '/' << b.year << endl << endl;
Date c = larger(a, b);
cout << "The larger of these two Dates is " << c.month << '/';
cout << c.day << '/' << c.year << endl << endl;
cin.ignore();

// this line is optional

return 0;
}
Date larger(Date date1, Date date2)
{
// Assume that all months have 30 days and all years have 365 days
long d1 = date1.day + 30*date1.month + 365*date1.year;
long d2 = date2.day + 30*date2.month + 365*date2.year;
if (d1 > d2)
return date1;
else
return date2;
}

// Q3
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
// needed for exit()
#include <string>
using namespace std;
int main()
{
string filename
string name;

= "pay.dat";

double rate, regPay, overPay, grossPay;


double regTot = 0, overTot = 0, grossTot = 0;
int hours;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// read and display the file's contents
cout << fixed << setprecision(2);
cout << "Name
Pay Rate
Hours
Regular Pay
Gross Pay" << endl;
cout << "-----------------------------------" << endl;
inFile >> name >> rate >> hours;
while (inFile.good())
{
if(hours > 40)
{
regPay = 40 * rate;
overPay = (hours - 40) * 1.5 * rate;
}
else
{
regPay = hours * rate;
overPay = 0;
}
grossPay = regPay + overPay;
regTot = regTot + regPay;
overTot = overTot + overPay;
grossTot = grossTot + grossPay;
cout << setw(11) << name << "
"
<< rate << "
" << setw(2)
<< hours << "
$"
<< regPay << "
$" << setw(6)
<< overPay << "
$"
<< grossPay << "
" << endl;;
inFile >> name >> rate >> hours;
}

cout << "-------------------------" << endl;


cout << "Totals:
overTot << "
$" << grossTot << endl;
inFile.close();
cin.ignore();
return 0;
}

// this line is optional

-----------

Overtime Pay
------------

----

------------

----

$" << regTot << "

$" <<

You might also like