Today in CS161 Lecture #5 Learn about… Data types (char, int, float) Input and Output (cin, cout) Writing our First Program Write the Inches to MM Program.

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

Computer Science 1620 Loops.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Computer Science 1620 Programming & Problem Solving.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Today in CS161 Lecture #4 Solving Problems with Computers Walk through the Tic Tac Toe Algorithm Getting ready for Creating Programs Turn the Inches to.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
CS161 Topic #15 1 Today in CS161 Lecture #15 Practicing! Writing Programs to Practice Write a game program (1 player) of Mad Math Reuse the functions to.
Input & Output: Console
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
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.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Today in CS161 Lecture #9**go to Blackboard ** Practicing… Review from Shackelford Reading If statements Loops Begin writing Programs Using if statements.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Fundamental Programming: Fundamental Programming Introduction to C++
1 CS161 Introduction to Computer Science Topic #3.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Today in CS161 Lecture #8 Learn about… Logicals: And, Or, Not Repetition: loops! Rewrite our First Program Using loops Graphics Let’s make the graphics.
CSC 107 – Programming For Science. Announcements.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Objective: Students will be able to: Declare and use variables Input integers.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 CS161 Introduction to Computer Science Topic #8.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Today in CS161 Week #2 Solving Problems with Computers What are Algorithms Write a short Algorithm Preparing to write programs On Unix and Mac systems.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
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
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Computing Fundamentals
Basic Elements of C++.
Today in CS161 Week #2 Preparing to write programs Assignments
Variables A piece of memory set aside to store data
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Today in CS161 Week #3 Learn about… Writing our First Program
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Today in CS161 Week #5 **weekly visit D2L** Practicing…
Today in CS161 Week #4 Learn about… Rewrite our First Program
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Let’s Write a Graphics Program
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Fundamental Programming
Today in CS161 Week #5 **go to Blackboard ** Practicing…
Chapter 2 part #3 C++ Input / Output
Presentation transcript:

Today in CS161 Lecture #5 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 Lecture #51

2 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 datachar initial; Whole numbersint age; Real numbers float salary;

CS161 Lecture #5 3 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.,

CS161 Lecture #5 4 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 Lecture #5 5 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 Lecture #5 6 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 Lecture #5 7 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 Lecture #5 8 Input Stream keyboard Input Buffer stores everything typed by user Memory for our variables cin >> variable

CS161 Lecture #5 9 Input Stream keyboard abcd\n Memory for ch abcd ‘\n’ is an escape sequence that stands for the carriage return! char ch; garbage

CS161 Lecture #5 10 Input Stream keyboard bcd\n Memory for ch char ch; cin >> ch; abcd ‘a’ This skips leading whitespace and reads a single character from the input buffer cin >> ch;

CS161 Lecture #5 11 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 Lecture #5 12 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.

Today in CS161 Lecture #6 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 Lecture #613

CS161 Lecture #6 14 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!!!!!!!!!!!!!!!

#include 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; } 15 CS161 Lecture #6

#include 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”; cin.get(); //wait so the window doesn't go away return 0; } 16 CS161 Lecture #6

Convert inches to millimeters #include 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 Lecture #517

(Different Kind of Comment...) #include 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 Lecture #618

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 Lecture #619

(A different way to do this...) //Define variables float inches,//to save # inches mm;//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 Lecture #620

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;//read the # inches //echo what was entered cout <<“You entered: “ <<inches <<“in” <<endl; CS161 Lecture #621

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 Lecture #622

CS161 Lecture #6 23 Program Style The Style of your program is important because by doing it cleanly, you can create programs that are easier to read and easier tocorrect Style includes......indentation...grouping like elements...using blank lines...variables and program names

CS161 Lecture #6 24 Poor Program Style #include using namespace std; int main() { float celsius; float fahrenheit; cout <<"Please enter” >celsius; fahrenheit = (celsius * 9.0/5.0) +32.0;cout <<celsius;cout <<" Celsius = " <<fahrenheit;cout <<" Fahrenheit"; cout<<endl; return 0;}

CS161 Lecture #6 25 Better Program Style # include using namespace std; //This program converts temperatures…… int main() { float celsius;//temp in celsius float fahr;//temp in Fahrs //Read in the temperature in celsius cout << "Enter temp in Celsius: “ << endl; cin >> celsius;

CS161 Lecture #6 26 Better Program Style //Convert celsius to fahrenheits fahr = (celsius * 9.0 / 5.0) ; //Print the results cout << celsius << " Celsius = " << fahr; cout << " Fahrenheit" << endl; cout <<“Please hit ENTER when finished:”; cin.get(); return 0; }

//Test Program #1 //Written by Karla Fant for CS161 Fall 2009 //The purpose of this program is to show how to display //a welcome message to the user //These first two lines make sure that we can perform Input and Output #include using namespace std; //This is where the heart of the program is…you will always have the following line: int main() { //”see out” is how we can output a message to the screen // The << is called the “insertion” operator; anything inside the “ “ is displayed cout <<"We are going to have a great time!"; //On PC’s you need to wait for the user to hit enter otherwise the screen just flashes //and you won’t see anything at all! cin.get() will wait for the user to hit Enter…. cin.get(); //wait so the window doesn't go away return 0; //return “success” back to the operating system! } 27 CS161 Lecture #6

#include using namespace std; // This is a simple program … add blank lines and more crazy // text displayed. // Written by: Beth Miller int main() { cout << "We are going to have a great time!!!!!!!!!!!!!!!!!!!"; cout << endl <<endl; cout <<"WOWWWWWWWWWWWWWWWWWWWW" <<endl; cout << endl <<endl <<endl; cout << "HIT ENTER TO END THIS SESSION"; cin.get(); return 0; } 28 CS161 Lecture #6

Same example w/ Poor Style 29 CS161 Lecture #6

Now…add some graphics With Dev C++ we can do regular C++ or we can add a graphics window and draw as well Again, graphics is extra and not required in this course To start you have to open a graphics window and decide what the size is: This window is from 0,0 (upper left) to 500,500 (lower right) You can make this bigger…we will see this in class To output text we use either: outtextxy(x,y, “text to display”); Or, outtext(“text at the current position”); CS161 Lecture #630

#include "graphics.h" #include using namespace std; int main() { cout <<"We are going to have a great time!"; initwindow(500,500); outtextxy(250,250,"HI!"); getch(); cin.get(); //wait so the window doesn't go away return 0; } 31 CS161 Lecture #6

#include "graphics.h" #include using namespace std; const int ScreenWidth = 1280;//Lets talk about this! const int ScreenHeight = 1024; //This is where the real program begins int main() { //Let's establish our graphics window //A common size is 640x480...but it can be any size you want //I am going full screen here, so it will be 1280x1024 initwindow(ScreenWidth,ScreenHeight); 32 CS161 Lecture #6

setbkcolor(BLUE); //Let's add a background color setcolor(RED); //This is the text color //Let's center the text //Other choices would be LEFT_TEXT or RIGHT_TEXT moveto(ScreenWidth/2,ScreenHeight/2); settextjustify(CENTER_TEXT, CENTER_TEXT); settextstyle(TRIPLEX_FONT,0,6); outtext("Welcome to CS161!!!!!!!!!!!!!"); getch(); //wait for user to press a key closegraph(); //close graphics window return 0; } 33 CS161 Lecture #6

Next in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements See example demo programs CS161 Lecture #734

CS161 Lecture #7 35 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 Lecture #7 36 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, ormm to inches! We will give the user a choice... type ‘m’ to convert to mm type ‘i’ to convert to inches

CS161 Lecture #7 37 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’) cout << “You selected to convert to inches!” << endl;

CS161 Lecture #7 38 if Statements have the form... 2) Two alternatives: if (logical expression) single C++ statement; else single C++ statement; if (selection == ‘m’) cout mm”; else cout inches”;

CS161 Lecture #7 39 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 Lecture #7 40 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 evaluates to zero (FALSE).

CS161 Lecture #7 41 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 mm”; else if (selection == ‘i’) cout inches”;

CS161 Lecture #7 42 Compound if statements... 4) You might want more than a single statement to be executed given an alternative...so instead of a single statement, you can use a compound statement if (logical expression) { Many C++ statements; } else //optional

CS161 Lecture #7 43 Example of if Statements #include 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 Lecture #7 44 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 Lecture #7 45 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 Lecture #7 46 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 Lecture #7 47 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 equal to != for not equal to