1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.

Slides:



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

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
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.
Computer Science 1620 Loops.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
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.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 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 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS150 Introduction to Computer Science 1
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.
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
1 CS161 Introduction to Computer Science Topic #3.
First steps Jordi Cortadella Department of Computer Science.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Completing the Problem-Solving Process
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 2 part #1 C++ Program Structure
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
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
Arithmetic Operations
What Actions Do We Have Part 1
Reading from and Writing to Files
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Presentation transcript:

1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I

2 September 6, 2005CS150 Introduction to Computer Science I Today  We have looked at a C++ program in some detail  What were the main components of that program?  Today we will o Learn how to make C++ manipulate the data that we stored o Look at examples of simple arithmetic operators

3 September 6, 2005CS150 Introduction to Computer Science I C++ Statements  There are two main types of C++ statements o Declaration statements  We looked at these last time. They are used to determine what data needs to be stored o Executable statements  Assignment statements  Input/Output operations  Arithmetic statements  Today we will investigate assignment and I/O statements.

4 September 6, 2005CS150 Introduction to Computer Science I Assignment Statements  Assign values to variables o 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

5 September 6, 2005CS150 Introduction to Computer Science I Assignments  Examples int num1 = 4; int num2, sum; num2 = 5; num1 = num2; sum = num1 + num2;

6 September 6, 2005CS150 Introduction to Computer Science I 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: o #include o using namespace std;

7 September 6, 2005CS150 Introduction to Computer Science I 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 o 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 \ o If the right operand is a variable or constant, then the value of that variable or constant is output

8 September 6, 2005CS150 Introduction to Computer Science I 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

9 September 6, 2005CS150 Introduction to Computer Science I Escape Characters  These are special characters that can be output  They are always preceded by a backslash \  Examples of escape characters include: o \n : moves the cursor to the beginning of the next line  Equivalent to endl o \r : moves the cursor to the beginning of the current line o \t : moves the cursor to the next tab stop o \\ : displays the backslash o \” : outputs the double quotes

10 September 6, 2005CS150 Introduction to Computer Science I Examples  What is the output? o cout << “This is a C++ program\n”; o cout << “This is a \nC++ program”; o cout << “\”This is a C++ program\””; o cout << “This is a\tC++\tprogram”;

11 September 6, 2005CS150 Introduction to Computer Science I 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 o Given a data type and allocated space in memory  When reading in the data typed by the user o Any spaces before the data item are skipped o Continues to read until the user hits return

12 September 6, 2005CS150 Introduction to Computer Science I Input  Examples: cin >> miles;  The variable miles must have already been declared int num1; int num2; cin >> num1 >> num2;

13 September 6, 2005CS150 Introduction to Computer Science I Problem  Write the C++ statements necessary to perform the following operations: o Display the message below onto the screen “C++is a useful language to know” o Read in from the user their initials (assume there are only two) and their age

14 September 6, 2005CS150 Introduction to Computer Science I 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 “ << << “. “ << endl;

15 September 6, 2005CS150 Introduction to Computer Science I 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

16 September 6, 2005CS150 Introduction to Computer Science I 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”;

17 September 6, 2005CS150 Introduction to Computer Science I Program  Write a program that reads in last week’s and this week’s gas prices and prints out the difference

18 September 6, 2005CS150 Introduction to Computer Science I Problem  Write the complete program that calculates the area of a circle based on the radius input by the user

19 September 6, 2005CS150 Introduction to Computer Science I Summary  In today’s lecture we learned o How to assign values to variables using the assignment operator o How to output strings and variables to the screen o How to read in input entered by the user using the keyboard  We have covered p of your textbook