Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

Similar documents
Eastern Mediterranean University Department of Computer Engineering

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

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

Lecture 3. More About C

C: How to Program. Week /Mar/05

ANSI C Programming Simple Programs

Chapter 1 & 2 Introduction to C Language

Unit 4. Input/Output Functions

Programming and Data Structures

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

BASIC ELEMENTS OF A COMPUTER PROGRAM

DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3. Introduction to C Programming: Sequential code structure

Chapter 2 - Introduction to C Programming

Computer Programming. Decision Making (2) Loops

Data Types and Variables in C language

C - Basic Introduction

Question Bank (SPA SEM II)

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Fundamentals of C. Structure of a C Program

Worksheet 4 Basic Input functions and Mathematical Operators

Java Notes. 10th ICSE. Saravanan Ganesh

Multiple Choice Questions ( 1 mark)

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

6.096 Introduction to C++ January (IAP) 2009

Number Systems, Scalar Types, and Input and Output

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

WARM UP LESSONS BARE BASICS

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

BSM540 Basics of C Language

Lesson 3 Introduction to Programming in C

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

C Programming

2. Numbers In, Numbers Out

Data Type Fall 2014 Jinkyu Jeong

Operators and Expressions:

SAMPLE MIDTERM SOLUTION

Introduction to C Language

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Pointers and scanf() Steven R. Bagley

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Basic Elements of C. Staff Incharge: S.Sasirekha

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

Fundamentals of Programming

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

BLM2031 Structured Programming. Zeyneb KURT

Fundamental of Programming (C)

M1-R4: Programing and Problem Solving using C (JAN 2019)

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

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

Basics of Programming

Lecture 02 C FUNDAMENTALS

Question 2. [2 points] True False By default, structures are passed-by-reference.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

Fundamental of C programming. - Ompal Singh

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

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

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

UNIT- 3 Introduction to C++

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

2. Numbers In, Numbers Out

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

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

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

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

CCE RR REVISED & UN-REVISED KARNATAKA SECONDARY EDUCATION EXAMINATION BOARD, MALLESWARAM, BANGALORE G È.G È.G È..

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

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

Chapter 2 THE STRUCTURE OF C LANGUAGE

EXAMINATION FOR THE BSC (HONS) INFORMATION SYSTEMS; BSC (HONS) INFORMATION TECHNOLOGY & BSC (HONS) COMPUTER SCIENCE; YEAR 1

Subject: Fundamental of Computer Programming 2068

CC112 Structured Programming

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

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

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

Input/Output Week 5:Lesson 16.1

EC 413 Computer Organization

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

What we have learned so far

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

JAVA Programming Fundamentals

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

THE FUNDAMENTAL DATA TYPES

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

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

Computer Programming 3 th Week Variables, constant, and expressions

CSCI 171 Chapter Outlines

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Learning to Program with Haiku

Transcription:

CMPE108, Homework-2 (C Fundamentals, Expressions, and Selection Structure.) Student Nr:... Name,Surname:...:... CMPE108 Group:... Signature... Please print this homework, and solve all questions on the printout using a pen or pencil. Do not forget to write your student number on every page. Late submissions, printout solutions, and photocopies will not be graded. The following information will be available on midterm exam sheets. Precedence and Associativity Table Operators Associativity ( ) [ ] ->. Left to right! ++ -- + - * & (type) Right to left (Unary) * / % Left to right + - Left to right < <= > >= Left to right ==!= Left to right && Left to right Left to right?: Right to left (ternary) = += -= *= /= %= Right to left, Left to right % specifiers in ANSI C: %c char single character %d (%i) int signed integer %e (%E) float or double exponential format %f float or double signed decimal %g (%G) float or double use %f or %e as required String Control Codes: %o int unsigned octal value %p pointer address stored in pointer %s array of char sequence of characters %u int unsigned decimal %x (%X) int unsigned hex value \a alert, \b backspace, \f formfeed, \n new line, \r carriage return, \t horizontal tab, \' single quote, \0 null. \\ backslash. Q1. a) From the following items encircle the C keywords. (Each false mark cancels one correct mark!) if, while, int, include, stdio, for, Pause, double, break, stop, end b) State which of the following C identifier names are valid or invalid. For the invalid names, propose a similar but valid name. i) Cmpe_108 ii) dollar$ iii) printf_scanf iv) 747_Boeing v) football club vi) Name vii) Name Surname viii) 20number ix) number# x) double xi) _age Q2. a) For the following statements, give the corresponding outputs into the boxes on the right which correspond to different spaces in the output. i) printf( k=%3d i=%4d, 5, 12); ii) printf( %4.3f, 1410.22); iii) printf( x=%8.1f% -3dm, 0.55, 55);

CMPE108, Homework-2 (C Fundamentals, Expressions, Selection.) Student. Nr:... b) Write the characters (each character in one box) printed by the following code section. int j=17, k=7, p=1; float m=33.66, n=3.0; printf( abc%3de%.1fg\nh%d, j%k, m/n, p++); c) What is the value of i, j, k, f, g after entering the following line for the scanf sentence: int i,j,k; float f, g; scanf( %d%d%d%f%f,&i,&k,&j,&g,&f); 1 1 2 3 3 3 4. 4 5. 5 i=... j=... k=... f=... g=... Q3 a) Find the value of the following expressions: Expression Result i) 20.0 2 / 6 + 3... ii) 10 + 17 % 3 + 4.0... iii) (10 + 3 ) % 4... iv) 3 % 2 / 5... v) 2 * 1-2.0 / 4... b) Given the following declarations: int i=5, j=7, k=12, z=0, a=5, b=2, c=4, d=6; char ch2= b, ch3= B ; Evaluate each of the following expressions and write the answer in the box provided i) i + 2 && k -1 ii) iii) iv) i + 2 * j == k k / 3!= - j + 3 * - i ch2!= ch3 = = z!== a v) d % b == c % b ch2 < ch3 vi) a - b * c c / b && a c) Evaluate the following expressions for the declared values of the variables, and fill its final type (integer or float) and numerical value into answering box. i) int i=2, n= 15; float x=2.4; x*2 + n/i % 3 type: value: ii) int m=5; float y=4.6; m = y + m / type: value: 2

iii) int m=5; 2.0f * m / 2 type: value: iv) int x=5; float i=4.5; i + ++ x - -- x type: value: v) int n=-5; n += 1 / 2+3 *4% 5-6 x type: value: d) Write the C expression of each real numbered mathematical expressions, without using any unnecessary parenthesis. Do not forget to add a decimal point to the integers to convert them to real numbers. Q i) 1+ Q ii) R+1 5 4 +2i 3j 4 5 k iii) x x y +2 y 3z 2 iv) 2z 2x x+y v) x y x2 + y 2 1.0 a vi) c 1 c 1 ac vii) viii) ix) x) 1/ b c c b/ c 1 (a+b) (c d) (e /f) g3... 1 2 x2 + xy x+ 1 x... 2 3 (x2 ) 1 4 +x e) Write down a mathematical expression to each of the following expressions without parenthesis. i) sqrt((b*b) (4*a*c))... ii) 2*n*n*n 4*n*n + 3*n + 1... iii) (a*a b*b)/(a - b) + 1/a + 1/b... f) Considering the precedence of operators put a cross mark into the box of unnecessary pair-ofparenthesis. Example: ( a+b ) * ( c/d ) (3/ ( 2*a ) ) [ ] [ ] [x] [x] [x] [ ] [ ] [x] i) ( a*b ) / (c/d)*( ( a-b) - c) [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] ii) ( a*b ) + (c/d)*( a-b ) [ ] [ ] [ ] [ ] [ ] [ ] iii) ( (5./4.) + (2.*i) )/ ( (3.*j) (4./5.)*k ) [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] 3

g) For the following display outputs fill in the blanks of the printf statements to get the output exacly as given below. Note that in codes and output _ represents the space character. i. printf(..., 256) to get the output: x= 256m ii. printf(..., -92.39538) to get the output: h:-92.40 m iii. printf(..., 78.461032) to get the output: _ 78.46m iv. printf(..., 1.2345678) to get the output: 1.2e000m v. printf(...%..., 5) to get the output: t= 005 min vi. printf( %..., 40) to get the output: 40 _m vii. printf( %..., 3.26) to get the output: _ 3.3 viii. printf( %..., 100) to get the output: 0100 ix. printf( %..., 55.33) to get the output: 55.3300 _Nt x. printf( %..., 43) to get the output: _ 43 xi. printf( %..., 7) to get the output: 007 h) Fill in the blanks to read two integers and one floating point numbers (i, j, and a) correctly. int i, j; float a; scanf(...); Q4 The following program calculates workers salary. (work_code and work_hour is entered by user.) Find the output of the following program for three different sets of inputs as given below: a) work_code: 2 b) work_code: 3 c) work_code: 1 work_hour: 25 work_hour: 10 work_hour: 30 Find the output and write your answer in the corresponding answer box. #include <stdio.h> #include <stdlib.h> int main (void) int work_code, work_hour; double salary; printf("code?: "); a) scanf("%d",&work_code); if ((work_code ==2) (work_code ==3)) printf("work hour?: "); scanf("%d",&work_hour); switch(work_code) b) case 1: salary=875; break; case 2: salary=6.80*work_hour; break; case 3: salary=12.5*(work_hour%40)+875; printf( salary: %.2f\n",salary); c) system("pause"); return (0); 4

Q5. a) The following C program contains 12 errors. Find each error and show it by putting a circle around it. (i.e. circle only the exact location of the error, you do not need to correct the errors, you will lose -1 point for each wrongly identified error) #include stdio.h> /* This program finds your age #define YearOfCalendar = 2017 int main (void) int birthyear age printf( Enter Your Birth Year, in four digits ); scanf ( %d, birthyear); printf( We are in the year %f, and, YearofCalendar ); age=yearofcalendar - birthyear; printf ( your age is calculated as %d, &age ); return0; b) The following C program contains some programming mistakes in five statements. Find the lines with programming mistake(s) and write down the corrected statements in the boxes given below: 1. // This program computes the sum of digits of 2. // a 3-digit integer number: 3. // (e.g. If input = 234, output = 9) 4. #include <standardio.h> 5. int main(void) 6. int num, a, b, d, SUM; 7. printf("enter a 3-digit number : "); 8. scanf("%d", num); 9. a = num % 10; 10. num= num/10 11. b = num % 10; 12. num= num/10; 13. d = num; 14. Sum = a+b+d; 15. printf ("The num and SUM are = %d \n", num,sum); 16. return(0); 17. Line number Corrected statement 5

Q6. a) The following C program calculates the travel time of a car running at constant speed for a given distance. Program reads the speed "kmperhr", and the distance "kmeters" from the keyboard as integer numbers, calculates time minutes, find hours hrs, and remaining minutes minr. Then, it writes the time in hours:minutes format as Travel time=h:m. Complete the missing parts. Example: if speed is 100, and distance is 150, then minutes shall be 60*150/100= 90, hrs shall be 1, minr shall be 30, and display shall be Travel time=1:30. #include <stdio.h> int main() int kmperhr, ; int, hrs, minr ; /*read speed and distance from the keyboard*/ ; /*compute minutes */ ; /* divide minutes by 60 to get hrs */ ; /* find remainder by 60 to get minr*/ ; /* Print the result in hr:min format */ ; return 0; b) The following C PROGRAM computes c = a 2 + b 2, the hypotenuse (the side opposite the right angle) of a right triangle, based on the Pythagorean Theorem. There are 10 blanks in the program. Complete each of the blanks. #include <stdio.h> #include <math.h> /* for the function sqrt */ c a b... main(void) float a, b, c; /* declare floating point variables */... sq_a,...; /* declare floating point variables */ printf("insert length of first side \'a\':");...; /* reads 'a' from the input */ printf("insert length of second side \'b\':");...; /* reads 'b' from the input */...; /* computes square of 'a' and stores it in 'sq_a' */...; /* computes square of 'b' and stores it in 'sq_b' */ c = sqrt(sq_a + sq_b); /* sqrt computes the square root of 'sq_a' + 'sq_b'*/ printf("the length of the hypotenuse \'c\' is...",...);...; /* terminates the main function normally */ 6

c) Given the following C program, trace it and write down the output of the printf statement. #include<stdio.h> void main() int num1 = 100, num2=200, num3; num3 = num2/num1 <= ( num1 >= 100 ) + (num2 < 50 ); printf("%d",num3); return 0; output: Q7) a) Assuming that int i, k, n; already declared write the following C code using switch-case statement if(k==5) ++n; else if(k==0) --n; else n=0; b) Assuming that int i, k, n, m; already declared write the following C code using if statements switch (k) case -4: case -3: case -2: case -1: n =m; ++m; break; case 0: n +=m; break; default: m =0; c) Assuming that int i, k, n, m; already declared write C code for the following flowcharts i) -2 k<5 n y n=m*2; m=m+1; ii) -2 k<5 y n n=k*2; m=n+1; d) Write the if-then-else structure of the following code using a switch-case statement by using the expression (g>=95)+(g>=85)+(g>=75)+(g>=50). #include <stdio> int main(void) int n, g; printf( Grade : );scanf( %d,&g); if(g>=95) println( perfect ); else if(g>=85) println( excellent ); else if(g>=75) println( good ); else if(g>=50) println( passed ); else println( failed ); return 0; 7