Download - CS 240 Computer Programming 1
![Page 1: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/1.jpg)
1
CS 240Computer
Programming 1
Variables
![Page 2: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/2.jpg)
2
Question 1:
Write the following algebraic expressions as C+
+ expressions :
b* b – 4*a*c
![Page 3: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/3.jpg)
3
Question 1:
Write the following algebraic expressions as C+
+ expressions :
(y*(x*x-y*y)( / )* +3* *x x x y)
![Page 4: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/4.jpg)
4
Question 2-a: Each of the following
assignment statement contains at least one error. Identify
them : D e l t a = x ( y + ( 3 x +
( z + 1 5 ) ) )
X = 5 = y + 4
![Page 5: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/5.jpg)
5
Question 2-b:Correct the following program
so that the output will be 5.
* include <iostream>using namespace std; \\function main begins program executionint main()[ int a=5;b=5;c=1;d==1; result = a+b/c+d; cout << "The Result is " << result << endl; // print result; end line}
![Page 6: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/6.jpg)
6
Question 2-b:
#include <iostream> using namespace std; //function main begins program execution int main() { int a=5; int b=5; int c=1; int d=1; int result;
result = (a+b)/(c+d); cout << "The Result is " << result << endl; // print result; end line return 0; }
Answer
![Page 7: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/7.jpg)
7
Question 3:For the each of the following expressions, give the type and value of its result. Assume the following declarations have been executed before each expression.
int x = 2; int y = 5; int z = 6 ; int t = 7; int d = 8;
d/= y %z + x;
Ty p e : i n t v a l u e : 1
![Page 8: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/8.jpg)
8
Question 3:For the each of the following expressions, give the type and value of its result. Assume the following declarations have been executed before each expression.
int x = 2; int y = 5; int z = 6 ; int t = 7; int d = 8;;
x % t < z / y;
Ty p e : b o o l v a l u e : f a l s e
![Page 9: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/9.jpg)
9
Question 3:For the each of the following expressions, give the type and value of its result. Assume the following declarations have been executed before each expression.
int x = 2; int y = 5; int z = 6 ; int t = 7; int d = 8;
--t == t-- ;
Ty p e : b o o l v a l u e : t r u e
![Page 10: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/10.jpg)
10
Question 3:For the each of the following expressions, give the type and value of its result. Assume the following declarations have been executed before each expression.
int x = 2; int y = 5; int z = 6 ; int t = 7; int d = 8;
t- = y + z - x/t;
Ty p e : i n t v a l u e : - 4
![Page 11: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/11.jpg)
11
Question 4:Trace the following code to find the value assigned to each variable. Assume that all variables are properly declared as int.
x = 17;y = 15;x = x + y / 4;z = x % 3 + 4;w = 17 / 3 + 6.5;t = x / 4.0 + 15 % 4 - 3.5;
X= 20Y= 15Z= 6
W= 11T=4
![Page 12: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/12.jpg)
12
Question 5:What is the output by each of the following code fragment?
int a=1;
int b=-3;
int c=2;
b=a++;
c+=--a+b++;
cout<<a<<" "<<b<<" " <<c;
The output :
1 2 4
![Page 13: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/13.jpg)
13
Question 5:What is the output by each of the following code fragment?
#include <iostream>using namespace std;int main(){int a=3;int result;result = 2 + 17 % 5 * a++ / 4; cout << "The Result is " << result << endl;return 0;}
The output :
The Result is :3
![Page 14: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/14.jpg)
14
Question 5:What is the output by each of the following code fragment?
#include <iostream>using namespace std;int main(){ int Cost=5,Price=10; Cost*=Price+2; cout << "The Cost is " << Cost<< endl; cout << "The Price is " << Price<< endl; return 0; }
The output :
The Cost is 60The Price is 10
![Page 15: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/15.jpg)
15
Problems
![Page 16: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/16.jpg)
16
Problem 1
Question
Write a program that asks the user to input 2 numbers A and B, then exchange the values of A and B.
![Page 17: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/17.jpg)
17
Problem 1Answer
![Page 18: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/18.jpg)
18
Problem 1Answer
![Page 19: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/19.jpg)
19
Problem 2
Question
Write a program that asks the user to type 5 integers and then prints the average of these 5 integers .
![Page 20: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/20.jpg)
20
Answer
Problem 2
![Page 21: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/21.jpg)
21
Problem 2Answer
![Page 22: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/22.jpg)
22
Problem 3Question
Write a program that reads in the length and the width of a
rectangular yard. Your program should compute the time required (in minutes) to cut
the grass at the rate of 2.3 square meters per a second.
![Page 23: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/23.jpg)
23
Problem 3Answer
![Page 24: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/24.jpg)
24
Problem 3Answer
![Page 25: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/25.jpg)
25
Problem 4Question
Write a program that asks the user to type the price without tax of one kilogram of tomatoes, the number of kilograms you want to buy and the tax in percent units. The program must calculate the
total price including taxes.
![Page 26: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/26.jpg)
26
Problem 4Answer
![Page 27: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/27.jpg)
27
Problem 4Answer
![Page 28: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/28.jpg)
28
Evaluations
![Page 29: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/29.jpg)
29
Question
Write a program that prompts the user to input a length expressed in inches. The program should then convert the length to centimeters.
(One inch equals 2.54 centimeters).
Hint
![Page 30: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/30.jpg)
30
Answer
![Page 31: CS 240 Computer Programming 1](https://reader036.vdocuments.us/reader036/viewer/2022081519/5681323d550346895d98a8f0/html5/thumbnails/31.jpg)
31
Problem 5Answer