What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.

Slides:



Advertisements
Similar presentations
Introduction to C++ Programming. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Advertisements

Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
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.
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.
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.
CS150 Introduction to Computer Science 1
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
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)
Input & Output: Console
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 C++ Syntax and Semantics, and the Program Development Process.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
1 CS161 Introduction to Computer Science Topic #3.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2 part #1 C++ Program Structure
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 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
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.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
C++ Basics Lecture 2.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming Language
Chapter 2 part #1 C++ Program Structure
Introduction to C++ October 2, 2017.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Arithmetic Operations
What Actions Do We Have Part 1
Reading from and Writing to Files
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008

2 8/29/08CS150 Introduction to Computer Science 1 Today  Last time, we saw a C++ program o You wrote your first program in Lab!  What are the main components of every C++ program?  Today we will o learn more about output o learn how C++ stores data  what types of data can C++ store?

3 8/29/08CS150 Introduction to Computer Science 1 main Function  int main()  A group of one or more programming statements  The set of parentheses indicate a function  C++ is case-sensitive o int Main() is incorrect!!!

4 8/29/08CS150 Introduction to Computer Science 1 cout Object  cout object is the standard output object  The monitor is the standard output device  cout is a stream object and works with streams of data o Streams of characters o Streams?  What statement gives us access to cout ?

5 8/29/08CS150 Introduction to Computer Science 1 cout Object  Output operator (insertion operator): <<  Standard output (monitor screen): cout  cout << “Hello out there!”; o right operand  string literal  variable

6 8/29/08CS150 Introduction to Computer Science 1 cout Object  What is the output? cout << “Type your name, then press enter” << endl;  endl will move the cursor to a new line  Statement must end in a semicolon

7 8/29/08CS150 Introduction to Computer Science 1 cout Object  Other ways of outputting the same message cout << “Type your name, “ << “then press enter” << endl; cout << “Type your name, “; cout << "then press enter” << endl;  Everything will output to the same line unless you specify otherwise

8 8/29/08CS150 Introduction to Computer Science 1 cout Object  Separate components with << int kms; kms = 4; cout << “Enter the distance in miles” << endl; cout << “The distance in kilometers is ” << kms << endl;  Don’t break string literals across a line cout << “Type your name, then press enter” << endl; o Compiler error!

9 8/29/08CS150 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 “ << << “. “ << endl;

10 8/29/08CS150 Introduction to Computer Science 1 Problem  How are the following different? o what will each look like on the  What is the output? cout << “Please type your name: ” << endl;  What is the output? cout << “Please type your name: ”;

11 8/29/08CS150 Introduction to Computer Science 1 Escape Characters  These are special characters that can be output o part of a string literal  They are always preceded by a backslash \  Examples of escape characters include: o \n : new 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 o \a : outputs a beep!

12 8/29/08CS150 Introduction to Computer Science 1 Problem  Write the C++ statements necessary to perform the following operation o Display the message below onto the screen “C++is a useful language to know”

13 8/29/08CS150 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”;

14 8/29/08CS150 Introduction to Computer Science 1 Variables  Named storage location for holding data o named piece of memory  You need to determine what variables you need o what data do we need to handle? Address Contents Shereen string name; int kms;

15 8/29/08CS150 Introduction to Computer Science 1 Variable Definition  int number;  Tells the compiler o The variable’s type ( int ) o The variable’s name (number)  int is short for integer  Variable definitions end with a semicolon

16 8/29/08CS150 Introduction to Computer Science 1 Assignment  number = 5;  = is an operator that copies the value on its right into the variable on its left  The item to the left of the = operator must be a variable  Let’s look at program 2-7 on p. 38, also on the next slide

17 8/29/08CS150 Introduction to Computer Science 1 Variables 1 // This program has a variable 2 #include 3 using namespace std; 4 5 int main() // what is the output of this program? 6 { 7 int number; 8 9 number = 5; 10 cout << "The value of number is " << "number" << endl; 11 cout << "The value of number is " << number << endl; number = 7; 14 cout << "Now the value of number is " << number << endl; return 0; 17 }

18 8/29/08CS150 Introduction to Computer Science 1 Problem  What variables/data will you need for the following program? (talk it over with your neighbor)  Write a program that will determine your average exam grade in this course and output that average to the screen. Remember, we will have three midterm exams. Don’t count the final exam in this average.

19 8/29/08CS150 Introduction to Computer Science 1 String Literals  Placing quotations around a variable name changes it to a string literal cout << "The value of number is " << "number" << endl;  What is the output of the statement o cout << “endl”;

20 8/29/08CS150 Introduction to Computer Science 1 Exercises  Which of the following are legal C++ statements? int a; a = 7; 7 = a; 7 = 7;

21 8/29/08CS150 Introduction to Computer Science 1 What problems can you spot? 1 // What problems can you spot here? 2 #include “iostream” 3 user namespace standard; 4 5 int MAIN() 6 { /* this program has errors in it! Can you find them? 7 string number; 8 int number; 9 number = 5; 10 cout << "The value of number is " << "number" endl; 11 cout >> "The value of number is " >> number >> endl; = number; 14 cout << "Now the value of number is " << number << endl; }

22 8/29/08CS150 Introduction to Computer Science 1 Summary  In today’s lecture we covered o main function o cout object o How data that is used by a program can be declared and stored  We have covered sections 2.2 & 2.5 of your textbook  Homework: page 67: 1-5,