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

Similar documents
Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

Fundamentals of Programming Session 7

Chapter 3 Structured Program Development in C Part II

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Day21 A. Young W. Lim Wed. Young W. Lim Day21 A Wed 1 / 13

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

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

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

Chapter 3 Structured Program Development

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

GAS Tutorial - 6. Expression

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

Basic Assignment and Arithmetic Operators

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

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

Structured Program Development

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

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

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

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

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

Structured Program Development in C

Programming for Engineers Iteration

1.3b Type Conversion

Pointers (2) Applications

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Arithmetic Operators. Portability: Printing Numbers

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

Computer System and programming in C

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Link 7.A Static Linking

Lecture 3. More About C

Link 7. Dynamic Linking

Fundamentals of Programming. Lecture 6: Structured Development (part one)

GAS Tutorial - 4. Sections & Relocation

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

Overview (1A) Young Won Lim 9/14/17

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

Logic Haskell Exercises

Overview (1A) Young Won Lim 9/9/17

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Overview (1A) Young Won Lim 9/25/17

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

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

CSI33 Data Structures

C Programming for Engineers Structured Program

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

Operators and Expressions:

C: How to Program. Week /Mar/05

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Introduction to C. Systems Programming Concepts

Expressions (2E) Young Won Lim 4/9/18

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Type Conversion. and. Statements

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

The C language. Introductory course #1

HyperGeometric Distribution

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Dept. of CSE, IIT KGP

Chapter 4 C Program Control

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

CS110D: PROGRAMMING LANGUAGE I

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

A Look Back at Arithmetic Operators: the Increment and Decrement

Selection Statements. Pseudocode

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

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

IEEE 754 Floating-Point Format

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Applications of Arrays (1A) Young Won Lim 2/11/17

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

ECET 264 C Programming Language with Applications

Accessibility (1A) Young Won Lim 8/22/13

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

Chapter 5: Control Structures

Control Statements. Musa M. Ameen Computer Engineering Dept.

ECET 264 C Programming Language with Applications

Unit 7. Functions. Need of User Defined Functions

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Link 8.A Dynamic Linking

Pointers (1A) Young Won Lim 11/1/17

Flow Control. CSC215 Lecture

Fundamentals of Programming Session 8

Lecture 3: C Programm

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Transcription:

Day05 A Young W. Lim 2017-10-07 Sat Young W. Lim Day05 A 2017-10-07 Sat 1 / 14

Outline 1 Based on 2 Structured Programming (2) Conditions and Loops Conditional Statements Loop Statements Type Cast Young W. Lim Day05 A 2017-10-07 Sat 2 / 14

Based on "C How to Program", Paul Deitel and Harvey Deitel I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Day05 A 2017-10-07 Sat 3 / 14

The if selection statement a decision can be based on any expression if the expression evaluates to zero : false condition if the expression evaluates to non-zero : true condition a single entry / single exit structure Young W. Lim Day05 A 2017-10-07 Sat 4 / 14

The if... else selection statement a double selection statement a single entry / double exit structure the conditional operator ( A? B : C ) A: conditional expression B: if the condition A is true, then B is executed C: if the condition A is false, then C is executed Young W. Lim Day05 A 2017-10-07 Sat 5 / 14

The nested if... else selection statements the values in a conditional expression can also be actions to be executed several statments in the body of if or else statement, must be enclosed by braces : a compound statement / block {... } Nested if... else statement for a multiple case selection if (A) { S1; } else if (B) { S2; } else if (C) { S3; } else { S4; ] Young W. Lim Day05 A 2017-10-07 Sat 6 / 14

The while repetition statement specifies that an action is to be repeated as long as a condition is true eventually the condition will become false the repetition terminates 1 counter controlled repetition 2 sentinel controlled repetition (guard or sentry) Young W. Lim Day05 A 2017-10-07 Sat 7 / 14

The counter controlled repetition a counter variable to specify the number of times a set of statements should execute definite repetition : the number is know in advance a counter variable initialized with zero or one (counting from zero or one) the conditions must be different an accumulation variable for a total must be initialized with zero an uninitialized variable contains a garbage value must write before read Young W. Lim Day05 A 2017-10-07 Sat 8 / 14

The sentinel controlled repetition indefinite repetition the number of repetition is not known in advance a sentinel / dummy / flag value the end of data entry : the end of repetition a sentinel value must be carefully chosen must be different from the normal input data while (grade!= -1) S = S + grade; i = i + 1; } // 0 <= grade <= 100 // negative grade : not a data // sentinel value Young W. Lim Day05 A 2017-10-07 Sat 9 / 14

The float type to represent the numbers with decimal point (floating point numbers) precision : the number of digits below the decimal point format / conversion specifier %f the default precision : 6 digits below the decimal point format / conversion specifier %.2f the precision of value 2 : 2 digits below the decimal point %f and %.2f use rounding off in order to print 6 and 2 digits below the decimal point Young W. Lim Day05 A 2017-10-07 Sat 10 / 14

Type Cast - Explicit to produce a floating point calculation with integer values must cast (convert) the integers into floating point numbers the unary cast operator (float) explicit conversion : cast operators #include <stdio.h> int main(void) { int a; float x; printf("2 /3 = %d\n", 2 /3 ); printf("2 /3. = %f\n", 2 /3.); printf("2./3 = %f\n", 2./3 ); printf("2./3. = %f\n", 2./3.); a = 2.12345; x = 3; printf("a = %d\n", a); printf("x = %f\n", x); 2 /3 = 0 2 /3. = 0.666667 2./3 = 0.666667 2./3. = 0.666667 a = 2 x = 3.000000 } Young W. Lim Day05 A 2017-10-07 Sat 11 / 14

Type Cast - Implicit implicit conversion int / float, float / int ==> float / float int = float (truncation) float = int (add.0000) #include <stdio.h> int main(void) { int a; float x; printf("2 /3 = %d\n", 2 /3 ); printf("2 /3. = %f\n", 2 /3.); printf("2./3 = %f\n", 2./3 ); printf("2./3. = %f\n", 2./3.); a = 2.12345; x = 3; printf("a = %d\n", a); printf("x = %f\n", x); } 2 /3 = 0 2 /3. = 0.666667 2./3 = 0.666667 2./3. = 0.666667 a = 2 x = 3.000000 Young W. Lim Day05 A 2017-10-07 Sat 12 / 14

Type Cast Examples #include <stdio.h> int main(void) { int a = 2, b = 3; float x, x1, x2, x3, x4 ; printf("a= %d\nb= %d\n", a, b); x = (a / b); x1 = (float) (a / b); x2 = (float) a / (float) b; x3 = (float) a / b; x4 = a / (float) b; $ gcc -m32 t.c $./a.out a= 2 b= 3 a / b = 0.000000 (float) (a / b) = 0.000000 (float) a / (float) b = 0.666667 (float) a / b = 0.666667 a / (float) b = 0.666667 printf(" a / b = %f\n", x); printf("(float) (a / b) = %f\n", x1); printf("(float) a / (float) b = %f\n", x2); printf("(float) a / b = %f\n", x3); printf(" a / (float) b = %f\n", x4); } Young W. Lim Day05 A 2017-10-07 Sat 13 / 14

Type Cast Operators (float), (int), (char)... (float *), (int *), (char *)... pointer type cast unary operators from right to left associativity the same precedence level as + and - unary operators higher precedence level than +, -, *, / binary operators Young W. Lim Day05 A 2017-10-07 Sat 14 / 14