From Pseudcode Algorithms directly to C++ programs

Similar documents
Add Subtract Multiply Divide

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Programming. C++ Basics

Introduction to Programming using C++

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

Chapter 2 C++ Fundamentals

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Introduction to Programming

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

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

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

CSI33 Data Structures

Pseudo-code vs. Assembly. Introduction to High-Level Language Programming. Disadvantages of Assembly. Pseudo-code vs. High-level Programs

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

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

UNIT- 3 Introduction to C++

A First Program - Greeting.cpp

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

C/C++ Programming Lecture 7 Name:

Objectives. In this chapter, you will:

CSCE 110 PROGRAMMING FUNDAMENTALS

Chapter 2: Using Data

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

C++ DATA TYPES BASIC CONTROL FLOW

Chapter 1 Introduction to Computers and C++ Programming

CS Exam 2 Study Suggestions

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

VARIABLES & ASSIGNMENTS

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Introduction to Programming EC-105. Lecture 2

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

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Fundamentals of Programming CS-110. Lecture 2

PIC 10A. Review for Midterm I

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.

Increment and the While. Class 15

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

IS 0020 Program Design and Software Tools

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Topics. bool and string types input/output library functions comments memory allocation templates classes

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Computer Programming : C++

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

6.096 Introduction to C++ January (IAP) 2009

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Fundamentals of Structured Programming

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

Companion C++ Examples

ITP 342 Mobile App Dev. Code

Computer Components. Software{ User Programs. Operating System. Hardware

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Boolean Algebra Boolean Algebra

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

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

Creating a C++ Program

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Building Java Programs

pointers + memory double x; string a; int x; main overhead int y; main overhead

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

CSCI 123 Introduction to Programming Concepts in C++

2 nd Week Lecture Notes

Building Java Programs

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

CS2141 Software Development using C/C++ C++ Basics

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

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

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Variables and Operators 2/20/01 Lecture #

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

Computer Science II Lecture 1 Introduction and Background

CSCI 1061U Programming Workshop 2. C++ Basics

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Exam I Review Questions Fall 2010

Scientific Computing

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

Programming, numerics and optimization

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Lecture 3 Tao Wang 1

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 1 INTRODUCTION

COMP322 - Introduction to C++ Lecture 01 - Introduction

1a Computers, Problem Solving!

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

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

REVIEW. The C++ Programming Language. CS 151 Review #2

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Chapter 3 Problem Solving and the Computer

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Introduction to C ++

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

OBJECT ORIENTED PROGRAMMING

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

Transcription:

From Pseudcode Algorithms directly to C++ programs (Chapter 7) Part 1: Mapping Pseudo-code style to C++ style input, output, simple computation, lists, while loops, if statements a bit of grammar Part 2: Details compilers, parts of a program, C++ specifics, data types 1 Ways of specifying algorithms: averaging a set of scores natural language well, first you need the scores, and how many there are, and you add them up and divide by how many there were, to get the average. unless there aren t any scores pseudo-code get howmany if howmany is not 0 then set total to 0 set counter to 1 while (counter <= howmany) get a value for onescore set total to total + onescore set counter to counter + 1 set avg to total/howmany print average is average else print no scores to average 2

cout << "how many numbers shall I average?"; cin >> howmany; if (howmany!= 0) total = 0; counter = 1; while (counter <= howmany) cin >> onescore; total = total + onescore; counter = counter + 1; avg = total/ howmany; cout << "average is" << avg; else cout << "no scores to average"; 3 Machines cannot directly execute high level programs. A=B+C LOAD Compiler STORE Assembler 0101 010 1 100 1 Linker 0101 1001 0110 Loader 4

// here is the program again that adds 2 numbers, from previous lecture #include <iostream> int main() int limit, floor, max; cout << Enter 2 numbers ; cin >> limit; cin >> floor; max = limit + floor; cout << max; comment Include directives Functions Main function Declarations Body return 0; 5 Libraries frequently used pieces of code; standard pieces of code key example: input/output code #include a directive to the compiler to find a library of code of the given name #include <iostream> // include everything in the library called // iostream cin and cout are defined in this library (our get and print ) cin >> limit; 24 <enter> cout << limit + 45; cout << all done ; cout << the value of limit is << limit << \n ; cout << all done ; 6

Definitions: function main () function a function is a named package of instructions. A function takes input (called arguments) and executes the instructions on that input, and produces ( returns ) a single result Consider calculator functions square(4) returns 16 in C++ the main() function - where your C++ instructions are located - it is called main and when it Exit code - the value returned by the main function - indicates whether the program terminated successfully return (0); - anything else means the program did not terminate successfully 7 Dev C++ program template includes all the necessary framing of the code #include <iostream.h> #include <stdlib.h> int main () // your program will go in here // system ( PAUSE ); return 0; C++ data types (there are more) Integer 7 2-5 int real 7.0 2.5-0.45 double A single character 7 a? char 8

Two classes of data items (named storage locations) Constants initialized unchangeable Variables const double timehalf = 1.5; const double doubletime = 2.0; const int reghours = 40; int double // and later in the program hoursworked, overtime; payrate; overtime = hoursworked - reghours; Names in programs are called identifiers. An identifier can consist of any combination of letters, digits, and _, except: - cannot start with a digit - cannot be same name as a C++ reserved word - is case-sensitiv 9 General format in C++ The Assignment Statement <variable> = <expression>; step 1: machine evaluates the expression step 2: machine puts it in ( assigns it ) to the named storage location const int magic=30; int floor, max, limit; cin >> floor; cin >> max; limit = floor + max * magic; cout << floor, max, limit, magic; 10

Addition + C = A + B; Arithmetic Operators Subtraction - C = A B; Multiplication * C = A * B; Division / C = A / B; Still more details. 1. operator precedence: *, / before +, - 2. integer division 3. type casting a result is converted to proper data typechar initial; answer = floor * limit - dog + max * 7; answer = (floor * limit) dog + (max * 7); 11 Comparison operators the same value as == 2 == 5 false not the same value as!= 2!= 5 true less than < 2 < 5 true greater than > 2 > 5 false less than or equal to <= 2<=5 true greater than or equal to >= 2 >=5 false Boolean Operators and && (2 < 5) && ( 10 == 10) true (2 == 5) && ( 10 == 10) false or (2 == 5) (10 == 10) true (2 == 5) (10 == 50) false not!!( 2 == 5 ) true 12