Question 1
What is the output of the following program given in Listing ? In your answer use "\0" to indicate the null character output by the program.
#include <iostream>
using namespace std;
int main()
{
int n = 1, * p;
p = &n;
char aString[] = { "student" };
for (int i = 0;i < 5; i++)
cout<< " i= " << i << " n*n= " << n * n << " n++= " << n++ << " *p= " << *p << endl;"
for (int i = 0;i <= 7;i++)
cout<< "Element at index " << i << " is " << aString[i] << endl;
return 0;"
}
Answer
i= 0 n*n= 4 n++= 1 *p= 2
i= 1 n*n= 9 n++= 2 *p= 3
i= 2 n*n= 16 n++= 3 *p= 4
i= 3 n*n= 25 n++= 4 *p= 5
i= 4 n*n= 36 n++= 5 *p= 6
Element at index 0 is s
Element at index 1 is t
Element at index 2 is u
Element at index 3 is d
Element at index 4 is e
Element at index 5 is n
Element at index 6 is t
Element at index 7 is "\0"
Question 2
As appropriate amend the following C++ program given below, in order to define and use a struct data structure called Driver, which will hold a given driver Age, claims and premiumDue variables. Their data type should remain the same as in the current version of this program. Amendments are given as questions a), b), c) and d)
#include <iostream>
using namespace std;
int main()
{
int driverAge, claims;
double premiumDue = 75.32;
cout << "Enter the driver age: ";
cin >> driverAge;
cout <<"\nEnter the number of insurance claims the driver made in the last 3 years: ";
cin >> claims;
if (driverAge < 26)
{
claims < 3 ? (premiumDue += 100) : (premiumDue += 170);
}
else if (driverAge < 70)
{
cout << " 70 bractket";
claims >= 3 ? (premiumDue += 70) : (premiumDue += 50);
}
cout << "\n your premium is :" << premiumDue << endl;
return 0;
}
a)
After line 7 in the above program) add an if else statement to only accept values for the driverAge variable which are between 16 and 70 inclusive.
Answer
#include <iostream>
using namespace std;
int main()
{
int driverAge, claims;
double premiumDue = 75.32;
cout << "Enter the driver age: ";
cin >> driverAge;
if ((driverAge < 16) || (driverAge > 70))
{
cout << "Invalid age please reenter";
}
else
{
cout << "\nEnter the number of insurance claims the driver made in the last 3 years: ";
cin >> claims;
if (driverAge < 26)
{
claims < 3 ? (premiumDue += 100) : (premiumDue += 170);
}
else if (driverAge < 70)
{
cout << " 70 bractket";
claims >= 3 ? (premiumDue += 70) : (premiumDue += 50);
}
cout << "\n your premium is :" << premiumDue << endl;
return 0;
}
}
b)
Based on the program given above, you are required to include a while loop after line 9 to only accept values for the claims variables which are between 0 and 5 inclusive. Also the program should loop until the user has entered a value for the claims variable.
Answer
#include <iostream>
using namespace std;
int main()
{
int driverAge, claims;
double premiumDue = 75.32;
cout << "Enter the driver age: ";
cin >> driverAge;
if ((driverAge < 16) || (driverAge > 70))
{
cout << "Invalid age please reenter";
}
else
{
cout << "\nEnter the number of insurance claims the driver made in the last 3 years: ";
cin >> claims;
while ((claims <0)|| (claims > 5))
{
cout << "Invalid enter value between 0 -5 ";
cin >> claims;
}
if (driverAge < 26)
{
claims < 3 ? (premiumDue += 100) : (premiumDue += 170);
}
else if (driverAge < 70)
{
cout << " 70 bractket";
claims >= 3 ? (premiumDue += 70) : (premiumDue += 50);
}
cout << "\n your premium is :" << premiumDue << endl;
return 0;
}
}
c)
Based on the program defined above, you are now required to introduce a function, which implements the behaviour defined from line 6 to Line 9. The function should be declared as follows.
It should be called userInput.
It should have void as its return type.
Should take (02) arguments: &driverAge and &claims (reference variables)
The function should be called from the main function.
The function prototype and implementation should be contained in the same file as the main function.
Answer
#include <iostream>
using namespace std;
void userInput(int & driverAge, int & claims);
int main()
{
int driverAge, claims;
double premiumDue = 75.32;
userInput(driverAge, claims);
if (driverAge < 26)
{
claims < 3 ? (premiumDue += 100) : (premiumDue += 170);
}
else if (driverAge < 70)
{
cout << " 70 bractket";
claims >= 3 ? (premiumDue += 70) : (premiumDue += 50);
}
cout << "\n your premium is :" << premiumDue << endl;
return 0;
}
void userInput(int & driverAgeand, int & claims)
{
cout << "Enter the driver age: ";
cin >> driverAge;
if ((driverAge < 16) || (driverAge > 70))
{
cout << "Invalid age please reenter";
}
else
{
cout << "\nEnter the number of insurance claims the driver made in the last 3 years: ";
cin >> claims;
while ((claims < 0) || (claims > 5))
{
cout << "Invalid Number Enter value between 0 -5 ";
cin >> claims;
}
}
}
d)
Define a class called Driver, which has the following:
Three private data members: int driverAge, int claims, float premiumDue.
A set of public member functions including:
userInput with an implementation similar to that of the function you have developed in Previous Qusetion Though, you can vary the return type and argument list to suit your needs.
A constructor function (with argument list of your choice).
A required setters and getters functions, anything else can be ignored for this question.
Answer
#include <iostream>
using namespace std;
Class Driver
{
private:
int driverAge, claims;
double premiumDue = 75.32;
public:
Driver(float pDue= 75.32)
{
premiumDue = pDue;
}
void userInput(int driverAge, int claims)
{
cout <<"Enter the driver age: ";
cin >> driverAge;
if ((driverAge < 16) || (driverAge > 70))
{
cout << "Invalid age please reenter";
}
else
{
cout << "\nEnter the number of insurance claims the driver made in the last 3 years: ";
cin >> claims;
while ((claims < 0) || (claims > 5))
{
cout << "Invalid enter value between 0 -5 ";
cin >> claims;
}
}
}
}
No comments:
Post a Comment