Programming and Data Structure Tutorial sheet: 1 Topics: Data Handling, Operators and Expressions, Input and Output Operations.

Similar documents
Name Roll No. Section

Computer Programming. Decision Making (2) Loops

Computer System and programming in C

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

The Hyderabad Public School, Begumpet, Hyderabad, A.P

only in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages.

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

Subject: Fundamental of Computer Programming 2068

16.216: ECE Application Programming Fall 2011

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Operators and Expressions:

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Data types, variables, constants

Advanced features of the Calculate signal tool

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

CS356: Discussion #3 Floating-Point Operations. Marco Paolieri

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CS 0449 Sample Midterm

'C' Programming Language

Programming and Data Structures

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Make sure the version number is marked on your scantron sheet. This is Version 1

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

COMP1917 Computing 1 Written Exam Sample Questions

Functions. Arash Rafiey. September 26, 2017

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

The Math Class. Using various math class methods. Formatting the values.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Question Bank (SPA SEM II)

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Q. 1 What will be the output of the following program? Justify your answer. [4] #include <stdio.h> main(){ int i=4, a[5]={1,2,3,4,5};

Programming Language A

CS1100 Introduction to Programming

CS16 Exam #1 7/17/ Minutes 100 Points total

COMP2611: Computer Organization. Data Representation

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

Basic Assignment and Arithmetic Operators

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Computer Programming for Engineering Applica4ons. Intro to Programming 10/2/13 ECE 175. Limita4ons of Reference by Value. The Concept of Pointers

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

Scientific Computing 1 Tutorial 1a 10/18/2012 Solution

Principles of C and Memory Management

Government Polytechnic Muzaffarpur.

WARM UP LESSONS BARE BASICS

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Work relative to other classes

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Essar Placement Paper

Introduction: The Unix shell and C programming

Dept. of CSE, IIT KGP

Practice Sheet #07 with Solutions

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

provided. Use any other space for rough work. The question paper has total 8 pages. Rough Work

EECE.2160: ECE Application Programming Fall 2017

Structured programming

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES EVEN SEMESTER FEB 2017

Fundamentals of Programming Session 20

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

C: How to Program. Week /Mar/05

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

printf( Please enter another number: ); scanf( %d, &num2);

Prepared by: Shraddha Modi

UNIT-I Input/ Output functions and other library functions

H192 Midterm 1 Review. Tom Zajdel

COMP 2001/2401 Test #1 [out of 80 marks]

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Tutorial 1: C-Language

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Basics of Programming

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

COMP 208 Computers in Engineering

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

Number Systems, Scalar Types, and Input and Output

Single Dimension Arrays

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

Chapter 7. Basic Types

Chapter 3. Fundamental Data Types

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Structured programming. Exercises 3

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Decision Making and Branching

BİL200 TUTORIAL-EXERCISES Objective:

Transcription:

Programming and Data Structure Tutorial sheet: 1 Topics: Data Handling, Operators and Expressions, Input and Output Operations. Q1. (a) Which one of the following is a valid name of a C variable: 2ab_c, Switch, xy#1, "rst"? ns: Switch. (b) What value does the following code print? #define N a*b int a = 5, b = 10, c = 15; printf("%d",c/n); ns: 30 (c) What will be the contents of the variables a and b after the execution of the following code on the input I do not know? char a, b; scanf("%c do not %c", &a, &b); ns: The characters I and k, respectively. (d) If the number of bits in the memory address of a computer is 16, what is the maximum number of addressable memory locations? ns: 2 16 = 65536. (e) What values does the following code print? int m = 7; int n = 9; m += n; n = m - n; m = m - n; printf ("%d, %d\n", m, n); ns: 9,7. (f) What values does the following code print? int m,n; m = n = 4; m *= 3/2; n = n * 3/2; printf("%d %d", m, n); ns: 4 6 (g)how many times is the statement i *= i+1; in the following for loop executed? for (i=1; i<100; ++i) i *= i+1; ns: Three times (h) How many times is the loop condition i<100 checked in the loop of Part (g)? ns: Four times (i) What is the value stored in the variable i immediately after the loop of Part (g) terminates? ns: 183 (j) What values does the following code print? int a = 7; int b = 4; int c = -2; a = a - a % b * c; printf ("%d\n", a); ns: 13 (k) Let the variables in the above code be defined as: int a = 5; int b = -3; int c = 0; Which of the following conditions evaluates to true in the C programming language? i. ( a < b ) ( c < b ) ii. ( c < a ) && ( c < b ) iii. ( a > b ) &&!c iv.!( a + b > c ) ns: iii

(l) Which of the following conditions is equivalent to the condition:!(( x >= y ) && ( y >= z ))? i.!( x >= z) ii. x <= z iii. ( x < y ) && ( y < z ) iv. ( x < y) ( y < z ) ns: iv (m) Fill in the blanks (for appropriate actions mentioned in brackets): # include <stdio.h> /* Include a header file for I/O operation. */ float x,y; int z; printf( Read x and y \n ); scanf( %f %f,&x,&y); /* Read x and y in appropriate format. */ z= (int) (x+y); /* Type-cast. (1) */ if (z = = 0) /* Check equality with zero. */ printf( x %f, y= %f \n,x, y); /* Print values of x, and y. */ (n) What values does the following code print? #define SNUM 10+10 SNUM * SNUM; printf ( "pnum = %d\n", pnum ); ns: 120 (o) What values does the following code print? int k; int num = 30; k = num > 5? (num <= 10? 100:200):500; printf ( "k = %d\n", k ); ns: 200 (p) What does the following program print? #include <stdio.h> main () int i = 100, m, n; m = 1 + (i++); n = 1 + (++i); printf("m = %d, n = %d\n",m,n); ns : m = 101, n = 103 (q) The RM size of a computer is 64Mbytes. How many char type data can it store? ns: 1 Character occupies 1 byte. Number of character data that can be stored = 64 Mbytes/1byte 64X2 20 or, 64*1024*1024 or, 68,719,476,736 Q 2. For questions 1.1 to 1.5 assume that variables a and b have data type int and variable c and d have data type float. lso, a =9, b = 8, c = 16.0, and d = 6.0. For each question write the value assigned to the variable z. Data type of z is float. 1.1. z = a + c / 4 * d / 3 + b; ns : 25 1.2. z = c + a / 4 * b / 3 + d ; ns :27 1.3. z = (int) c / a * b / 3 ; ns :2 1.4. z = a / b * b % 5 % 3 ; ns :16 1.5. z = ( a >= c )? a : c ; ns :0

Q3: (a) Consider the following snippet of a c-program for which int and unsigned data-types are represented by 8-bits each. Write the minimum and maximum possible values which can be correctly stored in the variables a and b. Show details of your steps. int a; unsigned b;.... ns: int a: i) largest 2 7-1 ii) minimum: - 2 7 unsigned a: iii) largest 2 8-1 iv) minimum: 0 (b) Write a program which will take your iit kgp roll no as input and print your roll no, department and year of admission as output. You must supply your complete IITKGP roll number to the program (like 09FB1331). #include <stdio.h> char roll[10]; printf("enter your roll number : "); scanf("%s",roll); printf("roll number : %s\n", roll); printf("department : %c%c\n", roll[2], roll[3]); printf("year : %d\n", 2010 + (int)(roll[1]- 0 )); The last three lines in the output of the above program are: Roll number: 11CS1331 Department: CS Year: 2011. Q4: Convert the following decimal numbers into 2 s complement 8-bit integer representations. -37, 79. Soln: (a) -37: 37->37/2 =18 ->1 18/2=9 ->0 9/2=4 ->1 4/2=2 ->0 2/2=1 ->0 1 / 2 =0 ->1 Hence: 37 -> 00100101 (in 8 bit) -37= 2 s complement of 37= 1 s complement of 37 + 1= 11011010 + 1 11011011 (ns.) (b) 79 ->79/2=39 ->1 39/2=19 ->1 19/2= 9 ->1 9/2=4 ->1 4/2= 2 ->0 2/2=1 ->0 1 / 2=0 ->1 Hence: 79->01001111 (ns.) Q5:. Consider the following C-code which intends to compute simple interest on an amount of Rs. 2000/- for a period of four years at simple interest rate of 10.5% per annum. # include <stdio.h> int amount = 2000; int time = 4 ; float rate = 10.5;

float simint; simint = time/100 * amount * rate; printf( Interest on Rs %d at %f per annum for %d y = %d\n, amount, rate, time, simint); return 0; What will be the output given generated by the above code? Write the reason(s) in one sentence. ns: Interest on Rs. 2000 at 10.5 per annum for 4 y = 0 Reason: Integer division of time/100 results in 0. Modify the above code so that you get the correct output. Explain your answer in one sentence. ns: simint = amount * rate * time/100; or simint = ((float)time/100) * amount * rate; or simint = (time/100.0) * amount * rate; Reason: In either of the solutions floating point division occurs. Q6: What will be the output generated by the following code? char x = ''; static char name[20] = "NIL KUMR GUPT"; printf("output OF CHRCTERS\n\n"); printf("%c\n%3c\n%5c\n", x,x,x); printf("%3c\n%c\n", x,x); printf("\n"); printf("output OF STRINGS\n\n"); printf("%s\n", name); printf("%20s\n", name); printf("%20.10s\n", name); printf("%.5s\n", name); printf("%-20.10s\n", name); printf("%5s\n", name); ns : Output OUTPUT OF CHRCTERS OUTPUT OF STRINGS NIL KUMR GUPT NIL KUMR GUPT NIL KUMR NIL NIL KUMR NIL KUMR GUPT

Q7: Write a program that determines whether a given integer is odd or even and displays the no. and description on the same line. Soln: #include<stdio.h> int num; printf ("Enter a number to be checked for even/odd: "); scanf ("%d",&num); if (num%2==0) printf ("The entered number is EVEN.\n"); else printf ("The entered number is ODD.\n"); Q8: In this exercise, we compute the binomial coefficient n C r by repeatedly using the formula n C r = ( n/r) n-1 C r-1. We compute n/r as a floating-point value. Finally, the accumulated product is rounded to the nearest integer. In both the following parts, you are not allowed to use any math library function. Fill in the blanks to complete the following C function that takes a floating-point value x as its only argument and returns the rounded value of x. The rounded value of x is the integer nearest to x. When x is mid-way between two consecutive integers, we follow the convention round half away from zero, that is, round (2.5) = 3 and round ( 2.5) = 3. int roundit ( double x ) int r; /* The rounded integer to return */ double fpart; /* Fractional part */ /* Store in r the truncated value of x */ r = (x >= 0)? (int)x : (int)(-x) ; /* Store in fpart the fractional part of x */ fpart = (x >= 0)? x - (double)r : -(x + (double)r); /* Modify r based conditionally upon fpart */ if (fpart >= 0.5) ++r ; /* Return r after sign adjustment */ return (x >= 0)? r: -r ;