C++ Basics Lecture 2.

Slides:



Advertisements
Similar presentations
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Advertisements

CPS120: Introduction to Computer Science INPUT/OUTPUT.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Chapter 2 C++ Basics. 2 Overview Variables, Constants and Assignments (2.1) Input and Output (2.2) Data Types and Expressions (2.3) Simple Flow of Control.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
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 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Problem Solving with C++
C++ Basics Joaquin Vila. For Thursday Read Savitch Do practice problems.
10/8/2015 CSI Chapter 02 1 Statements A statement in C++ is the basic unit for building C++ programs.  A statement performs an action.  A complete.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: Basic Elements of C++.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Fundamental Programming: Fundamental Programming Introduction to C++
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 
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Slide 1. Slide 2 Chapter 1 C++ Basics Slide 3 Learning Objectives  Introduction to C++  Origins, Object-Oriented Programming, Terms  Variables, Expressions,
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
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.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
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
CS 240 Computer Programming 1
Variables, Identifiers, Assignments, Input/Output
TK1913 C++ Programming Basic Elements of C++.
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 C++ Basics. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow.
Chapter 2 C++ Basics.
CMPT 201.
Basics (Variables, Assignments, I/O)
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Basics (Variables, Assignments, I/O)
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Basics (Variables, Assignments, I/O)
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Introduction to C++ Programming
CISC 1600/1610 Computer Science I
Variables, Identifiers, Assignments, Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
What Actions Do We Have Part 1
Chapter 2 part #3 C++ Input / Output
Using string type variables
Unit 3: Variables in Java
Presentation transcript:

C++ Basics Lecture 2

Variables and Assignments Programs manipulate data such as numbers and letters C++ uses variables to name & store data Variable num1 Memory num2 123 456 grade B Name (Identifier) Value

Variables and Assignments Variable Declarations Syntax: Type_Name Variable_Name1, Variable_Name_2, …; Example: int count, total; double price; All variables must be declared before they are used in the program.

Variables and Assignments #include <iostream> using namespace std; int main() { int number_of_bars; double one_weight, total_weight; cout<< "Enter the number of candy bars in a package\n"; cout<< "and the weight in ounces of one candy bar.\n"; cout<< "Then press return.\n"; cin>> number_of_bars; cin>> one_weight; total_weight = one_weight * number_of_bars; cout<< "Total weight is " <<total_weight << " ounces.\n"; return 0; }

Variables and Assignments Names: Identifiers Begin with a letter or underscore Remaining characters must be Letters or Digits or Underscore  sum Big_Bonus  3X  _address  %change  program1.cpp  _3X  price-1  total5* 

Variables and Assignments Notes on Identifiers C++ is case sensitive Average AVERAGE average Use meaningful names Keywords/reserved words int double

Variables and Assignments Assignment Statements Syntax: Variable = Expression ; Examples: distance = speed_rate * time; count = count + 2; weight = 35;

Variables and Assignments Initializing variables Syntax: Type Variable_Name_1 = Expression_for_value_1, Variable_Name_2 = Expression_for_value_2,..; Type Variable_Name_1 ( Expression_for_value_1), Variable_Name_2 ( Expression_for_value_2),..; Examples: int count = 0, max = 555; int count(0), max(555);

Input and Output Input stream Output stream The stream of input that is being fed into the computer for the program to use cin ( cin>> number_of_bars; ) Output stream The stream of output generated by the program cout (cout<< "Enter the number of candy bars.\n";)

Input and Output Input Using cin cin >> grade1; Syntax: cin >> Variable_1 >> Variable_2 >>…; Examples: cin >> number >> size; cin >> grade1 >> grade2; cin >> grade1; cin >> grade2;

Input and Output Output Using cout Syntax: cout << Variable_or_string_1 << Variable_or_string_2 << … ; Examples: cout << number << size; cout << “Hello \n”;

Input and Output Include directive Using directive Namespaces (collection of names) # include <iostream> using namespace std;

Input and Output Escape Sequences The backslash \ preceding a character tells the compiler that the sequence following the \ doesn’t have the same meaning as the character appearing by itself. New_line \n Horizontal tab \t Alert \a Backslash \\ Double quote \” Others: v, b, r, ?, :, \000, \xhhh

Input and Output New line & Blank lines cout<< “\n”; cout<< endl; If you could include the \n at the end of a longer string, then use \n. If the \n would appear by itself as the short string “\n”, then use endl instead.

Input and Output Formatting numbers with a decimal point double price = 84.50; cout << “The price is $” << price<<endl; The price is $84.5 The price is $84.500000 The price is $84.50 The price is $84.5000e01

Input and Output Magic Formula cout.setf (ios::fixed); cout.setf(ios::showpoint); cout.precision(2);

Input and Output Line Breaks in I/O You can keep input and output on the same line by omitting the \n or endl at the end of the last prompt line. Example: cout<< “Enter the cost per person: $”; cin >> cost_per_person; Enter the cost per person: $5.40

Lab Exercise Your local department store is having its annual sale. Write a program that calculates the sale price for items in the store. The program should prompt the user for the original price and the discount (10%, 25%, etc.)

Lab Exercise include <iostream> using namespace std; int main() { double discount, price; cout<<"Enter the price of the item: $"; cin>> price; cout<<"Enter the discount for the this item: %"; cin>> discount; price = price - (price * discount/100); cout.setf (ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout<<"\nThe sale price for your item is: $"<< price; cout<<endl; return 0; }

Lab Exercise Write a Program to convert a temperature in degrees Fahrenheit to degree Celcius. Data Requirement Problem input int Fahrenheit Problem OutPut Float Celcius Formula Celcius = (5/9) * (faherenheit – 32)

Lab Exercise write a Program to read two data items and print their sum, difference, product, and quotient. 2. Write a program that reads in the length and width of a rectangular yard and the length and width of a rectangular house situated in the yard. Your program should compute the time required to cut the grass at the rate of 2 square meters per second