0% found this document useful (0 votes)
74 views16 pages

COM 235 Revision Assignment Solution of Some Problems: Engineering Computer Programming I

This document contains solutions to programming problems and explanations of errors in C++ code snippets. It begins by showing corrections made to a program that calculates the area of a rectangle without properly declaring variables. It then demonstrates fixing issues with variable initialization order and missing brackets. The document proceeds to discuss the order of operations for expressions and use of parentheses. It concludes by providing examples of programs that output disk drive manufacturers based on codes and approximate Euler's number using a formula.

Uploaded by

Abdelrahman Saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views16 pages

COM 235 Revision Assignment Solution of Some Problems: Engineering Computer Programming I

This document contains solutions to programming problems and explanations of errors in C++ code snippets. It begins by showing corrections made to a program that calculates the area of a rectangle without properly declaring variables. It then demonstrates fixing issues with variable initialization order and missing brackets. The document proceeds to discuss the order of operations for expressions and use of parentheses. It concludes by providing examples of programs that output disk drive manufacturers based on codes and approximate Euler's number using a formula.

Uploaded by

Abdelrahman Saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

COM 235

Engineering Computer Programming I

Revision Assignment Solution of


Some Problems
Dr. Shereen Mohamed & Dr. Essam Abdrabou
1

Determine and correct the errors in the following program


#include <iostream>
using namespace std;
int main()
{
width = 15
area = length * width;
cout<< "The area is " << area
}

Variable width is used without declaration


No semi colon at the end of the statement
Variable length is used without declaration
Variable area is used without declaration
No semi colon at the end of the statement
The main function ends with no return value

Correction
#include <iostream>
using namespace std;
int main()
{
int width = 15, length = 20, area;
area = length * width;
cout<< "The area is " << area;
return 0;
}

Determine and correct the errors in the following program


#include <iostream>
using namespace std;
int main()
{
int length, width, area;
area = length * width;
length = 20;
width = 15;
cout<< "The area is " << area;
return 0;

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 <iostream>
using namespace std;
int main()
{
int length, width, area;
length = 20;
width = 15;
area = length * width;
cout << "The area is " << area;
return 0;
}

Determine and correct the errors in the following program


Assignment statement goes from right to
#include <iostream.h>
left, not from left to right.
int main()
{
int length = 20; width = 15, area;
length * width = area;
cout<< "The area is " , area;
Stream insertion operator << is missing
return 0;
}

Correction
#include <iostream>
using namespace std;
int main()
{
int length = 20, width = 15, area;
area = length * width;
cout<< "The area is " << area;
return 0;
}

Determine the value of the following expressions (true or false) ,


assuming a = 5, b = 2, c = 4, d = 6, and e = 3.
a>b
d* b == c*e
5>2
true
(6 x 2) is equal to (4 x 3)
a != b
12 is equal to 12
true
52
true
!(a* b)
d% b == c%b
NOT any non zero value is zero
(6 mod 2) is equal to (4 mod 2)
0
false
0 is equal to 0
true
!(a% b*c)
a * c != d * b
NOT ((5 mod 2) x 4)
5x4 6x2
0
false
20 12
true
!(c% b*a)
NOT ((4 mod 2) x 4)
5
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.
a % b * c && c % b * a
((5 % 2) * 4) && ((4 % 2) * 5)
((1) * 4) && ((0) * 5)
(4) && (0)
0

false

()
++, -++, -!
+, *, /, %
+, <=, >=, >, <
==, !=
&&
||
+=, -+, *=, /=, %=
=

Parentheses
Postincrement
Preincrement
Logical NOT
Positive, negative
Multiplication, division
Addition, subtraction
Relational operator
Relational operator
Logical AND
Logical OR
Compound assignment
Assignment

L to R
L to R
R to L
L to R
L to R
L to R
L to R
L to R
L to R
L to R
L to R
R to L
R to L

1
2
3
3
3
4
5
6
7
8
9
10
10

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)
((1) * 4) || ((0) * 5)
(4) || (0)
1

true

()
++, -++, -!
+, *, /, %
+, <=, >=, >, <
==, !=
&&
||
+=, -+, *=, /=, %=
=

Parentheses
Postincrement
Preincrement
Logical NOT
Positive, negative
Multiplication, division
Addition, subtraction
Relational operator
Relational operator
Logical AND
Logical OR
Compound assignment
Assignment

L to R
L to R
R to L
L to R
L to R
L to R
L to R
L to R
L to R
L to R
L to R
R to L
R to L

1
2
3
3
3
4
5
6
7
8
9
10
10

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)
((0) * 5) && ((1) * 2)
(0) && (2)
0

false

()
++, -++, -!
+, *, /, %
+, <=, >=, >, <
==, !=
&&
||
+=, -+, *=, /=, %=
=

Parentheses
Postincrement
Preincrement
Logical NOT
Positive, negative
Multiplication, division
Addition, subtraction
Relational operator
Relational operator
Logical AND
Logical OR
Compound assignment
Assignment

L to R
L to R
R to L
L to R
L to R
L to R
L to R
L to R
L to R
L to R
L to R
R to L
R to L

1
2
3
3
3
4
5
6
7
8
9
10
10

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)
((0) * 5) || ((1) * 2)
(0) || (2)
1

true

()
++, -++, -!
+, *, /, %
+, <=, >=, >, <
==, !=
&&
||
+=, -+, *=, /=, %=
=

Parentheses
Postincrement
Preincrement
Logical NOT
Positive, negative
Multiplication, division
Addition, subtraction
Relational operator
Relational operator
Logical AND
Logical OR
Compound assignment
Assignment

L to R
L to R
R to L
L to R
L to R
L to R
L to R
L to R
L to R
L to R
L to R
R to L
R to L

1
2
3
3
3
4
5
6
7
8
9
10
10

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
An automobiles speed is greater than
50 mph and it has been moving for at
least 5 hours.

(len > 2 && len < 3)

(speed > 50 && time >= 5)

Rewrite the following if-else chain by using a switch statement


if (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 statement


switch (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 code


2- Switch case on the code value
3- According to the value display the manufacturer.

#include <iostream> // include the input output stream library


using namespace std; // go to the definition of the std section
void main()
// define the main function
{
int code = 0;
// declare the variable code
cout << "Enter the disk code : "; // prompt the user to give value
cin >> code;
// get the value from the keyboard
switch (code)
// decision based on the value of the variable
{
case 1:
cout << "3M Corporation." << endl;
break;
case 2:
cout << "Maxell Corporation." << endl;
break;
case 3:
cout << "Sony Corporation." << endl;
break;
case 4:
cout << "Verbatim Corporation." << endl;
break;
default:
cout << "Invalid Code!" << endl;
}
}
14

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 1


2- Start by i = 1 to 50
3- Calculate the factorial of i
4- Add each factorial inverse to the Eulers number
5- Print the value.

//Eurler's Number Approximation


#include <iostream>
// include the input output stream library
using namespace std; // go to the definition of the std section
void main()
// define the main function
{
const int MAX = 50; // required limit
double euler_number = 1.0; //euler's number
double fact_i = 1.0; // factorial of i
int j;
for (int i = 1; i <= MAX; i++)
{
j = i; // to calculate the factorial, i has to keep its value
fact_i = 1.0; // initialize the factorial
while (j > 1) // factorial loop
fact_i *= j--;
euler_number += 1.0/fact_i; // Euler's Formula
}
cout << "Euler's Number = " << euler_number << endl;
}

16

You might also like