Q1) Identify the following items in the programming code shown... Function prototype Function heading

advertisement
C++-firstExam-2015
Q1) Identify the following items in the programming code shown below:
Function prototype
Function heading
Function body
Function definitions
void hello(int&, double, char);
1.void hello(int& first, double
second, char ch)
2. void hello(int&, double, char);
{
int num;
void hello(int& first, double
second, char ch)
{
double y;
int num;
int u ;
double y;
…
int u ;
}
…
}
Function call statement
Formal parameters
1. hello(x, y, z);
(int&, double, char)
Actual
parameter
(x, y, z)
2. hello(x, y - 3.5, 'S');
Reference parameter
int& first
Local variables
int num;
int one;
void hello(int&, double, char);
…
int main()
{
int x;
double y;
char z;
…
hello(x, y, z);
…
hello(x, y - 3.5, 'S');
…
}
void hello(int& first, double second, char ch)
{
int num;
double y;
int u ;
…
}
Global variables
int one;
Value parameter
(x, y - 3.5, 'S')
Q2) You are asked to implement a simple Tax System for Citizens. To calculate the Tax for each citizen;
assume the following:
1- Citizen Single and his Salary <= 500  Tax = Salary * 0.01
2- Citizen Single and his Salary > 500 < 1000  Tax = Salary * 0.05
3- Citizen Single and his Salary >= 1000  Tax = Salary * 0.15
4- Citizen Married and his Salary <= 500  Tax = 0
5- Citizen Married and his Salary > 500 < 1000  Tax = Salary * 0.02
6- Citizen Married and his Salary >= 1000  Tax = Salary * 0.09
Write the C++ statements for the following:
1- A function to input citizen type (Single or Married), and return that value.
Answer
char cStatus( )
{
char citizenStatus;
cout << "Enter citizen type (s) for Single or (m) for Married: ";
cin >>citizenStatus;
return citizenStatus;
};
2- A function to input the citizen’s salary amount, and return that value.
Answer
float citizensalaray()
{
float sal;
cout << "Enter employee salary: ";
cin >> sal;
return sal;
};
3- A function to calculate the Tax for that citizen, and return that value.
Answer
float taxCalc(float salary, float tax)
{
return salary * tax;
};
4- Define the variables needed in the main C++ function to call your defined functions.
5- Print the final Tax amount for that citizen.
Answer 4 and 5
#include <iostream>
using namespace std;
char cStatus( )// Answer 1. function to read citizen status
{
char citizenStatus;
cout << "Enter citizen type (s) for Single or (m) for Married: ";
cin >>citizenStatus;
return citizenStatus;
};
float citizensalaray()//Answer 2. function to read citizen salary
{
float sal;
cout << "Enter employee salary: ";
cin >> sal;
return sal;
};
float taxCalc(float salary, float tax)//Answer 3. function to calculate tax
{
return salary * tax;
};
int main()
{
char status;//Answer 4. the following are declaration variables
float tax, salary;
salary = citizensalaray();//calling salary function
status = cStatus();//calling status function
if (status == 's')// Answer 4. for printing the final result which is tax
{
if (salary <= 500)
tax = taxCalc(salary, 0.01);// calling tax function
else if (salary > 500 && salary < 1000 )
tax = taxCalc(salary, 0.05); // calling tax function
else if (salary >= 100 )
tax = taxCalc(salary, 0.15); // calling tax function
}
else if (status == 'm')
{
if (salary <= 500)
tax = 0;
else if (salary > 500 and salary < 1000 )
tax = taxCalc(salary, 0.02); // calling tax function
else if (salary >= 100 )
tax = taxCalc(salary, 0.09); // calling tax function
}
else
cout << "Sorry no such status" << endl;
cout << "Tax for employee is: "<< tax << endl;
return 0;
}
Q3) You are asked to implement a simple
Answer
#include <iostream>
using namespace std;
int main()
{
struct invoice
{
int iInvoice_no;
string customer_nam;
struct drug_type
{
string type1,type2,type3;
}d1;
}Inv1;
cout << "this is just the declaration of the structure invoice"
return 0;
}
Download