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

Similar documents
A Look Back at Arithmetic Operators: the Increment and Decrement

Structured Program Development

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Dept. of CSE, IIT KGP

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Structured Program Development in C

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

BİL200 TUTORIAL-EXERCISES Objective:

Chapter 3 Structured Program Development

Operators & Expressions

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Programming for Engineers Iteration

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Fundamentals of Programming Session 8

CS 108 Computing Fundamentals. October/November Array Bootcamp

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Pointers and Arrays 1

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Introduction to C Programming

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 4: Control structures. Repetition

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

1.3b Type Conversion

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Loops / Repetition Statements

Basic and Practice in Programming Lab7

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Chapter 4: Control structures

Control Structure: Loop

Introduction to Computer Science Midterm 3 Fall, Points

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

BSM540 Basics of C Language

Arithmetic Operators. Portability: Printing Numbers

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 4 C Program Control

Fundamentals of Programming Session 7

SAMPLE MIDTERM SOLUTION

H192 Midterm 1 Review. Tom Zajdel

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

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

ALGORITHM 2-1 Solution for Exercise 4

Introduction. C provides two styles of flow control:

Lecture 3. More About C

بسم اهلل الرمحن الرحيم

1. The keyword main in C language is used for

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Give one example where you might wish to use a three dimensional array

COMP 208 Computers in Engineering

n Group of statements that are executed repeatedly while some condition remains true

Name SECTION: 12:45 2:20. True or False (12 Points)

Full file at C How to Program, 6/e Multiple Choice Test Bank

Introduction to C. Systems Programming Concepts

Lab 2: Structured Program Development in C

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

11.3 Function Prototypes

Computer Programming CS F111

Chapter 4: Basic C Operators

Repetition and Loop Statements Chapter 5

Structured Programming. Dr. Mohamed Khedr Lecture 9

DS: CS Computer Sc & Engg: IIT Kharagpur 1. roblem Set III. Goutam Biswas

Chapter7 Expression and Assignment Statement. Introduction


CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

C: How to Program. Week /Apr/16

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

Computer System and programming in C

Chapter 4. Flow of Control

The C language. Introductory course #1

Lecture 04 FUNCTIONS AND ARRAYS

Flow Chart. The diagrammatic representation shows a solution to a given problem.

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

Operators and Expressions:

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

LAB 5: REPETITION STRUCTURE(LOOP)

C: How to Program. Week /Mar/05

C: How to Program. Week /Apr/23

Chapter 3 Structured Program Development in C Part II

Expression and Operator

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

15213 Recitation Section C

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

PROGRAMMING IN C AND C++:

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

Array. Arrays. Declaring Arrays. Using Arrays

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Chapter 2 - Introduction to C Programming

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

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

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

Introduction to C Final Review Chapters 1-6 & 13

Transcription:

CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be. Q1 (30 marks) In each of the following program segments, find the error(s) in the program. (a) For ( x = 100, x >= 1, x++ ) printf( %d\n, x); (b) The following code should print whether a given integer is odd or even: switch (value %2 ) case 0: printf ( Even integer \n ); case 1: printf( Odd integer ); (c) The following code should input an integer and a character and print them. Assume the user types as input 100 A. scanf( %d, intval ); charval = getchar(); printf( Integer : %d \ncharacter : %c \n, intval, charval); (d) for (x = 0.000001; x =< 0.0001; x+= 0.000001) printf( %.7f\n, x); (e) The following code should output the odd integers from 999 to 1 for (x = 999; x >= 1; x =+ 2) printf( %d\n, x); 1

(f) The following code should output the even integers from 2 to 100 (inclusive): counter = 2; do if (counter % 2 = 0) ; printf( %d\n, counter); counter ++; while (counter < 100); (f) The following code should sum the integers from 100 to 150. Total=0; for (x = 100; x <= 150; x++); total =+ 150; Q2 (24 marks). For each of the following, indicate whether the assertion is TRUE or FALSE. s/n Assertion True or False 1 In C, it is illegal for a function to call itself 2 Static variables are variables whose values cannot change during program execution 3 Variables declared as static within a function are not accessible from outside the function where they are declared 4 In general, C functions are only accessible from within the file where they are defined 5 A function prototype is always necessary before a function can be invoked in a program 6 Any selection structure (if, if/else, and switch) can always be replaced by one or more simple if structures 7 In a repetition structure, the continue statement always forces execution to continue from the next statement after the repetition structure 8 Counter-controlled repetition is typically used for indefinite repetition while sentinel-controlled repetition is typically used for definite repetition 9 In C, all variables must be declared before they are used, however such declarations can appear anywhere within the program 10 The parenthesis operator ( ) has the highest precedence of all C operators 11 In general, the unary forms of the arithmetic operators (+, -, *) always have a higher precedence than the corresponding binary operators 12 Every C program contains the function main() which must always come before other functions in the program text 2

Q3 (20 marks) For each of the following program segments, list the results that will be produced if the program were to be executed. (a) What is the output of the following program? #include <stdio.h> int main() int x = 1, total = 0, y; while (x <= 5) y = x * x; printf( %d \n, y); total += y; ++x; printf( Total is %d\, total); return 0; (b) What is the output of the following program? #include <stdio.h> int main() int row=6, column; while (row >= 1) column =1 ; while (column <= 6) printf( %s, row % 2? < : > ); ++column; --row; printf( \n ); return 0; 3

Q4 (26 marks) (a) A point is usually defined by its x- and y-coordinates. Given two points p 1 = (x 1,x 2 ) and p 2 = (x 2,y 2 ), the distance between them is defined by: d(p 1,p 2 ) = [(x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 ] ½. Assume we have 7 pairs of points and we want to find the distance between each pair. By making use of a function distance(), write a program that reads each of the pair of points from the standard input, and prints out the distance between them. Remember that each pair of points will contain 4 real numbers. You will need to write a function distance(x 1,y 1, x 2,y 2 ) that takes the coordinates of two points as its inputs and then returns the distance between the input positions. The function should accept real numbers as inputs and should also return real numbers as the output. You can assume the existence of two standard functions: pow(x, n) which returns the value of x n, and sqrt(n) which returns the square root of n. Assume also that the input and outputs for the two functions are all real numbers. 4

(b) Given the following declarations, answer the following questions: int a; float b; double c; c=a+b; a=b+c; b=a+c; What will be the data type for the result of the following statement? a*b*c; 5