1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.

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

 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.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
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/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
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.
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.
Chapter 2: Introduction to C++.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
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.
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++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –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.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
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 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 part #1 C++ Program Structure
Introduction to C++ October 2, 2017.
Chapter 2 – Getting Started
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:

1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I

2 9/1/06CS150 Introduction to Computer Science 1 Challenge Solution 1 //******************************************************** 2 // File name: hello.cpp 3 // Author: Chadd Williams 4 // Date: 09/01/ // Purpose: This program asks for a user’s name and then 6 // displays a personalized message. 7 //******************************************************** 8 #include 9 #include “stdafx.h” 10 #include // load the string library 11 using namespace std; int main() 14 { 15 string name; // store the user’s name in this variable 16 int number; 17 cout << “What is your name? ” << endl; cin >> name; // read input from the user cout << “Hello “ << name << “!” << endl; return 0; 24 }

3 9/1/06CS150 Introduction to Computer Science 1 Today  On Wednesday I showed you a C++ program that displays a personalized message to the user  What are the main components of that program?  Today we will o learn how C++ stores data o Some of the different types of data that C++ can store

4 9/1/06CS150 Introduction to Computer Science 1 main Function  int main()  Marks the beginning of a function  A function is a group of one or more programming statements  The set of parentheses indicate a function  C++ is case-sensitive o int Main() is incorrect!!!

5 9/1/06CS150 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

6 9/1/06CS150 Introduction to Computer Science 1 cout Object  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

7 9/1/06CS150 Introduction to Computer Science 1 cout Object  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

8 9/1/06CS150 Introduction to Computer Science 1 cout Object  << is used to separate the different output items  Example: o cout << “Type your name, then press enter” << endl;  It is illegal to break up the string literals across lines o cout << “Type your name, then o press enter” << endl; o Is illegal!!

9 9/1/06CS150 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

10 9/1/06CS150 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;

11 9/1/06CS150 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

12 9/1/06CS150 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: 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

13 9/1/06CS150 Introduction to Computer Science 1 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”;

14 9/1/06CS150 Introduction to Computer Science 1 Variables  A variable is a named storage location for holding data  Part of the job of programming is to determine how many variables a program will need  Let’s look at program 2-7 on p. 41, also on the next slide

15 9/1/06CS150 Introduction to Computer Science 1 Variables 1 #include "stdafx.h" 2 #include 3 4 using namespace std; 5 6 int main() 7 { 8 int number; 9 10 number = 5; 11 cout << "The value of number is " << "number" << endl; 12 cout << "The value of number is " << number << endl; number = 7; 15 cout << "Now the value of number is " << number << endl; return 0; 18 }

16 9/1/06CS150 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 of integer  Variable definitions end with a semicolon

17 9/1/06CS150 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

18 9/1/06CS150 Introduction to Computer Science 1 Program Output  What do you think is the program’s output?

19 9/1/06CS150 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 9/1/06CS150 Introduction to Computer Science 1 Exercises  Which of the following are legal C++ statements? o a = 7; o 7 = a; o 7 = 7;

21 9/1/06CS150 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 p of your textbook