LAB 4.1 Relational Operators and the if Statement

Similar documents
conditional statements

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Review. Relational Operators. The if Statement. CS 151 Review #4

INTRODUCTION TO COMPUTER SCIENCE - LAB

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Add Subtract Multiply Divide

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CS 141, Introduction to Computer Science Fall Midterm Exam

C++ Final Exam 2017/2018

University of Dublin

Write a program that displays all the even integers between 1 and 100, inclusive

Lecture #9 **go to Blackboard ** Review from Shackelford Reading If statements Loops. Using if statements and loops

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

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

Project Identity Shampoo-Conditioner Identification Device/ Medicine Reminder Week 2: 1/28/07-2/3/07 Karla Sittnick

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

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.

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

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

CSCE Practice Midterm. Data Types

CSCE 2004 Midterm Exam Spring 2017

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

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CSCE Practice Midterm. Data Types

Unit 7. 'while' Loops

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

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

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

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

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

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

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

a data type is Types

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

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

CPS 216, Problem Solving and Programming Techniques in C++ Saleh M. Alnaeli, Ph.D. Spring, Lab_Week2

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

COMP 11 Class 17 Outline

SELECTION. (Chapter 2)

CS242 COMPUTER PROGRAMMING

Arrays in C++ Instructor: Andy Abreu

Sample Code: OUTPUT Daily Highs & Lows

1. In C++, reserved words are the same as predefined identifiers. a. True

LAB 7.1 Working with One-Dimensional Arrays

Learning Objectives: General Description: DONE DONE Structure Chart

Introduction to Algorithms and Programming (COMP151)

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

Relational Operators EXAMPLE. C++ By

EE 109 Lab 8a Conversion Experience

Lecture 3 Tao Wang 1

Introduction to Programming

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

What is Object-Oriented Programming

Score score < score < score < 65 Score < 50

CPE Summer 2015 Exam I (150 pts) June 18, 2015

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Lab # 02. Basic Elements of C++ _ Part1

Homework #3 CS2255 Fall 2012

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

WARM UP LESSONS BARE BASICS

Chapter 4: Making Decisions

CSci 1113 Midterm 1. Name: Student ID:

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Chapter 4: Making Decisions

Decisions. Arizona State University 1

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Loops and Files. of do-while loop

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

REPETITION CONTROL STRUCTURE LOGO


Flow of Control: Programs can control the order in which their instructions are executed. Four types of flow:

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

CS 115 Midterm 2 Review Quiz

EE 355 Lab 4 - Party Like A Char Star

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Errata for C++ and Algorithmic Thinking for the Complete Beginner

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

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

Computer Programming. Decision Making (2) Loops

Transcription:

LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not initialized num2 = 5; // num2 has been initialized to 5 cout << "Please enter an integer" << endl; cin >> num1; cout << "num1 = " << num1 << " and num2 = " << num2 << endl; if (num1 = num2) cout << "The two values are the same. Hey, that s a coincidence!" << endl; if (num1!= num2) cout << "The two values are not the same" << endl; Exercise 1: Run the program several times using some input values that are not equal to the initialized value of num2, and a value that is equal to num2. Does the program do what you expect? If not, locate the error and fix it. Exercise 2: Modify the program so that the user inputs both values for num1 and num2 to be tested for equality. Make sure you have a prompt for each input. Test the program with pairs of values that are the same and that are different. Exercise 3: Modify the program so that when the numbers are the same it prints the following two lines: The values are the same. Hey that s a coincidence! Note: You must print each of the above two lines using separate cout statements. (Print out the program and the running result for Exercise 3, and hand them in with the rest of the lab. Note: Print out two copies of running result, one for two identical input values and another for two different input values.)

LAB 4.2 if/else and if/else if Statements // This program prints "You Pass" if a student's average is // 60 or higher and prints "You Fail" otherwise float average; // holds the grade average cout << "Input your average:" << endl; cin >> average; if (average > 60) cout << "You Pass" << endl; if (average < 60) cout << "You Fail" << endl; Exercise 1: Run the program three times using 80, 55 and 60 for the average. What happens when you input 60 as the average? Modify the first if statement so that the program will also print You Pass if the average equals 60. Exercise 2: Modify the program so that it uses an if/else statement rather than two if statements. Exercise 3: Modify the program from Exercise 2 to allow the following five categories: Invalid data (grade is either above 100 or below 0), A category (90 100), B category (80 89), You Pass category (60 79), You Fail category (0 59). (Print out the program and the running result for Exercise 3, and hand them in with the rest of the lab. Note: Please print out five copies of running result, one for each category.)

Lab 4.3 Logical Operators // This program illustrates the use of logical operators int year; float gpa; cout << "What year student are you?" << endl; cout << "Enter 1 (freshman), 2 (sophomore), 3 (junior), or 4 (senior)" << endl << endl; cin >> year; cout << "Now enter your GPA" << endl; cin >> gpa; // Add decision (if) statements below to display "It is time to graduate soon" // if the student is a senior and the gpa is greater than or equal to 2.0. // If the student is not a senior or the gpa is less than 2.0, // display "You need more schooling". Exercise 1: Complete the program and then run it. Test the program using different combinations of data. Print out the program and two running results: one for input data representing a senior student with a gpa 2.0, and the other for input data representing a student who is a non-senior or his/her gpa is less than 2.0.

LAB 4.4 The switch Statement // This program illustrates the use of the switch statement. char grade; cout << "" << endl; cin >> grade; switch( grade ) // This is where the switch statement begins case 'A': cout << "an A - excellent work!" << endl; case 'B': cout << "you got a B - good job" << endl; case 'C': cout << "earning a C is satisfactory" << endl; case 'D': cout << "while D is passing, there is a problem" << endl; case 'F': cout << "you failed - better luck next time" << endl default: cout << "You did not enter an A, B, C, D, or F" << endl; Exercise 1: Add an additional switch statement that allows for a Passing option for a grade of D or better. Use the sample run given below to model your output. Sample Run 1: A YOU PASSED! an A - excellent work! Sample Run 2: F You failed better luck next time

Sample Run 3: J You did not enter an A, B, C, D, or F Exercise 2: Rewrite the program using if / else if statements rather than a switch statement. Use a trailing else in your new version to correspond to the default case in the switch statement. (Print out the program and the running results for both Exercise 1 and Exercise 2. Print out three copies of running results for each exercise corresponding to the three sample runs above.)