Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Similar documents
Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Fundamentals of Programming CS-110. Lecture 2

Lecture #9 **go to Blackboard ** Review from Shackelford Reading If statements Loops. Using if statements and loops

Introduction to Programming EC-105. Lecture 2

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

2 nd Week Lecture Notes

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

Lab # 02. Basic Elements of C++ _ Part1

Week 3: File I/O and Formatting 3.7 Formatting Output

Getting started with C++ (Part 2)

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

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

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

C++ Input/Output: Streams

Programming. C++ Basics

The C++ Language. Arizona State University 1

Multiple Choice (Questions 1 14) 28 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.

Lesson 2 Variables and I/O

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CS242 COMPUTER PROGRAMMING

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

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

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

Multiple Choice (Questions 1 13) 26 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

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

Chapter 3 - Notes Input/Output

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

COP Programming Assignment #7

1. In C++, reserved words are the same as predefined identifiers. a. True

A First Program - Greeting.cpp

Chapter 2: Overview of C++

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

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Maciej Sobieraj. Lecture 1

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

CHAPTER 3 BASIC INSTRUCTION OF C++

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Introduction to Programming

Objectives. In this chapter, you will:

Why Is Repetition Needed?

LECTURE 02 INTRODUCTION TO C++

Software Design & Programming I

Name. Classes and Objects 1. We re going to develop a class to help out Café Below is the definition for a class called

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Computer Programming : C++

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

Chapter 2: Introduction to C++

WARM UP LESSONS BARE BASICS

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Introduction to Programming using C++

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

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

Computer Science II Lecture 1 Introduction and Background

download instant at Introduction to C++

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

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Reviewing all Topics this term

Introduction to C++ (Extensions to C)

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

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

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

Chapter 2. C++ Basics

I/O Streams and Standard I/O Devices (cont d.)

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

Chapter 2 C++ Fundamentals

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

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

Should you know scanf and printf?

Creating a C++ Program

Chapter 1 Introduction to Computers and C++ Programming

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

REPETITION CONTROL STRUCTURE LOGO

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Chapter 2 Basic Elements of C++

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

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

VARIABLES & ASSIGNMENTS

Add Subtract Multiply Divide

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

Expressions, Input, Output and Data Type Conversions

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

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

Introduction to C# Applications

6.096 Introduction to C++ January (IAP) 2009

CS103L SPRING 2018 UNIT 7: FILE I/O

Engineering Problem Solving with C++, Etter/Ingber

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

Transcription:

Today in CS161 Week #3 Learn about Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program See example demo programs CS161 Week #3 1

Data Types and Variables To work with programs, we need to create variables that will hold information that we will need Think of it as memory locations with names We can store: Single character data Whole numbers Real numbers char initial; int age; float salary; CS161 Week #3 2

Data Types and Variables The name in front is called a data type and it represents how much memory to set aside and what can be done with that memory char will set aside 1 byte of memory and hold 1 character a, b, z, A, 1, & etc. int will set aside a word of memory and hold a whole number float will hold something with a decimal component e.g., 3.14159 CS161 Week #3 3

Data Types and Variables The name after the data type represents the handle for how we can access that memory So, saying char initial; //means that I can store a single character //and access it through the name initial The name must start with a letter and be any sequence of letters, digits and underscores: count count_2_numbers my_age CS161 Week #3 4

Output Stream We can output messages, integers, floating point numbers and characters using the insertion (<<) operator cout << We did it! ; cout << whole_number; cout << age; cout << salary; cout <<endl; //end followed by lower case l CS161 Week #3 5

Input Stream We can read integers, floating point numbers and characters using the extraction (>>) operator It looks like: cin >> variable; We can t, however, control what the user types in. Anything the user types in...goes into the input buffer once they hit the enter (or return) key...regardless of what our programs might want! CS161 Week #3 6

Input Stream Therefore, it is important to prompt users, so they know exactly what they are supposed to type in And, it is important to understand how input operations behave CS161 Week #3 7

Input Stream user @ keyboard Input Buffer stores everything typed by user cin >> variable Memory for our variables CS161 Week #3 8

Input Stream user @ keyboard abcd<enter> abcd\n \n is an escape sequence that stands for the carriage return! char ch; garbage Memory for ch CS161 Week #3 9

Input Stream user @ keyboard abcd<enter> bcd\n This skips leading whitespace and reads a single character from the input buffer char ch; cin >> ch; a cin >> ch; Memory for ch CS161 Week #3 10

Input Stream What about integers? int number; cin >> number; Skips leading whitespace and reads in digits until it gets to a non-digit, from the input buffer. CS161 Week #3 11

Input Stream What about floating point numbers? float inches; cin >> inches; Skips leading whitespace and reads in digits and optionally one decimal point until it gets to a non-digit or more than one decimal point from the input buffer. CS161 Week #3 12

Today in CS161 Applying what we learn to programs Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program See example demo programs Using graphics! CS161 Week #3 13

Now let s use this in a program! Now that we have learned some about Data types Variables Input And, Output Let s put it all together!!!!!!!!!!!!!!! CS161 Week #3 14

#include <iostream> using namespace std; int main() { cout <<"We are going to have a great time!"; cin.get(); //wait so the window doesn't go away } return 0; CS161 Week #3 15

cin.get(); //wait so the window doesn't go away #include <iostream> using namespace std; int main() { int num_classes = 0; //the number of classes you are taking //prompt and read in the number of classes cout << How many classes are you taking this term? ; cin >> num_classes; cin.get(); //echo what we got back to the user cout << You are taking << num_classes << classes <<endl; cout << Hit ENTER to finish ;

Convert inches to millimeters #include <iostream> using namespace std; // *********************************** // Karla S. Fant // CS161 Programming Assignment #0 // Purpose of this program is to convert // inches entered in by the user into // millimeters and display the results // ********************************** int main() { CS161 Week #3 17

(Different Kind of Comment...) #include <iostream> using namespace std; /* *********************************** Karla S. Fant CS161 Programming Assignment #0 Purpose of this program is to convert inches entered in by the user into millimeters and display the results ********************************* */ int main() { CS161 Week #3 18

Convert inches to millimeters //Define variables float inches; //to save # inches float mm; //to save the result //Step #1, welcome the user cout << Welcome! We will be converting << inches to mm today <<endl; CS161 Week #3 19

(A different way to do this...) //Define variables float inches, mm; //to save # inches //to save the result //Step #1, welcome the user cout << Welcome! We will be converting ; cout << inches to mm today <<endl; (NOTE: endl is end followed by a letter l) CS161 Week #3 20

Convert inches to millimeters //Step #2, Get the input (prompt, read) cout << Please enter the number of inches << that you wish to convert: ; cin >> inches; cin.get(); //read the # inches //remove the newline //echo what was entered cout << You entered: <<inches << in <<endl; CS161 Week #3 21

Convert inches to millimeters //Step #3 Convert inches to millimeters mm = 25.4 * inches; //Step #4 Display the results cout <<inches << in converts to <<mm << mm <<endl; //Step #5 Sign off message cout << Thank you for using CONVERT <<endl << Hit ENTER to finish! ; cin.get(); //wait for user input return 0; } CS161 Week #3 22

Next in CS161 Next Topic Learn about If and else statements Rewrite our First Program Using if and else statements See example demo programs CS161 Week #3 23

Selective Execution Most programs are not as simple as converting inches to mm! We need to select from alternatives... think of the ATM example... this can be done using an if statement an if allows us to select between 2 choices for example, we can select one thing or another, depending on the user CS161 Week #3 24

if Statements For example, we can change our inches to mm conversion program, allowing the user to select whether they want to convert from inches to mm, or mm to inches! We will give the user a choice... type m to convert to mm type i to convert to inches CS161 Week #3 25

if Statements have the form... 1) One alternative: if (logical expression) single C++ statement; char selection; cout << Enter a selection (m or i): ; cin >> selection; if (selection == i ) //better to say if ( i == selection) cout << You selected to convert to inches! << endl; CS161 Week #3 26

if Statements have the form... 2) Tw o alternatives: if (logical expression) single C++ statement; else single C++ statement; if (selection == m ) cout << Converting inches -> mm ; else cout << Converting mm -> inches ; CS161 Week #3 27

if Statements have the form... This means that either the first statement is executed when running your program OR the second statement is executed. BOTH sets of statements are NEVER used. ONE OR THE OTHER! If the comparison is true - the first set is used; If the comparison is false - the second set is used; CS161 Week #3 28

if Statements have the form... When an if is encountered, the logical expression is TRUE if it is non zero. In this case, the statement following the expression is executed. Otherwise, if the logical expression evaluates to zero it means it is FALSE. In this case, if there is an else the statement following the else is executed. If there is no else then nothing is done if the logical expression evalu ates to zero (FALSE). CS161 Week #3 29

if Statements have the form... 3) Two or more alternatives: if (logical expression) single C++ statement; else if (logical expression) single C++ statement; if (selection == m ) cout << Converting inches -> mm ; else if (selection == i ) cout << Converting mm -> inches ; CS161 Week #3 30

Compound if statements... 4) You might want more than a single statement to be executed given an alternative...so instead of a single statem ent, you can u se a compound statement if (logical expression) { Many C++ statements; } else //optional CS161 Week #3 31

Example of if Statements #include <iostream> using namespace std; int main() { char selection; //the user s answer one character float inches, mm; //prompt for input from the user cout << Enter i to convert to inches << and m to convert to mm: ; cin >> selection; //get the response cin.get(); CS161 Week #3 32

Example of if Statements if ( m == selection) //notice expression! { cout << Enter the # inches: ; cin >> inches; cin.get(); mm = 25.4 * inches; //this is multiplication! cout << inches << in converts to << mm << millimeters << endl; } CS161 Week #3 33

Example of if Statements else //selection is not an m { cout << Enter the # millimeters: ; cin >> mm; cin.get(); inches = mm / 25.4; cout << mm << mm converts to << inches << inches << endl; } cin.get(); //wait for user input CS161 Week #3 34

Or, use the else if sequence else if ( i == selection) //selection is not an m { cout << Enter the # millimeters: ; cin >> mm; cin.get(); inches = mm / 25.4; //this is division cout << mm << mm converts to << inches << inches << endl; } else cout << Neither i nor m were selected << endl; CS161 Week #3 35

logical expressions The comparison operators may be: Relational Operators: > for greater than < for less than >= for greater than or equal <= for less than or equal Equality Operators: == for equ al to!= for not equal to CS161 Week #3 36

Let s Write a Graphics Program New Topic time for Graphics! To create a graphics window you have to use initwindow To allow the user to interact and set the size we will use an integer variable: int window_size; Then, we will prompt the user for the size: cout << "How big of a window do you want : "; cin >> window_size; initwindow(window_size, window_size); CS161 Week #3 37

CS161 Week #3 38

Next, Let s draw a circle Drawing To create an outline of a circle, We will ask the user for the radius and then place it at the center of the window (window width divided by 2) The circle outline is drawn in the color established by the setcolor function: cout << "What color do you want...enter in a number 1-15 "; cin >> color; setcolor(color); //the color for the circle cout << "How big do you want the circle? "; cin >> circle_radius; circle(window_size/2, window_size/2, circle_radius); CS161 Week #3 39

CS161 Week #3 40

Now Create a Filled Area Drawing To create filled circle We can either use the fillellipse function or floodfill Here is an example of both of these They fill in the color set by setfillstyle setfillstyle(solid_fill,color); fillellipse(window_size/2,window_size/2,circle_radius,circle_radius); //or setfillstyle(solid_fill,15-color); //changing the color. floodfill(0,0,color); CS161 Week #3 41

Hit Enter in the graphics window. CS161 Week #3 42

#include "graphics.h" #include <iostream> using namespace std; The Complete Program int main() { //Here is where I am going to put my variables int window_size; int color; int circle_radius; cout <<"This program will work with what we learned about in Lecture 5"; cout <<endl <<endl; //have an extra blank line cout << "How big of a window do you want (pick a number less than 1200): "; cin >> window_size; initwindow(window_size, window_size); CS161 Week #3 43

cout << "What color do you want...enter in a number 1-15 (15 is white) "; cin >> color; setcolor(color); cout << "How big do you want the circle? "; cin >> circle_radius; circle(window_size/2, window_size/2, circle_radius); getch(); //if you don't do this...the first circle disappears! setfillstyle(solid_fill,color); fillellipse(window_size/2,window_size/2,circle_radius,circle_radius); //let's have some fun! setfillstyle(solid_fill,15-color); floodfill(0,0,color); } //keep the windows open longer. getch(); cin.get(); //wait so the window doesn't go away return 0; CS161 Week #3 44