0% found this document useful (0 votes)
4 views7 pages

WeekEndAssignment-1 - Computer Programming

The document outlines a weekend assignment for a workshop on Operating Systems, focusing on C programming. It includes various programming tasks related to C operators, expressions, and evaluation, along with specific problems to solve. Students are instructed to write their own code and avoid copying from other sources.

Uploaded by

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

WeekEndAssignment-1 - Computer Programming

The document outlines a weekend assignment for a workshop on Operating Systems, focusing on C programming. It includes various programming tasks related to C operators, expressions, and evaluation, along with specific problems to solve. Students are instructed to write their own code and avoid copying from other sources.

Uploaded by

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

Department of Computer Science and Engineering

Institute of Technical Education & Research, SOA, Deemed to be University


W EEK -E ND A SSIGNMENT-01
Operating Systems Workshop (CSE 3541)

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.

Instruction to Students (If any):


Students are required to write his/her own program by avoiding any kind of copy from any sources.
Additionally, They must be able to realise the coutcome of that question in relevant to systems pro-
gramming.

Programming/ Output Based Questions:


1. Evaluate the arithmetic expression a − b/c ∗ d, where the floating-point variables a , b , c and d
have been assigned the values 1.0, 2.0, 3.0 and 4.0. Create a C program to display the value of the
expression on standard output device.
Write/paste your code here t Output t

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

4. Write C statements to carry out the following steps.


(a) If item is nonzero, then multiply product by item and save the result in product ; other-
wise, skip the multiplication. In either case, print the value of product.
(b) Store the absolute difference of x and y in y , where the absolute difference is ( x - y ) or
(y - x ), whichever is positive. Do not use the abs or fabs function in your solution.
(c) If x is 0 , add 1 to zero count. If x is negative, add x to minus sum. If x is greater than 0 ,
add x to plus sum.

Write/paste your code here 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

6. Consider the following C expressions;


Answer Cause
(a) Suppose that i is an integer variable whose value is 7,
and f is a floating-point variable whose value is 8.5. The (a)
expression (i + f )%4 is valid or invalid.

(b) Suppose that i is an integer variable whose value is 7, (b)


and f is a floating-point variable whose value is 8.5. The
expression ((int)(i + f ))%4 is valid or invalid.

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

(a) !f lag (a)

(b) x + y/z <= 3.5 (b)

(c) !f lag||(y + z >= x − z) (c)

(d) !(f lag||(y + z >= x − z)) (d)

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

ans = (p > 95) + (q < 95);

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)

11. Suppose that i is an integer variable, x is a floating-point variable, d is a double-precision variable


and c is a character-type variable. Find the output generated by these statements that make use of the
operator sizeof.
printf("integer:%ld bytes\n", sizeof i);
printf("integer:%ld bytes\n", sizeof(i)); Answer
printf("float:%ld bytes\n", sizeof x);
printf("float:%ld bytes\n", sizeof(x));
printf("double:%ld bytes\n", sizeof d);
printf("double:%ld bytes\n", sizeof(d));
printf("character:%ld bytes\n", sizeof c);
printf("character:%ld bytes\n", sizeof(c));
/* Same way can be used for other data types to
find the size */

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;

NOTE: Multiple assignments of the form


identifier 1 = identifier 2 = ... = expression
are permissible in C. In such situations, the assignments are carried out from right to left.
4
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

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);

19. A C program contains the following declarations and initial assignments:


int i= 8;
int j = 5;
float x = 0.005;
float y = -0.01;
char c = ’c’ , d = ’d ’ ;

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

22. Determine the output of the following code snippet.


int main(){
int m1,a,b,c; Answer
printf("Enter the values of a,b,c:");
(1)a=10 b=20 30 m1=
scanf("%d%d%d",&a,&b,&c);
m1=a>b?a>c?a:c:b;
(2)a=30 b=10 c=20 m1=
printf("m1=%d\n",m1);
return 0;
(3)a=20 b=30 c=10 m1=
}

23. Evaluate the expressions;


Answer
(1) A =
Assume A, B, num, xy, f, t, p, q, r
are int type variables; (2) B =
(1) A=10+(num=2)*3;
(2) B +=(xy *=3); [here xy=10] (3) x =
(3) x +=(f=(t*=20)); [here x=20, t=10]
(4) p=q=r=100; (4) p= q= r=

6
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

24. State the output of the following code snippet;


Answer
int a,b,s;
s=scanf("%d%d%d",&a,&b,&a);
printf("%d\n",s+printf("OSW CSE="));

25. State the output of the following code snippet;


Answer
int i=-1,j=-1,k=0,l=2,m;
m=++i || k++ && ++j || l++;
printf("%d %d %d %d %d\n", i,j,k,l,m);

26. State the output of the following code snippet;


Answer
int i=10,j=6;
printf("%d\n", i+++j++);

27. State the output of the following code snippet;


int i=3>4, j=4>3; Answer
int k=(i=j);
int l=(k==j);
printf("%d %d %d %d",i,j,k,l);

28. State the output of the following code snippet;


Answer
int x=400;
printf("%d %d\n",x=40,x>=50);

29. verify the output/ error of the following code snippet;


int i=2,j=0; Answer
int k=i&&j=1;
printf("%d\n",k);

30. Find the output of the following code snippet;


int i=2,j=2; Answer
int k=iˆj&i;
printf("%d\n",k);

31. Find the output of the following code snippet;


int i=3,j=2; Answer
int k=i << 1 > 5;
printf("%d\n",k);

You might also like