Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

Similar documents
Local and Global Variables

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Lab Instructor : Jean Lai

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Programming Language. Functions. Eng. Anis Nazer First Semester

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

A Freshman C++ Programming Course

do { statements } while (condition);

WARM UP LESSONS BARE BASICS

University of Dublin

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Unit 7. 'while' Loops

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

C++ Quick Reference. switch Statements

Chapter 3 Problem Solving and the Computer

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

EP578 Computing for Physicists

C++ basics Getting started with, and Data Types.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Practice test for midterm 1

Introduction to Programming using C++

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Computer Science II Lecture 1 Introduction and Background

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Fundamentals of Programming CS-110. Lecture 2

A First Program - Greeting.cpp

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

VARIABLES & ASSIGNMENTS

Chapter 2: Overview of C++

REPETITION CONTROL STRUCTURE LOGO

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lecture 3 Tao Wang 1

BITG 1233: Introduction to C++

EP241 Computing Programming

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Functions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.

Ch 6. Functions. Example: function calls function

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

CS101 PLEDGED SPRING 2001

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

CS 007A Midterm 1 Practice Chapters 1 5

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

The following is a typical execution run of this program:

Introduction to Algorithms and Programming (COMP151)

Chapter 4 - Notes Control Structures I (Selection)

Outline. First Quiz Results. Exercise Five Goals. Question Three. Questions One and Two. Exercise five if statements February 28, 2006

Functions and Recursion

Expressions, Input, Output and Data Type Conversions

Engineering Problem Solving with C++, Etter/Ingber

Increment and the While. Class 15

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

REVIEW. The C++ Programming Language. CS 151 Review #2

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C++ Programming Lecture 1 Software Engineering Group

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

C++ Programming: From Problem Analysis to Program Design, Third Edition

A Freshman C++ Programming Course

Introduction to Programming EC-105. Lecture 2

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Introduction to C ++

Unit 6. Thinking with Functions

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE Instructor: Final Exam Fall 2014

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Transcription:

Lecture 6

Other operators Some times a simple comparison is not enough to determine if our criteria has been met. For example: (and operation) If a person wants to login to bank account, the user name and the password both need to match. Another example: (or operation) Find all the person with age greater than 60 and less than 10. Yet another example: (negation operation) A person is not on the authorized list of users,abort.

And operator The and operator in C++ is denoted with the double & ampersand sign. && Example of usage: if(age > 0 && age < 120)

Or operator The or operator in C++ is denoted with the double pipe symbol. Pipe key is usually located above the enter key Example of usage: if(age < 0 age> 120) // that is not a valid age

Not operator The not operator in C++is denoted with the! exclamation symbol.! Same symbol as the! in!= Example of usage: if(!(number %2==0) ) // do some thing for odd numbers

Boolean logic Symbol Operation C++ syntax Action && AND expr1 && expr2 Evaluate expr1. If it s true, evaluate expr2. Then, if both are true, return true; otherwise, return false. OR expr1 expr2 Evaluate expr1. If false, evaluate expr2. Then return true unless both are false.! NOT! expr1 Evaluate expr1. Reverse the true/false value.

Boolean Logic - Order Logical operators have lower precedence than the relational operators (<, >, >=, <=,!, and ==), which in turn have lower precedence than arithmetic operators (such as + and *). if (x + 2 > y && a == b) cout << "The data passes the test"; // is equal to if ((x + 2 > y) && (a == b) cout << "The data passes the test ; // is equal to if (x + 2 > y && a == b) cout << "The data passes the test";

Logical And (&&): expr 1 -> expr 2

Logical And (&&) EX1 #include <iostream> using namespace std; int main() { int i=1,n=10; if (1==0 && 1/0==1){ cout << Hi"; cout <<" Bye!"; return 0; Result

Logical And (&&) EX2 #include <iostream> using namespace std; int main() { int i=1,n=10; if (1/0==1 && 1==0 ){ cout << Hi"; cout <<" Bye!"; return 0; Result

Ex.3 Testing a Person s Age The program here determines whether a number is in a particular range in this case, the range of teen numbers, 13 through 19. Get a subject age and show whether subject is teenager or not. the range of teen numbers, 13 through 19 means 13<=age and age<=19 (inclusive) Equivalently, 12<age and age <20

Ex.3 Testing a Person s Age #include <iostream> using namespace std; int main() { int age; cout << "Enter an age and press ENTER: "; cin >> age; if (age > 12 && age < 20) cout << "Subject is a teenager." << endl; else cout << "Subject is not a teenager." << endl; return 0;

Exercise 1 Exercise 1: Ask user to input one number that range between 0 to 100, if the number is not in this range keep asking user for a valid number. After get the valid number then convert from Celsius to Fahrenheit by using the user input number. Formula: F=9/5 C + 32

Exercise 1 solution #include <iostream> using namespace std; main(){ int c; cout<<"please enter a number between 0 and 100:"; cin>>c; while(c<0 c>100){ cout<<"please re enter a valid number:"; cin>>c; float f; f= 9.0/5*c+32; cout<<c<<" degree celcius is "<<f<<" degree Fahrenheit."<<endl; return 0;

Exercise 2 Exercise 2: Ask user enter an integer. 1, check is the number divisible by 3 not 5, if yes, divide by 3, and print result. 2, check is the number divisible by 5 not 3, if yes, divide by 5, and print result. 3, check is the number divisible by 3 and 5, if yes, divide by 3 and 5, and print result.

Exercise 2 solution #include <iostream> using namespace std; main(){ int n; cout<<"please enter an integer:"; cin>>n; if(n%3==0 && n%5!=0) cout<<n/3<<endl; if(n%3!=0 && n%5==0) cout<<n/5<<endl; if(n%3==0 && n%5==0) cout<<n/15<<endl; return 0;

Conversions double x = sqrt(2.0); // Assign square root of 2 to x int n = sqrt(2.0); // Place the value 1 into n, // after truncating 1.41421. double x = sqrt(2); // Ok: int converted to double. Do not forget to use sqrt we should include #include <cmath>

Prime-Number Test Prime number? a natural number greater than 1 that has no positive divisors other than 1 and itself. Ex 2,3,5,7,11,11, How many prime numbers do we have? There are infinitely many primes.

Prime-Number Test Goal: Determine whether a number is prime. Main idea: Check whether a number is divisible by another. Ex. Determine whether 7 is prime. 7 % 2= 1 0 (Not divisible by 2) 7 % 3= 1 0 (Not divisible by 3) 7 % 4= 3 0 (Not divisible by 4) 7 % 5= 2 0 (Not divisible by 5) 7 % 6= 1 0 (Not divisible by 6) Divide by 2,3,,n 1

Exercise 3 Exercise 3: Ask user input an integer, and test if it is a prime number. (prime test) prime test: a natural number greater than 1 that has no positive divisors other than 1 and itself.

Prime Test part Assume n is a given number. bool is_prime=true; i=2; while( i < n){ if (n%i==0) is_prime=false; i++; Explanation Using is_prime variable, we save the result of prime test. 1<i<n n is divisible by i, means n is not prime.

Exercise 3 solution #include <iostream> using namespace std; main(){ int n; cout<<"please enter an integer:"; cin>>n; int count=2; while(count<n){ if(n%count==0){ cout<<n<<" is divisible by "<<count<<", so it is not prime."<<endl; return 0; count++; cout<<n<<" is a prime number."<<endl; return 0;

Optimize prime test. Key Idea. Assume n is divisible by i. Then n is also divisible by n/i. If i > n/i, we already know n is not a prime when we divide by n/i. So we can assume i n/i i sqrt(n)

Prime Test part again Assume n is a given number. bool is_prime=true; i=2; while(i <= sqrt(n)) { if (n%i==0) is_prime=false; i++; Explanation Using is_prime variable, we save the result of prime test. 2 i sqrt(n) n is divisible by i, means n is not prime.