WeekEndAssignment-1 - Computer Programming
WeekEndAssignment-1 - Computer Programming
Problem Statement:
Experiment with C operators, role of operator precedence, associativity and expressions.
Assignment Objectives:
To become familiar with C operators, expression evaluation as per operator precedence and associativity
rule.
2. Which of the following identifiers are (a) C reserved words, (b) standard identifiers, (c) conventionally
used as constant macro names, (d) other valid identifiers, and (e) invalid identifiers?
void MAX_ENTRIES return printf "char"
xyz123 time part#2 G Sue’s
#insert this_is_a_long_one double __hello_
Answer here t
1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
3. The Pythagorean theorem states that the sum of the squares of the sides of a right triangle is equal to
the square of the hypotenuse. For example, if two sides of a right triangle have lengths of 3 and 4, then
the hypotenuse must have a length of 5. Together the integers 3, 4, and 5 form a Pythagorean triple.
There are an infinite number of such triples. Given two positive integers, m and n, where m > n, a
Pythagorean triple can be generated by the following formulas:
side1 = m2 − n2
side2 = 2mn
hypotenuse = m2 + n2
The triple ( side1 = 3, side2 = 4, hypotenuse = 5) is generated by this formula when m = 2
and n = 1. Write a program that takes values for m and n as input and displays the values of the
Pythagorean triple generated by the formulas above. The values of m and n should be provided from
an input file through input redirection.
Write/paste your code here t Output t
2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
5. Consider the C arithmetic expression 2 ∗ ((i%5) ∗ (4 + (j − 3)/(k + 2))) where i , j and k are integer
variables. If these variables are assigned the values 8, 15 and 4, respectively, then the given determine
the value of the expression. (Note: The interpretation of the remainder operation (%) is unclear
when one of the operands is negative. Most versions of C assign the sign of the first operand to the
remainder. The % operation is undefined when second operand is zero.)
Expression evaluation t
7. ASCII code for the character ? is 63. Characters are represented by integer codes, C permits con-
version of type char to type int and vice versa. So find the output for the given code snippet;
Output
int q_code = (int)’?’;
printf("%d %c %d\n", q_code,’?’,’?’);
8. The following expressions contain different operands and operators assuming x=3.0 , y=4.0 , and
z=2.0 are type double , flag=0 is type int. Write each expressions value.
Answer
9. What value is assigned to the type int variable ans in this statement if the value of p is 100 and q
is 50?
Output
3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
10. Evaluate each of the following expressions if a is 6 , b is 9 , c is 14 , and flag is 1 . Which parts of
these expressions are not evaluated due to short-circuit evaluation?
Answer
(a)
(a) c == a + b || !flag
(b)
(b) a != 7 && flag || c >= 6
(c)
(c) !(b <= 12) && a % 2 == 0
(d)
(d) !(a > 5 || c < a + b)
12. Another way to generate the same information as like previous question is to use a cast rather than a
variable within each printf statement. Find the output generated by these statements that make use of
the operator sizeof.
printf("integer:%ld bytes\n", sizeof(int));
printf("float:%ld bytes\n", sizeof(float));
Answer
printf("double:%ld bytes\n", sizeof(double));
printf("character:%ld bytes\n", sizeof(char));
/* Same way can be used for other data types to
find the size */
13. C supports several assignment operators. The most commonly used assignment operator is =. Assign-
ment expressions that make use of this operator are written in the form identifier = expression,
where identifier generally represents a variable, and expression represents a constant, a vari-
able or a more complex expression. Determine the expression values assume that i is an integer-type
variable, and that the ASCII character set applies.
Answer
i=(’x’-’o’)/3;
i=(’y’-’o’)/3;
i=2*j/2; (say j is an integer and j is 5)
i=2*(j/2);
i=3.0;
i=-3.5;
14. C also contains other form assignment operators: +=, -+, *=, /=, %= etc., called short hand operators.
Suppose that i and j are integer variables whose values are 5 and 7, and f and g are floating-point
variables whose values are 5.5 and -3.25. Determine the value of the expressions
Answer
i += 5;
f -= g;
j *= ( i - 3);
f /= 3;
i %= ( j - 2 )
15. Suppose that x, y and z are integer variables which have been assigned the values 2, 3 and 4, respec-
tively. Determine the value of the given expression;
Answer
x*=-2*(y+z)/3;
16. The assignment statement that contains a conditional expression on the right-hand side. Determine
the value of flag if i=-5 and i=-6 respectively.
Answer
flag = ( i < 0 ) ? 0 : 100
17. In the following assignment statement, a, b and c are assumed to be integer variables. If a, b and
c have the values 1, 2 and 3, respectively, then determine the value of the expression that includes
operators of different precedence groups.
Answer
c += (a > 0 && a <= 10) ? ++a : a / b ;
18. Illustrate the purpose of the following code snippet over the inputs a,b and c respectively.
int m1,m2,a,b,c; Answer
printf("Enter the values of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
m1=(a>b)?a:b;
m2=(m1>c)?m1:c;
printf("%d\n",m2);
Determine the value of each of the following expressions. Use the values initially assigned to the
variables for each expression.
5
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University
(a) ( 3 * i - 2 *j ) % ( 2 * d - c ) Answer
(b) ( x > y ) && (i > 0) && ( j < 5 )
(c) 2 * x + ( y = = 0)
(d) ( 2 * x + y ) == 0
(e) 5 * (i + j ) > ’ c ’
(f) i++
20. Suppose a is an unsigned integer variable (say represented in 16 bits format) whose value is 0x6db7.
In the following the expression, we will shift all bits of a six places to the right and assign the resulting
bit pattern to the unsigned integer variable b. Find the resulting value of b. Also write the lost bits
because of shifting.
Answer
b = a >> 6 ;
21. Determine the value of each of the following expressions, assume that a is an unsigned integer variable
whose initial value is 0x6db7.
Answer
(a) a &= 0x7f
(b) a ˆ= 0x7f
(c) a |= 0x7f
(d) a = a & 0x3f06
(e) a = a | 0x3f06 << 8
6
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University