16
COM 235 Engineering Computer Programming I Revision Assignment Solution of Some Problems Dr. Shereen Mohamed & Dr. Essam Abdrabou 1

Revision_Assign_Sol.ppt

Embed Size (px)

Citation preview

  • COM 235Engineering Computer Programming I

    Revision Assignment Solution of Some ProblemsDr. Shereen Mohamed & Dr. Essam Abdrabou*

    *

  • Determine and correct the errors in the following program*#include using namespace std;int main(){width = 15area = length * width;cout
  • Determine and correct the errors in the following program*Variable area, width, and length are used without being initialized. They are given rubbish values by the compiler.The end bracket is missing.

    Correction

    #include using namespace std;int main(){int length, width, area;area = length * width;length = 20;width = 15;cout

  • #include int main(){int length = 20; width = 15, area;length * width = area;cout
  • Determine the value of the following expressions (true or false) , assuming a = 5, b = 2, c = 4, d = 6, and e = 3.*a > b

    true5 > 2a != b

    true5 2d% b == c%b

    true(6 mod 2) is equal to (4 mod 2)0 is equal to 0a * c != d * b

    true5 x 4 6 x 220 12d* b == c*e

    true(6 x 2) is equal to (4 x 3)12 is equal to 12!(a* b)

    falseNOT any non zero value is zero0!(a% b*c)

    falseNOT ((5 mod 2) x 4)0!(c% b*a)

    trueNOT ((4 mod 2) x 4)1

    *

  • Using parentheses, rewrite the following expressions to indicate their order of evaluation correctly. Then evaluate each expression, assuming a = 5, b = 2, and c = 4.a % b * c && c % b * a((5 % 2) * 4) && ((4 % 2) * 5) ( ) ParenthesesL to R1 ++, --PostincrementL to R2 ++, -- PreincrementR to L3 !Logical NOTL to R3 +, -Positive, negativeL to R3 *, /, % Multiplication, division L to R4 +, -Addition, subtractionL to R5 =, >, < Relational operator L to R6 ==, != Relational operator L to R7 && Logical ANDL to R8 || Logical ORL to R9+=, -+, *=, /=, %=Compound assignment R to L10 = AssignmentR to L10((1) * 4) && ((0) * 5) (4) && (0) 0

    false

    *

  • Using parentheses, rewrite the following expressions to indicate their order of evaluation correctly. Then evaluate each expression, assuming a = 5, b = 2, and c = 4.a % b * c || c % b * a((5 % 2) * 4) || ((4 % 2) * 5) ( ) ParenthesesL to R1 ++, --PostincrementL to R2 ++, -- PreincrementR to L3 !Logical NOTL to R3 +, -Positive, negativeL to R3 *, /, % Multiplication, division L to R4 +, -Addition, subtractionL to R5 =, >, < Relational operator L to R6 ==, != Relational operator L to R7 && Logical ANDL to R8 || Logical ORL to R9+=, -+, *=, /=, %=Compound assignment R to L10 = AssignmentR to L10((1) * 4) || ((0) * 5) (4) || (0) 1

    true

    *

  • Using parentheses, rewrite the following expressions to indicate their order of evaluation correctly. Then evaluate each expression, assuming a = 5, b = 2, and c = 4.b % c * a && a % c * b((2 % 4) * 5) && ((5 % 4) * 2) ( ) ParenthesesL to R1 ++, --PostincrementL to R2 ++, -- PreincrementR to L3 !Logical NOTL to R3 +, -Positive, negativeL to R3 *, /, % Multiplication, division L to R4 +, -Addition, subtractionL to R5 =, >, < Relational operator L to R6 ==, != Relational operator L to R7 && Logical ANDL to R8 || Logical ORL to R9+=, -+, *=, /=, %=Compound assignment R to L10 = AssignmentR to L10((0) * 5) && ((1) * 2) (0) && (2) 0

    false

    *

  • Using parentheses, rewrite the following expressions to indicate their order of evaluation correctly. Then evaluate each expression, assuming a = 5, b = 2, and c = 4.b % c * a || a % c * b((2 % 4) * 5) || ((5 % 4) * 2) ( ) ParenthesesL to R1 ++, --PostincrementL to R2 ++, -- PreincrementR to L3 !Logical NOTL to R3 +, -Positive, negativeL to R3 *, /, % Multiplication, division L to R4 +, -Addition, subtractionL to R5 =, >, < Relational operator L to R6 ==, != Relational operator L to R7 && Logical ANDL to R8 || Logical ORL to R9+=, -+, *=, /=, %=Compound assignment R to L10 = AssignmentR to L10((0) * 5) || ((1) * 2) (0) || (2) 1

    true

    *

  • Write relational expressions to express the following conditions (using variable names of your choosing):The distance is equal to 30 feet.(distance == 30)The ambient temperature is 86.4 degrees.(temp == 30)A speed is 55 mph.(speed == 55)The letter input is K.(input == 'K')A length is greater than 2 feet and less than 3 feet(len > 2 && len < 3)An automobiles speed is greater than 50 mph and it has been moving for at least 5 hours.(speed > 50 && time >= 5)

    *

  • Rewrite the following if-else chain by using a switch statementif (factor == 1)pressure = 25.0;else if (factor == 2)pressure = 36.0;else if (factor == 3)pressure = 45.0;else if ((factor == 4) || (factor == 5) || (factor == 6))pressure = 49.0;

    *

  • Rewrite the following if-else chain by using a switch statementswitch (factor){case 1:pressure = 25.0;break;case 2:pressure = 36.0;break;case 3:pressure = 45.0;break;case 4:case 5:case 6:pressure = 49.0;break;}}Dont forget the break statement.If break is not written, the code will continue to execute the next statement.The code will execute if factor has any of the values (4, 5, or 6)

    *

  • Write a C++ program that accepts the code number as input and, based on the value entered, displays the correct disk drive manufacturer. Each disk drive in a shipment is stamped with a code from 1 through 4 to indicate the manufacturer, as follows:1- Read the code2- Switch case on the code value3- According to the value display the manufacturer.

    *

  • *#include // include the input output stream libraryusing namespace std;// go to the definition of the std sectionvoid main()// define the main function{int code = 0;// declare the variable codecout > code;// get the value from the keyboardswitch (code)// decision based on the value of the variable{case 1:cout
  • The value of Eulers number, e, can be approximated by the following formula. Using this formula, write a C++ program that approximates the value of e for 50 terms 1- Initialize Eulers number by 12- Start by i = 1 to 503- Calculate the factorial of i4- Add each factorial inverse to the Eulers number5- Print the value.

    *

  • *//Eurler's Number Approximation#include // include the input output stream libraryusing namespace std;// go to the definition of the std sectionvoid main()// define the main function{const int MAX = 50;// required limitdouble euler_number = 1.0;//euler's numberdouble fact_i = 1.0; // factorial of iint j;

    for (int i = 1; i 1)// factorial loopfact_i *= j--;euler_number += 1.0/fact_i; // Euler's Formula}cout