CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 7: Errors 1 Based on slides created by Bjarne Stroustrup.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 5: Functions 1 Based on slides created by Bjarne Stroustrup.
True or false A variable of type char can hold the value 301. ( F )
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
C++ fundamentals.
The switch Statement, DecimalFormat, and Introduction to Looping
Basic Elements of C++ Chapter 2.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
High-Level Programming Languages: C++
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 3: Objects, Types, and Values 1 Based on slides.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ crash course Class 8 statements, sort, flight times program.
Previously Repetition Structures While, Do-While, For.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 CS161 Introduction to Computer Science Topic #8.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CPS120: Introduction to Computer Science Decision Making in Programs.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Chapter 6: User-Defined Functions I
REPETITION CONTROL STRUCTURE
Basic Elements of C++.
Introduction to Programming
Bjarne Stroustrup Chapter 4 Computation Bjarne Stroustrup
Basic Elements of C++ Chapter 2.
11/10/2018.
Modification of Slides by
Iteration with While You can say that again.
One-Dimensional Array Introduction Lesson xx
IO Overview CSCE 121 J. Michael Moore
Selection CSCE 121 J. Michael Moore.
Bjarne Stroustrup Chapter 4 Computation Bjarne Stroustrup
Chapter 4 Computation Bjarne Stroustrup
Bjarne Stroustrup Chapter 4 Computation Bjarne Stroustrup
Presentation transcript:

CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

CSCE 121: Set 4: Computation Input, Compute, Output 2 Data (information) comes into program via keyboard, reading files, other input devices, other programs Program does something to input data to produce the output Display on screen, write to file, send to other output devices or other programs Input Computation Output

CSCE 121: Set 4: Computation Tools for Designing Computation How to figure out what computations to do to accomplish task? Divide and Conquer: break up large computation into many smaller ones Abstraction: provide a higher-level concept that hides detail Emphasis on structure and organization! 3

CSCE 121: Set 4: Computation Expressions An expression is made out of operators and operands Operator: what is to be done Operand: what data the operator works on (remember type safety!) A single literal or variable name is an expression Build up larger expressions by combining smaller ones with operands 4

CSCE 121: Set 4: Computation Examples of Expressions 13 x x+y b1 == b2 b*b – 4*c*c b*(b-4)*a*c 5

CSCE 121: Set 4: Computation Assignment as an Operator a = b is an expression Assignment operator = has two operands, the left-hand-side (LHS) and the right-hand-side (RHS) Variable name on LHS (e.g., a) refers to a location (address) Variable name on RHS (e.g., b) refers to the value stored in the location with that name 6

CSCE 121: Set 4: Computation Statements An expression followed by a semicolon A declaration A compound statement (group together a sequence of statements with { } ) A control flow statement (if, while, return,…) 7

CSCE 121: Set 4: Computation Statement Examples a = b*3; int n; {a = b; c = a;} 8

CSCE 121: Set 4: Computation Selection: If 9 if (condition) // returns boolean statement-1 else statement-2 if (a < b) // no semi-colon max = b; else // no semi-colon max = a;

CSCE 121: Set 4: Computation Nested Ifs 10 if (choice == ‘a’) … else if (choice == ‘b’) … else if (choice == ‘c’) … Can get confusing; motivates next selection statement

CSCE 121: Set 4: Computation Selection: switch 11 switch (choice) { // choice must be int, char, or // enumeration type case ‘a’: // values in case labels must be // constant expressions … break; // if you forget break, execution will // continue to next case case ‘b’: … break; case ‘c’: … break; default: // what to do if there is no match … break; }

CSCE 121: Set 4: Computation Iteration: while 12 while (condition) // returns boolean statement int i = 0; while (i < 100) { cout << i; ++i; // shorthand for i = i+1 }

CSCE 121: Set 4: Computation Iteration: for 13 for (int i = 0; i < 100; ++i) { cout << i; } Puts control variable management all in one place. Variable i only exists inside the for loop (note declaration). No control variable (i) modification in body of loop!

CSCE 121: Set 4: Computation Vectors Way to keep a collection of data items, all of the same type (e.g., a bunch of ints) Automatically increases in size as you add data items to the collection 14

CSCE 121: Set 4: Computation Some Vector Details To declare a vector variable named v that contains objects of type T: vector v; To add a data item x to a vector (at the end): v.push_back(x); // function call, more soon To get the number of data items in vector v: v.size() // another function call 15

CSCE 121: Set 4: Computation Vector Example Obtain a sequence of temperatures from the user and then find the mean (average). See sample code (pg. 122). 16

CSCE 121: Set 4: Computation Sorting a vector Another useful function that works on vectors: sort the elements in the vector – Rearrange them in the vector to be in sorted order Let v be the name of the vector. sort(v.begin(),v.end()); (More detail about this will be given later in the semester; for now, just use it.) 17

CSCE 121: Set 4: Computation Exercise Write a C++ program that gets a list of words from the user and then prints them all out, but with NO DUPLICATES First figure out what your strategy will be Then convert into C++ code 18

CSCE 121: Set 4: Computation Acknowledgments Photo on slide 1: “Exercise…” by A Health Blog, licensed under CC BY-SA 2.0Exercise…A Health BlogCC BY-SA 2.0 Slides are based on those for the textbook: t 19