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

Similar documents
Fundamentals of Programming Session 7

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

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

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

Add Subtract Multiply Divide

Programming for Engineers Iteration

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

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Lecture 3 Tao Wang 1

VARIABLES & ASSIGNMENTS

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

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

To become familiar with array manipulation, searching, and sorting.

In this chapter you will learn:

Why Is Repetition Needed?

Chapter 3 Structured Program Development in C Part II

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

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

Chapter 2 Basic Elements of C++

More Programming Constructs -- Introduction

UNIT- 3 Introduction to C++

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

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

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

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

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

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

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

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C/C++ Programming Lecture 7 Name:

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Chapter 2 - Control Structures

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

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

Chapter 2 - Control Structures

WARM UP LESSONS BARE BASICS

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Chapter 2 - Control Structures

Structured Program Development in C

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

Study recommendations for Quiz 5 - Chapter 5

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Information Science 1

CHAPTER 3 Expressions, Functions, Output

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Introduction to Programming

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

W3101: Programming Languages C++ Ramana Isukapalli

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

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CSI33 Data Structures

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CSCE Practice Midterm. Data Types

REPETITION CONTROL STRUCTURE LOGO

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

This watermark does not appear in the registered version - Slide 1

Information Science 1

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Chapter 7. Additional Control Structures

C++ Programming: From Problem Analysis to Program Design, Third Edition

1.3b Type Conversion

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Engineering Problem Solving with C++, Etter/Ingber

Computer System and programming in C

Lecture 4 Tao Wang 1

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

LECTURE 3 C++ Basics Part 2

Fundamentals of Programming CS-110. Lecture 2

Chapter 7. Expressions and Assignment Statements ISBN

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

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

C Programming

Increment and the While. Class 15

Computational Expression

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Java Primer 1: Types, Classes and Operators

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Transcription:

Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise refinement o Examine different ways of writing assignments o Learn about the increment and decrement operators o Start looking at the for repetition structure 10/3/05 CS150 Introduction to Computer Science 1 2 12.1 Write a program that reads an undetermined number of student grades and calculates the average student grade The answer is on the following slides Solution #include stdafx.h #include <iostream> using namespace std; int main() int gradecounter; int grade; int average; double total; total = 0; gradecounter = 0; 10/3/05 CS150 Introduction to Computer Science 1 3 10/3/05 CS150 Introduction to Computer Science 1 4 Solution cout << "Enter grade, -1 to end: "; cin >> grade; while ( grade!= -1 ) total = total + grade; gradecounter = gradecounter + 1; cout << "Enter grade, -1 to end: "; cin >> grade; if ( gradecounter!= 0 ) average = static_cast< double >( total ) / gradecounter; cout << "Class average is " << average << endl; else cout << "No grades were entered" << endl; The program that we have just solved contained the line: average = static_cast< double >( total ) / gradecounter; o Where total and gradecounter are int s o And average is a double What would be stored in average if total was 310 and gradecounter was 4? o Without cast: o With cast: 10/3/05 CS150 Introduction to Computer Science 1 5 10/3/05 CS150 Introduction to Computer Science 1 6 1

To produce a floating point calculation with integer values, we must convert one of the operands to a floating point static_cast< double >( total ) o Stores a temporary version of total as a double o If total was 310, it will be stored as 310.0 o This temporary value will be used in calculations Called an explicit conversion C++ can only evaluate expressions where both operands are of the same type static_cast< double >( total ) / gradecounter o Is trying to divide a double by an int double / int Compiler performs a promotion (implicit conversion) on the int to make it a double o If gradecounter was 4, will now be 4.0 10/3/05 CS150 Introduction to Computer Science 1 7 10/3/05 CS150 Introduction to Computer Science 1 8 average = static_cast< double >( total ) / gradecounter; If total was originally 310 and gradecounter was 4, compiler will o 310.0 / 4.0 static_cast It s a unary operator The syntax: o static_cast<data type>( variable ) o Results in 77.5 If average is a double, then 77.5 is stored If average is an int then the fractional part will be truncated 10/3/05 CS150 Introduction to Computer Science 1 9 10/3/05 CS150 Introduction to Computer Science 1 10 Operator Precedence & Associativity () L->R Parentheses static_cast<type>() L->R Unary!, +, - R->L Negation, Unary +, - *,/,% L->R Mult, div, mod +, - L->R Add, Subtract <<, >> L->R Insertion/extraction <, <=, >, >= L->R Relational ==,!= L->R Equality && L->R And L->R Or = R->L Assignment 10/3/05 CS150 Introduction to Computer Science 1 11 A Note on Stepwise Refinement P. 87-89 in your book describe the process of top-down stepwise refinement This is a really useful process for solving a problem It describes how to start from the top-most description of the problem and refining it until you have a detailed description of the process Be sure to read it! 10/3/05 CS150 Introduction to Computer Science 1 12 2

Top-Down, Stepwise Refinement There is a description of how to solve a complete problem using top-down, stepwise refinement on p. 94-98 The solution to this problem requires that an if selection structure be embedded within a while repetition structure Assignment Operators We ve seen that C++ provides the ability to abbreviate an assignment operator in which the same variable appears on either side of the operator sum = sum + num; Can be abbreviated to sum += num; 10/3/05 CS150 Introduction to Computer Science 1 13 10/3/05 CS150 Introduction to Computer Science 1 14 Assignment Operators This abbreviation can be done to the following operators o + - * / % Examples, where c = 3, e = 4 o c += 7 o e %= 2 o c *= 3 o e /= 4 o e -= 1 10/3/05 CS150 Introduction to Computer Science 1 15 Increment and Decrement Operators ++ is the unary increment operator x++; is the same as x = x + 1; -- is the unary decrement operator x--; is the same as x = x - 1; 10/3/05 CS150 Introduction to Computer Science 1 16 Pre-increment vs. post-increment Example Pre k = --x; k = ++x; Increment/ decrement x then assign value of x to k Post k =x--; k = x++; Assign value of x to k, then increment or decrement x 12.2: What is the output if i = 2? cout << Value of x is << i; cout << Value of i++ is << i++; cout << Value of ++i is << ++i; cout << Value of --i is << --i; cout << Value of i-- is << i--; 10/3/05 CS150 Introduction to Computer Science 1 17 10/3/05 CS150 Introduction to Computer Science 1 18 3

Operator Precedence () L->R Parentheses ++, --, static_cast<type>() L->R Unary post-op ++, --,!, +, - R->L Negation, Unary pre-op *,/,% L->R Mult, div, mod +, - L->R Add, Subtract <<, >> L->R Insertion/extraction <, <=, >, >= L->R Relational For loops 3 main things for loops: o Initialization of lcv, testing of lcv, updating lcv For loops provide a concise way to do this for (count = 0; count < 5; count++) cout << count << endl; ==,!= L->R Equality && L->R And L->R Or?: R->L Conditional =, +=, -=, *=, /=, %= R->L Assignment 10/3/05 CS150 Introduction to Computer Science 1 19 10/3/05 CS150 Introduction to Computer Science 1 20 General Format for (initialization expression; loop repetition condition; update expression) statements; Examples 12.3: Write a for loop that outputs odd numbers less than 10 12.4: Write a program that computes the factorial of a number. The factorial of a number is given by the formula o N! = N*(N-1)* *2*1 where 0!=1, 1!=1, 2!=2, 3!=6, 10/3/05 CS150 Introduction to Computer Science 1 21 10/3/05 CS150 Introduction to Computer Science 1 22 Localized Declarations for (int i = 0; i < n; i++) cout << i << endl; cout << i << endl; i is declared ONLY in the loop Equivalent Logic 12.5: Rewrite the following for loop as a while loop. for (i = 5; i < 10; i+= 2) cout << i; 12.6: What does this output? 10/3/05 CS150 Introduction to Computer Science 1 23 10/3/05 CS150 Introduction to Computer Science 1 24 4

12.7: Write a program that will print the sum of the odd integers between 1 and 50 inclusive. Write one program using a while and the other using a for loop 12.8: Write a program that allows the user to enter an unknown number of integer values one at a time. When the user enters -999, you are to terminate the loop and print the following: o The sum of all integers inputted o The average of all integers inputted o The largest integer of all integers inputted 10/3/05 CS150 Introduction to Computer Science 1 25 10/3/05 CS150 Introduction to Computer Science 1 26 Summary In today s lecture we covered o Type casting o Top-down, stepwise refinement o Abbreviating assignment operators o Increment and decrement operators o for repetition structures Readings o P. 92 type casting o P. 93, p. 113 formatting output o P. 94-98 top-down, stepwise refinement o P. 98 Assignment operators o P. 99-102 Increment and decrement operators o P. 104-113 for repetition structures 10/3/05 CS150 Introduction to Computer Science 1 27 5