OOPinCPP - Chapter 3- Solutions

Embed Size (px)

Citation preview

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    1/10

    Solutions to Exercises (Chapter No. 3

    Loops and Decisions)

    Object Oriented Programming in C++ by Robert Lafore

    Solutions provided by Muhammad Anas

    Question No. 1

    #include#include

    usingnamespace std;

    int main(){

    char exit = 'n';do {int number;cout > number;for (int i=1; i

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    2/10

    cout choice;if (choice == 1){

    cout > f;c = 5.0/9.0*(f - 32);cout

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    3/10

    } while (exit != 'y' && exit != 'Y');return 0;

    }Question No. 4

    #include

    usingnamespace std;

    int main(){

    char exit = 'y';do {float first_num, second_num, result;char operation;cout > first_num >> operation >> second_num;switch(operation){case'+':

    result = first_num + second_num;cout

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    4/10

    for (int j = 1; j

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    5/10

    cin >> initial_amount;cout > years;cout > interest_rate;for (int n=1; n > dot >> pence1;cout > pounds2 >> dot >> shillings2 >> dot >> pence2;pencer = pence1 + pence2;shillingsr = shillings1 + shillings2;poundsr = pounds1 + pounds2;if (pencer >= 12){

    pencer -= 12;shillingsr++;

    }if (shillingsr >=20){

    shillingsr -= 20;poundsr++;

    }cout

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    6/10

    cin >> exit;} while (tolower(exit) != 'y');return 0;

    }Question No. 9

    #include

    usingnamespace std;

    int main(){

    char exit = 'n';do {int num_of_guests, num_of_chairs;double possible_arrangements;cout > num_of_chairs;cout > num_of_guests;possible_arrangements = num_of_guests;for (int i = 1; i < num_of_chairs; i++){

    num_of_guests--;possible_arrangements *= num_of_guests;cout

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    7/10

    cout > interest_rate;while (initial_amount shillings1 >> dot >> pence1;cout > pounds2 >> dot >> shillings2 >> dot >> pence2;

    poundsr = pounds1 + pounds2;shillingsr = shillings1 + shillings2;pencer = pence1 + pence2;if (pencer >= 12){

    shillingsr++;

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    8/10

    pencer -= 12;}if (shillingsr >= 20){

    poundsr++;shillingsr -= 20;

    }cout > dot >> pence1;cout > pounds2 >> dot >> shillings2 >> dot >> pence2;

    poundsr = pounds1 - pounds2;shillingsr = shillings1 - shillings2;

    pencer = pence1 - pence2;/*while (poundsr < 0 || shillingsr < 0 || pencer < 0){*/

    if (pencer < 0){

    shillingsr--;pencer = 12 + pencer;

    }if (shillingsr =0){

    poundsr--;}else{

    poundsr++;}shillingsr = 20 + shillingsr;

    }/*}*/cout dot >> pence1;

    cout > float_num;decimal_pounds = ((((pounds1 * 20) + shillings1) * 12) +

    pence1)/(20*12);decimal_pounds *= float_num;//multiply amount entered with

    the floating point numberpoundsr = static_cast(decimal_pounds);fractional_part = decimal_pounds - poundsr;decimal_shillings = fractional_part * 20;shillingsr = static_cast(decimal_shillings);

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    9/10

    fractional_part = decimal_shillings - shillingsr;pencer = fractional_part * 12;cout operation >> c >> slash >> d;switch(operation){case'+':

    e = a*d + b*c;f = b*d;cout

  • 8/7/2019 OOPinCPP - Chapter 3- Solutions

    10/10

    return 0;}