Input a number #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; return 0; }

Slides:



Advertisements
Similar presentations
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Advertisements

CMPUT 101 Lab # 5 October 22, :00 – 17:00.
If Statements & Relational Operators Programming.
Computer Science 1620 Loops.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Chapter 4 Summation.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
What is the out put #include using namespace std; void main() { int i; for(i=1;i
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Selection Structures (if & switch statements) (CS1123)
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CONTROLLING PROGRAM FLOW
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
First steps Jordi Cortadella Department of Computer Science.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Chapter 4 Selection: the if-else and switch-case instructions.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Bill Tucker Austin Community College COSC 1315
for Repetition Structures
Chapter 2 Assignment and Interactive Input
Decisions Given hours worked and pay rate, calculate total pay
Arrays Part-1 Armen Keshishian.
Quiz Next Monday.
Decisions Given hours worked and pay rate, calculate total pay
Chapter 2 Elementary Programming
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
CS150 Introduction to Computer Science 1
Counting Loops.
Starting Out with C++: From Control Structures through Objects
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
If Statements.
CS1201: Programming Language 2
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
CS150 Introduction to Computer Science 1
Fundamental Programming
Introduction to Functions
Presentation transcript:

Input a number #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; return 0; }

Print positive if greater than 0 #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num >= 0) cout << "positive" << endl; return 0; } Enter 5 => positive Enter -5 => no output

Add ; #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num >= 0); cout << "positive" << endl; return 0; }

Add ; #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num >= 0); cout << "positive" << endl; return 0; } Enter 5 => positive Enter -5 => positive Why?

Add ; #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num >= 0) ; cout << "positive" << endl; return 0; } Enter 5 => positive Enter -5 => positive Why? ; is null statement

Print if negative also

#include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num >= 0) cout << "positive" << endl; else cout << "negative" << endl; return 0; }

Print if negative also #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num >= 0) cout << "positive" << endl; else cout << "negative" << endl; return 0; } Enter 5 => positive Enter -5 => negative

Input a number, print if even or odd #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; return 0; }

Input a number, print if even or odd #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (xxxx) cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

Input a number, print if even or odd #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num % 2 == 0) cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

What is wrong with this solution? #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num % 2 = 0) cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

Watch = (assignment) vs == (test for equality) #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num % 2 = 0) should be num %2 == 0 cout >> "even" << endl; else cout <<"odd" << endl; return 0; }

Selection exercises 1.Input an age and print “over the hill” if over Input an age and print whether the individual is legal to drink or underage 3. Change the above problem to use a constant 4. Input a temperature and print whether it is freezing or above 5. Input 2 integer values, print out the largest.

constant 1.Declare a variable type name; 2. int LEGAL_AGE; //use caps for constants 3.const int LEGAL_AGE; //make it constant 4.const int LEGAL_AGE = 21; //give it a value

Selection exercises 1.Input an age and print “over the hill” if over 40 #include using namespace std; int main() { int age; cout << "Enter an age" << endl; cin >> age; if (age >= 40) cout << "Over the Hill" << endl; return 0; }

2. Input an age and print whether the individual is legal to drink or underage #include using namespace std; int main() { int age; cout << "Enter an age" << endl; cin >> age; if (age >= 21) cout << "Legal to drink" << endl; else cout << "Underage" << endl; return 0; }

3. Change the above problem to use a constant #include using namespace std; int main() { int age; const int LEGAL_AGE = 21; cout << "Enter an age" << endl; cin >> age; if (age >= LEGAL_AGE) cout << "Legal to drink" << endl; else cout << "Underage" << endl; return 0; }

//4. Input a temperature and print whether it is freezing or above #include using namespace std; int main() { float temp; const float FREEZING = 32; cout << "Enter a temperature" << endl; cin >> temp; if (temp <= FREEZING) cout << "Freezing" << endl; else cout << "Above Freezing" << endl; return 0; }

//5. Input 2 integer values, print out the largest. #include using namespace std; int main() { int num1; int num2; cout << "Enter two numbers" << endl; cin >> num1 >> num2; if (num1 > num2) cout << num1 << " is largest" << endl; else cout << num2 << " is largest" << endl; return 0; }

//5. Input 2 integer values, print out the largest. #include using namespace std; int main() { int num1; int num2; cout << "Enter two numbers" << endl; cin >> num1 >> num2; if (num1 > num2) cout << num1 << " is largest" << endl; else cout << num2 << " is largest" << endl; return 0; } Enter 5 and 3 => 5 is largest Enter 3 and 5 => 5 is largest Enter 3 and 3 => 3 is largest

What if they are equal?

//5. Input 2 integer values, print out the largest. #include using namespace std; int main() { int num1; int num2; cout << "Enter two numbers" << endl; cin >> num1 >> num2; if (num1 > num2) cout << num1 << " is largest" << endl; else if (num2 < num1) cout << num2 << " is largest" << endl; else cout << "same" << endl; return 0; }

This is better, remove whitespace //5. Input 2 integer values, print out the largest. #include using namespace std; int main() { int num1; int num2; cout << "Enter two numbers" << endl; cin >> num1 >> num2; if (num1 > num2) cout << num1 << " is largest" << endl; else if (num2 < num1) cout << num2 << " is largest" << endl; else cout << "same" << endl; return 0; }

//input a number, print if positive, negative, or zero #include using namespace std; int main() { int num; cout << "Enter a number" << endl; cin >> num; if (num > 0) cout << "positive" << endl; else if (num < 0) cout << "negative" << endl; else cout << "zero" << endl; return 0; }

Better if (num > 0) cout << "positive"; else if (num < 0) cout << "negative"; else cout << "zero"; Less efficient, why? if (num > 0) cout << "positive"; if (num < 0) cout << "negative"; if (num == 0) cout << "zero";

Calculate pay What if your work overtime? Time and a half for overtime Overtime is over 40 hours Double overtime is over 60 hours

Hint Calculate regular pay first

//calculate regular pay first #include using namespace std; int main() { float payRate; float hrsWorked; float totalPay; cout << "enter hours worked" << endl; cin >> hrsWorked; cout << "enter pay rate" << endl; cin >>payRate; if (hrsWorked <= 40) { totalPay = hrsWorked * payRate; cout << "totalpay is " << totalPay << endl; } return 0; }

Next hint:Add variables regPay, otPay Next calculation is hours less than 60, regular overtime

float payRate; float hrsWorked; float totalPay; float regPay; float otPay; cout << "enter hours worked" << endl; cin >> hrsWorked; cout << "enter pay rate" << endl; cin >>payRate; if (hrsWorked <= 40) { totalPay = hrsWorked * payRate; cout << "totalpay is " << totalPay << endl; } else if (hrsWorked <= 60) { regPay = ?? otPay = ?? totPay = ?? cout << ??? }

float payRate; float hrsWorked; float totalPay; float regPay; float otPay; cout << "enter hours worked" << endl; cin >> hrsWorked; cout << "enter pay rate" << endl; cin >>payRate; if (hrsWorked <= 40) { totalPay = hrsWorked * payRate; cout << "totalpay is " << totalPay << endl; } else if (hrsWorked <= 60) { regPay = 40 * payRate; // regular pay otPay = (hrsWorked – 40) * payRate; // overtime pay totPay = ?? cout << ??? }

float payRate; float hrsWorked; float totalPay; float regPay; float otPay; cout << "enter hours worked" << endl; cin >> hrsWorked; cout << "enter pay rate" << endl; cin >>payRate; if (hrsWorked <= 40) { totalPay = hrsWorked * payRate; cout << "totalpay is " << totalPay << endl; } else if (hrsWorked <= 60) { regPay = 40 * payRate; // regular pay otPay = (hrsWorked – 40) * payRate; // overtime pay totPay = regPay + otPay; cout << ??? }