Fundamentals of Programming Session 8

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

Fundamentals of Programming Session 4

Fundamentals of Programming Session 7

Fundamentals of Programming Session 12

Fundamentals of Programming Session 25

Fundamentals of Programming Session 15

Fundamentals of Programming Session 24

Fundamentals of Programming Session 20

Fundamentals of Programming Session 20

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Fundamentals of Programming Session 19

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

Fundamentals of Programming Session 23

Fundamentals of Programming Session 14

Unit 4. Input/Output Functions

Fundamentals of Programming Session 13

Fundamentals of Programming Session 19

Introduction to Programming

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

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

Programming for Engineers Introduction to C

C: How to Program. Week /Mar/05

Chapter 2 - Control Structures

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

Fundamentals of Programming. Lecture 11: C Characters and Strings

Introduction to Programming

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

Chapter 2 - Introduction to C Programming

Fundamentals of Programming Session 2

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

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

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Control Statements: Part Pearson Education, Inc. All rights reserved.

Data Type Fall 2014 Jinkyu Jeong

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

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

Arithmetic Expressions in C

Data Types. Data Types. Integer Types. Signed Integers

Fundamentals of Programming Session 9

ANSI C Programming Simple Programs

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

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

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

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

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

Chapter 4 C Program Control

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

Chapter 2 - Control Structures

Should you know scanf and printf?

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

Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

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

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

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

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

Programming for Engineers Iteration

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

THE FUNDAMENTAL DATA TYPES

Reserved Words and Identifiers

ECET 264 C Programming Language with Applications. C Program Control

Fundamentals of Programming Session 1

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Chapter 2 - Control Structures

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Fundamental of Programming (C)

Fundamentals of Programming Session 1

LESSON 4. The DATA TYPE char

C Functions. 5.2 Program Modules in C

Data Types and Variables in C language

Dept. of CSE, IIT KGP

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

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

Structured Program Development in C

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

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

Chapter 3 Structured Program Development

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

Chapter 1 & 2 Introduction to C Language

CS242 COMPUTER PROGRAMMING

Course Outline 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.

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

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

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

Fundamentals of Programming

Formatted Output Pearson Education, Inc. All rights reserved.

Fundamental of Programming (C)

Computer System and programming in C

2. Numbers In, Numbers Out

CSE123. Program Design and Modular Programming Functions 1-1

COSC2031 Software Tools Introduction to C

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Unit 7. Functions. Need of User Defined Functions

UNIT 3 OPERATORS. [Marks- 12]

Introduction to C Programming

Input/Output Week 5:Lesson 16.1

Number Systems, Scalar Types, and Input and Output

Transcription:

Fundamentals of Programming Session 8 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology

Outlines Examples Using the for Statement switch Multiple-Selection Statement 2

Examples Using the for Statement The next two examples provide simple applications of the for statement. Figure 4.5 uses the for statement to sum all the even integers from 2 to 100. 3

4 Examples Using the for Statement

Examples Using the for Statement The body of the for statement in Fig. 4.5 could actually be merged into the rightmost portion of the for header by using the comma operator as follows: for ( number = 2; number <= 100; sum += number, number += 2 ) ; /* empty statement */ The initialization sum = 0 could also be merged into the initialization section of the for. 5

Examples Using the for Statement 6 Consider the following problem statement: A person invests $1000.00 in a savings account yielding 5% interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts: a = p(1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate n is the number of years a is the amount on deposit at the end of the n th year. This problem involves a loop that performs the indicated calculation for each of the 10 years the money remains on deposit. The solution is shown in Fig. 4.6.

7 Examples Using the for Statement

8 Examples Using the for Statement

Examples Using the for Statement 9 Although C does not include an exponentiation operator, we can use the Standard Library function pow for this purpose. The function pow(x, y) calculates the value of x raised to the yth power. It takes two arguments of type double and returns a double value. Type double is a floating-point type much like float, but typically a variable of type double can store a value of much greater magnitude with greater precision than float. The header <math.h> (line 4) should be included whenever a math function such as pow is used.

Examples Using the for Statement The conversion specifier %21.2f is used to print the value of the variable amount in the program. The 21 in the conversion specifier denotes the field width in which the value will be printed. A field width of 21 specifies that the value printed will appear in 21 print positions. The 2 specifies the precision (i.e., the number of decimal positions). If the number of characters displayed is less than the field width, then the value will automatically be right justified in the field. This is particularly useful for aligning floating-point values with the same precision (so that their decimal points align vertically). 10

Examples Using the for Statement To left justify a value in a field, place a - (minus sign) between the % and the field width. The minus sign may also be used to left justify integers (such as in %- 6d) and character strings (such as in %-8s). 11

switch Multiple-Selection Statement Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume, and different actions are taken. This is called multiple selection. C provides the switch multiple-selection statement to handle such decision making. The switch statement consists of a series of case labels, an optional default case and statements to execute for each case. 12 Figure 4.7 uses switch to count the number of each different letter grade students earned on an exam.

13 switch Multiple-Selection Statement

14 switch Multiple-Selection Statement

15 switch Multiple-Selection Statement

16 switch Multiple-Selection Statement

17 switch Multiple-Selection Statement

switch Multiple-Selection Statement In the while header (line 19), while ( ( grade = getchar() )!= EOF ) the parenthesized assignment (grade = getchar()) executes first. The getchar function (from <stdio.h>) reads one character from the keyboard and stores that character in the integer variable grade. Characters are normally stored in variables of type char. However, an important feature of C is that characters can be stored in any integer data type because they re usually represented as one-byte integers in the computer. 18

switch Multiple-Selection Statement Thus, we can treat a character as either an integer or a character, depending on its use. For example, the statement 19 printf( "The character (%c) has the value %d.\n", 'a', 'a' ); uses the conversion specifiers %c and %d to print the character a and its integer value, respectively. The result is The character (a) has the value 97. The integer 97 is the character s numerical representation in the computer.

switch Multiple-Selection Statement Characters can be read with scanf by using the conversion specifier %c. Assignments as a whole actually have a value. This value is assigned to the variable on the left side of =. 20 The value of the assignment expression grade = getchar() is the character that is returned by getchar and assigned to the variable grade.

switch Multiple-Selection Statement 21 We use EOF (which normally has the value -1) as the sentinel value. The user types a system-dependent keystroke combination to mean end of file i.e., I have no more data to enter. EOF is a symbolic integer constant defined in the <stdio.h> header (we ll see how symbolic constants are defined in Chapter 6). If the value assigned to grade is equal to EOF, the program terminates. Chose to represent characters in this program as ints because EOF has an integer value (normally -1).

22 switch Multiple-Selection Statement

switch Multiple-Selection Statement 23 Listing several case labels together (such as case 'D': case 'd': in Fig. 4.7) simply means that the same set of actions is to occur for either of these cases. When using the switch statement, remember that each individual case can test only a constant integral expression i.e., any combination of character constants and integer constants that evaluates to a constant integer value. A character constant is represented as the specific character in single quotes, such as 'A'.

switch Multiple-Selection Statement Characters must be enclosed within single quotes to be recognized as character constants characters in double quotes are recognized as strings. Integer constants are simply integer values. In our example, we have used character constants. Remember that characters are represented as small integer values. 24

switch Multiple-Selection Statement 25 In addition to int and char, C provides types short (an abbreviation of short int) and long (an abbreviation of long int). C specifies that the minimum range of values for short integers is 32768 to +32767. For the vast majority of integer calculations, long integers are sufficient. The standard specifies that the minimum range of values for long integers is 2147483648 to +2147483647. The standard states that the range of values for an int is at least the same as the range for short integers and no larger than the range for long integers.