C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.

Slides:



Advertisements
Similar presentations
Revision.
Advertisements

Programming Functions: Passing Parameters by Reference.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
Programming is instructing a computer to perform a task for you with the help of a programming language.
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.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Objective: Students will be able to: Declare and use variables Input integers.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
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!"
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
ISLAMIC UNIVERSITY OF GAZA FACULTY OF ENGINEERING ISLAMIC UNIVERSITY OF GAZA FACULTY OF ENGINEERING Computer Programming Lab 1 Programming Basics By Eng.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
CS 240 Computer Programming 1
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
The Ohio State University
Variables Mr. Crone.
LESSON 2 Basic of C++.
CMPT 201 if-else statement
Computing Fundamentals
Quiz Next Monday.
Chapter 2 Elementary Programming
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CSC1201: Programming Language 2
Screen output // Definition and use of variables
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS1201: Programming Language 2
Statements and flow control
do/while Selection Structure
CSC1201: Programming Language 2
Reading from and Writing to Files
Control Structures Selection
Introduction to Algorithms and Programming COMP151
Reading from and Writing to Files Part 2
Introduction to Algorithms and Programming COMP151
Reading from and Writing to Files
Odds and Ends.
Presentation transcript:

C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010

Quiz: Write a program that asks the user to enter an uppercase letter and prints out the lowercase of that letter.

Quiz Solution: #include Using namespace std; int main() { cout<<"enter an uppercase letter: "; char ch; cin>>ch; cout<<"the lowercase letter of "<<ch<<" is : "; cout<< (char)(ch+'a'-'A')<<endl; system("PAUSE"); return 0; }

Exercise #1: Write a program that takes a character input from the user then: 1.If the input character in uppercase print it in lowercase. 2.If the input character in lowercase print it in uppercase. 3.The output should be as follows:

Solution: #include using namespace std; int main() { cout << "Enter a letter: "; char ch; cin>>ch; if(ch>='a'&&ch<='z') {cout<<"you entered "<<ch<<". It is a lowercase letter\n"; cout<<"the uppercase equivalent is: "<<(char)(ch-('a'-'A'))<<endl;; } else if(ch>='A'&&ch<='Z') {cout<<"you entered "<<ch<<". It is an uppercase letter\n"; cout<<"the lowercase equivalent is: "<<(char)(ch+('a'-'A'))<<endl; } else cout<<"you entered "<<ch<<". It is not a letter\n"; system("pause"); return 0; }

Exercise #2: Write a program that takes 3 integers as inputs from the user then prints out the largest. for solution see lecture notes

Exercise #3: Write a program that takes 3 integers as inputs from the user then prints out the three numbers in descending order.

Solution #include using namespace std; int main() { cout<<"enter three integers: "; int i, j, k ; int num1, num2, num3; cin>>i>>j>>k; if (i > j) if (i > k) { num1=i; if (k>j) {num2=k; num3=j;} else { num2=j; num3=k; } } else {num1=k; num2=i; num3=j;} else if(j>k) {num1=j; if (i>k) {num2=i; num3=k; } else {num2=k; num3=i; } } else {num1=k; num2=j; num3=i;} cout<<"the numbers are: "<< num1<<" "<<num2<<" "<<num3<<" \n"; system("PAUSE"); return 0; }

Exercise #4: A. In the code fragment below, the programmer has almost certainly made an error in the first line of the conditional statement. 1. What is the output of this code fragment as it is written? 2. How can it be corrected to do what is the programmer surely intended? int n = 5; if (n = 0) cout << "n is zero" << ".\n"; else {cout << "n is not zero" << ".\n"; cout << "The square of n is " << n * n << ".\n";} B. What is the output when the following code fragment is executed? int n, k = 5; n = (100 % k ? k + 1 : k - 1); cout << "n = " << n << " k = " << k << endl;