Fundamentals of Programming Session 7

Similar documents
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 in C Part II

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

Structured Program Development in C

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

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

Chapter 3 Structured Program Development

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

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

Fundamentals of Programming Session 8

Fundamentals of Programming Session 20

CS110D: PROGRAMMING LANGUAGE I

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

Fundamentals of Programming Session 9

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

Structured Program Development

Fundamentals of Programming Session 4

Programming for Engineers Iteration

Fundamentals of Programming Session 12

Fundamentals of Programming Session 20

Information Science 1

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

Information Science 1

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

Fundamentals of Programming Session 25

Control Structures (Deitel chapter 4,5)

Control Statements. Musa M. Ameen Computer Engineering Dept.

1.3b Type Conversion

Fundamentals of Programming Session 15

Fundamentals of Programming Session 19

Fundamentals of Programming Session 13

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Fundamentals of Programming Session 14

Chapter 3 Structure of a C Program

Visual C# Instructor s Manual Table of Contents

1 EEE1008 C Programming

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

JavaScript: Control Statements I Pearson Education, Inc. All rights reserved.

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

Full file at

Introduction to Programming

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

Fundamentals of Programming Session 23

Fundamentals of Programming Session 19

Programming in C++ 6. Floating point data types

Fundamentals of Programming Session 2

Introduction to Programming

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

ECE 122 Engineering Problem Solving with Java

C Programming

Chapter 2. Elementary Programming

More Programming Constructs -- Introduction

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Fundamentals of Programming Session 1

Fundamentals of Programming Session 24

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

Add Subtract Multiply Divide

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

Control Statements: Part 1

Operators & Expressions

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

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

C: How to Program. Week /Mar/05

Course Outline Introduction to C-Programming

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

Chapter 12 Variables and Operators

Data Types and Variables in C language

Chapter 4: Basic C Operators

Operators and Expressions:

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Chapter 2 - Control Structures

Data Types. Data Types. Integer Types. Signed Integers

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

A Look Back at Arithmetic Operators: the Increment and Decrement

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

Chapter 2: Using Data

Special Section: Building Your Own Compiler

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

CS 106 Introduction to Computer Science I

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Chapter 12 Variables and Operators

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

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

Chapter 3: Program Statements

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

In this chapter you ll learn:

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Fundamentals of Programming Session 1

Chapter 2 - Control Structures

COMP Primitive and Class Types. Yi Hong May 14, 2015

Visual Basic 2010 How to Program by Pearson Education, Inc. All Rights Reserved.

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

Declaration and Memory

Chapter 2, Part III Arithmetic Operators and Decision Making

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

Program Development. Java Program Statements. Design. Requirements. Testing. Implementation

Transcription:

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

Outlines Formulating Algorithms Case Study 1. Counter-Controlled Repetition 2. Sentinel-Controlled Repetition 3. Nested Control Structures Assignment Operators Increment and Decrement Operators Repetition Essentials for Repetition Statement 2

3 Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition)

Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition) 4 The pseudocode algorithm in Fig. 3.7 solves the more general class averaging problem. This algorithm was developed after only two levels of refinement. Sometimes more levels are necessary. Although only integer grades are entered, the averaging calculation is likely to produce a decimal number with a decimal point. The type int cannot represent such a number. The program introduces the data type float to handle numbers with decimal points (called floating-point numbers) and introduces a special operator called a cast operator to handle the averaging calculation.

5 Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition)

6 Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition)

7 Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition)

Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition) 8 The variable average is defined to be of type float (line 12) to capture the fractional result of our calculation. However, the result of the calculation total / counter is an integer because total and counter are both integer variables. To produce a floating-point calculation with integer values, we must create temporary values that are floating-point numbers. C provides the unary cast operator to accomplish this task. Line 38 average = ( float ) total / counter; includes the cast operator (float), which creates a temporary floating-point copy of its operand, total. The value stored in total is still an integer.

Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition) 9 Using a cast operator in this manner is called explicit conversion. Most computers can evaluate arithmetic expressions only in which the data types of the operands are identical. To ensure that the operands are of the same type, the compiler performs an operation called promotion (also called implicit conversion) on selected operands. For example, in an expression containing the data types int and float, copies of int operands are made and promoted to float. Cast operators are available for most data types. The cast operator is formed by placing parentheses around a data type name. The cast operator is a unary operator, i.e., an operator that takes only one operand.

Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition) 10 Figure 3.8 uses the printf conversion specifier %.2f (line 41) to print the value of average. The f specifies that a floating-point value will be printed. The.2 is the precision with which the value will be displayed. If the %f conversion specifier is used (without specifying the precision), the default precision of 6 is used exactly as if the conversion specifier %.6f had been used. When floating-point values are printed with precision, the printed value is rounded to the indicated number of decimal positions, but the value in memory is unaltered. When the following statements are executed, the values 3.45 and 3.4 are printed. printf( "%.2f\n", 3.446 ); /* prints 3.45 */ printf( "%.1f\n", 3.446 ); /* prints 3.4 */

11 Formulating Algorithms Case Study 2 (Sentinel- Controlled Repetition)

Formulating Algorithms Case Study 3 (Nested Control Structures) 12 We ll once again formulate the algorithm using pseudocode and write a corresponding C program. In this case study we ll see the only other structured way control statements may be connected in C, namely through nesting of one control statement within another. Consider the following problem statement: A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, 10 of the students who completed this course took the licensing examination. Naturally, the college wants to know how well its students did on the exam. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name a 1 is written if the student passed the exam and a 2 if the student failed.

Formulating Algorithms Case Study 3 (Nested Control Structures) Your program should analyze the results of the exam as follows: Input each test result (i.e., a 1 or a 2). Display the prompting message Enter result each time the program requests another test result. Count the number of test results of each type. Display a summary of the test results indicating the number of students who passed and the number who failed. If more than eight students passed the exam, print the message Bonus to instructor! 13

Formulating Algorithms Case Study 3 (Nested Control Structures) 14 After reading the problem statement carefully, we make the following observations: The program must process 10 test results. A counter-controlled loop will be used. Each test result is a number either a 1 or a 2. Each time the program reads a test result, the program must determine if the number is a 1 or a 2. We test for a 1 in our algorithm. If the number is not a 1, we assume that it s a 2. Two counters are used one to count the number of students who passed the exam and one to count the number of students who failed the exam. After the program has processed all the results, it must decide if more than 8 students passed the exam.

15 Formulating Algorithms Case Study 3 (Nested Control Structures)

16 Formulating Algorithms Case Study 3 (Nested Control Structures)

17 Formulating Algorithms Case Study 3 (Nested Control Structures)

18 Formulating Algorithms Case Study 3 (Nested Control Structures)

19 Formulating Algorithms Case Study 3 (Nested Control Structures)

Assignment Operators C provides several assignment operators for abbreviating assignment expressions. For example, the statement c = c + 3; can be abbreviated with the addition assignment operator += as c += 3; Any statement of the form variable = variable operator expression; where operator is one of the binary operators +, -, *, / or % (or others we ll discuss in Chapter 10), can be written in the form variable operator= expression; Thus the assignment c += 3 adds 3 to c. 20

Increment and Decrement Operators 21 C also provides the unary increment operator, ++, and the unary decrement operator, --. If a variable c is incremented by 1, the increment operator ++ can be used rather than the expressions c = c + 1 or c += 1. If increment or decrement operators are placed before a variable (i.e., prefixed), they re referred to as the preincrement or predecrement operators, respectively. If increment or decrement operators are placed after a variable (i.e., postfixed), they re referred to as the postincrement or postdecrement operators, respectively.

22 Increment and Decrement Operators

23 Increment and Decrement Operators

Increment and Decrement Operators 24 It s important to note here that when incrementing or decrementing a variable in a statement by itself, the preincrement and postincrement forms have the same effect. It s only when a variable appears in the context of a larger expression that preincrementing and postincrementing have different effects (and similarly for predecrementing and postdecrementing). Of the expressions we ve studied thus far, only a simple variable name may be used as the operand of an increment or decrement operator. Attempting to use the increment or decrement operator on an expression other than a simple variable name is a syntax error e.g. writing ++(x+1).

Repetition Essentials Counter-controlled repetition requires: The name of a control variable (or loop counter). The initial value of the control variable. The increment (or decrement) by which the control variable is modified each time through the loop. The condition that tests for the final value of the control variable (i.e., whether looping should continue). 25

26 Repetition Essentials

27 Repetition Essentials

Repetition Essentials 28 You could make the program in Fig. 4.1 more concise by initializing counter to 0 and by replacing the while statement with while ( ++counter <= 10 ) printf( "%d\n", counter ); This code saves a statement because the incrementing is done directly in the while condition before the condition is tested. Also, this code eliminates the need for the braces around the body of the while because the while now contains only one statement. Some programmers feel that this makes the code too cryptic and error prone.

for Repetition Statement The for repetition statement handles all the details of counter-controlled repetition. To illustrate its power, let s rewrite the program of Fig. 4.1. The result is shown in Fig. 4.2. 29

30 for Repetition Statement