What Actions Do We Have Part 1

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Bill Tucker Austin Community College COSC 1315
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Computing Fundamentals
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Introduction to C++ Programming
Programming Funamental slides
Programming Funamental slides
Course websites CS201 page link at my website: Lecture slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Life is Full of Alternatives
Arithmetic Operations
What Actions Do We Have Part 1
Life is Full of Alternatives
Reading from and Writing to Files
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

What Actions Do We Have Part 1 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Today Last week we looked at a C++ program in some detail What were the main components of that program? Today we will Learn how to make C++ manipulate the data that we stored Look at examples of simple arithmetic operators 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 C++ Statements There are two main types of C++ statements Declaration statements We looked at these last time. They are used to determine what data needs to be stored Executable statements Assignment statements Input/Output operations Arithmetic statements Today we will investigate assignment and I/O statements. We will leave arithmetic statements till Friday 09/08/04 CS150 Introduction to Computer Science 1

Assignment Statements Assign values to variables Variables must have been declared Assignment operator is = The left operand must be a variable The right operand is an expression, where an expression can be a variable, constant, value, or complex expression using arithmetic operators The left operand gets the value of right operand 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Assignments Examples int num1 = 4; int num2, sum; num2 = 5; num1 = num2; sum = num1 + num2; 09/08/04 CS150 Introduction to Computer Science 1

Input/Output Operations Output operations allow you to write information to a computer screen Input operations allow you to read information in from keyboard Other possible sources of I/O: files, printers, etc Stream: output and input is accomplished by using streams of characters Must have: #include<iostream> using namespace std; 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Output Output operator (insertion operator): << Standard output (monitor screen): cout The value to the right of the operator (right operand) is displayed on the screen If the right operand is within double quotes, then it is output exactly as it appears The exception is if it is an escape character \ If the right operand is a variable or constant, then the value of that variable or constant is output 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Output What is the output? cout << “Enter the distance in miles” << endl; cout << “The distance in kilometers is ” << kms << endl; You must always use the insertion operator << to separate the different components you wish to output endl will move the cursor to a new line All output statements must end in a semicolon Output strings within double quotes “” should always appear on one line 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Escape Characters These are special characters that can be output They are always preceded by a backslash \ Examples of escape characters include: \n: moves the cursor to the beginning of the next line Equivalent to endl \r: moves the cursor to the beginning of the current line \t: moves the cursor to the next tab stop \\: displays the backslash \”: outputs the double quotes 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples What is the output? cout << “This is a C++ program\n”; cout << “This is a \nC++ program”; cout << “\”This is a C++ program\””; cout << “This is a\tC++\tprogram”; 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Input Input operator (extraction operator): >> Gets input from some device/file Standard input (from keyboard): cin Whatever the user types in is stored in the variable to the right of the operator (the right operand) That variable must have already been declared Given a data type and allocated space in memory When reading in the data typed by the user Any spaces before the data item are skipped Continues to read until the user hits return 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Input Examples: cin >> miles; The variable miles must have already been declared int num1; int num2; cin >> num1 >> num2; 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write the C++ statements necessary to perform the following operations: Display the message below onto the screen “C++ is a useful language to know” Read in from the user their initials (assume there are only two) and their age 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem What is the output? cout << “My name is: ”; cout << “Doe, Jane.” << endl; cout << “I live in “; cout << “Ann Arbor, MI “; cout << “and my zip code is “ << 48109 << “. “ << endl; 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? cout << “Enter two numbers: “; cin >> a >> b; a = a + 5.0; b = 3.0 * b; cout << “a = “ << a << endl; cout << “b = “ << b << endl; Assume 5.0 and 7.0 are entered for a & b 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? Assume x = 2, y = 3 cout << x; cout << x + x; cout << “x=“; cout << x + y << “ = “ << y + x; z = x + y; cin >> x >> y; // cout << “x + y = “ << x + y; cout << “\n”; 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a program that reads in last week’s and this week’s gas prices and prints out the difference 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write the complete program that calculates the area of a circle based on the radius input by the user 09/08/04 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we learnt How to assign values to variables using the assignment operator How to output strings and variables to the screen How to read in input entered by the user using the keyboard We have covered p. 26 - 31 of your textbook 09/08/04 CS150 Introduction to Computer Science 1