In: Computer Science
Every C++ program starts with this function:____________________
2. If A is > 100 I want cnt to be equal to 32 but if A 100 or less I want cnt to be 2. Write a single line using a conditional operator to do this.
3. I want to store into B the remainder of dividing B by the total of A plus D. Write the statement:
as a combined operator statement
not using a combined operator
4. I want to print the following, including quotes, apostrophes and slashes
"My system's programs are in c:\prog"
Write the code below.
5. I have the following bit patterns stored in 2 variables, A and B. In the answer part of each column write the results of the following bitwise operations.
A 1 1 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1
B 1 0 1 0 1 0 1 1 1 0 0 1 0 1 0 1 1 1 1 0 0 0 1
operation ^ & |
answer
6. Write an if ... else to do the following, all having to do with the variables A, B, C, D.
Your answer should not have independent if statements but should be one if...else...if...else.........
If the variable A is between 1 and 17, but not 10, print “hello” . 1 and 17 are included
If the variable A is greater than 72 and also less than 100 print “Goodbye” or if
the variable D is 12 print "Goodbye"
If the variable A is between 200 and 300 while C is not 12 print “waiting”
7. Write the if statement which will satisfy the following conditions about the variables A, B, and C.
a. If A is not equal to 23 at the same time that either B or D is equal to 11 then
print the value of A otherwise print the value of B.
b. If A is 23 at the same time that B is 3 then cout the value of C otherwise print
“Bye”.
8. What is the value of the variable Number after each statement. Each answer is dependent on the previous answer.
int Number=0; ___________________
Number = 100; __________________
Number /= 3 + 2; ________________
Number--; ________________
9. Based on the last answer above, what will this print: cout << (Number++ + 2);
1. Main()
Reason: Main() function is the entry point of all c++
programs
2. cnt = A > 100 ? 32 : 2;
Reason: syntax of conditional operator: condition?statement for
true condition:statement for false condition
3. B %= (A + D);
Reason a % = b is equivalent to a = a % b
4.
#include<iostream>
using namespace std;
int main()
{
cout<<"\"My system's programs are in
c:\\prog\"";
return 0;
}
Reason: '\' is called as escape character. compiles prints " if it
is after a '\'.
5.
^: 0 1 1 0 0 1 1 0 0 1 1 0 0 0 1 0 1 0 0 1 0 1 0
&: 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 0 1 1 0 0 0 1
|: 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1
Reason: a ^ b is 1 if a and b are different and 0 otherwise
a & b is 1 if a and b both are 1 and 0 otherwise
a | b is 1 if either a or b is 1 and o if both are 0
6.
if(A >= 1 && A <= 17) cout<<"hello";
else if((A > 72 && A < 100) || (D == 12))
cout<<"Goodbye";
if(A >= 200 && A <= 300 && C != 12)
cout<<"waiting";
7.
a.
if(A != 23 && (B == 11 || D == 11)) cout<<A;
else cout<<B;
if(A != 23 && B == 3) cout<<C;
else cout<<"Bye";
8.
int Number=0;//Number=0
Number=100;//Number=100
Number /= 3 + 2;//Number = Number/3 + 2 = 100/3 +2 = 33 + 2
=35
Number--;//Number=Number-1=33-1=32
9.
34
Reason:Number++ + 2 = Number + 1 + 2 = 32 + 2=34
In post increment, a number is updated in the next statement, that
is why Number wasn't incremented here.'