Introduction to Algorithms and Programming COMP151

Slides:



Advertisements
Similar presentations
Appendix Lab Manual for Programming Skills 1. Symbols used in Writing Programs { opening curly bracket } closing curly bracket # hash sign or number sign.
Advertisements

Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Programming is instructing a computer to perform a task for you with the help of a programming language.
1 2/6/1435 h Wednesday Lecture 8. 2 Q: What is a %(modulus) operator ? 1. % (modulus) operator computes the remainder resulting from integer division.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Perimeter = 2*(length+width)
Previously Repetition Structures While, Do-While, For.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Lecture 4 1 Loops - Exercises. Lecture 4 Exercise 1 2 What is the value of x when the do... while loop completes? int x = 10, k; do{ k = ++x; x -= 2;
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Introduction to Computer Programming
What Actions Do We Have Part 1
#define #include<iostream> using namespace std; #define GO
Control Statement tsenghy.
LESSON 4 Decision Control Structure
Decisions Chapter 4.
C++ Programming: CS150 For.
Controlling execution - iteration
Engineering Problem Solving with C++, Etter/Ingber
CSC113: Computer Programming (Theory = 03, Lab = 01)
Introduction to C++ October 2, 2017.
Programming Fundamentals
Intro to Programming Week # 4 Switch Statement Lecture # 7
Repetition Structures (Loops)
Chapter 4 Loops Case Studies
Lab 1 Introduction to C++.
Random Number Generation
Compound Assignment Operators in C++
Programming Funamental slides
Introduction to Algorithms and Programming COMP151
Programming -2 برمجة -2 المحاضرة-7 Lecture-7.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
Structure As Function Arguments
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Stacks Data structure Elements added, removed from one end only
CS1201: Programming Language 2
CHAPTER 2 Arrays and Vectors.
Name: Muhammad Hassan VU-ID: BC Version: Office 2007
What Actions Do We Have Part 1
Reading from and Writing to Files
CHAPTER 2 Arrays and Vectors.
Introduction to Algorithms and Programming
Using string type variables
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
TOPIC: FUNCTION OVERLOADING
Introduction to Algorithms and Programming
Control Structures contd… Selection
Introduction to Algorithms and Programming COMP151
Introduction to Algorithms and Programming COMP151
CSE Module 1 A Programming Primer
Reading from and Writing to Files Part 2
Introduction to Algorithms and Programming COMP151
Reading from and Writing to Files
CSE Module 1 A Programming Primer
Presentation transcript:

Introduction to Algorithms and Programming COMP151 LAB 6 Switch case Introduction to Algorithms and Programming COMP151

Review Questions

Q1: A car with a 20 litters of petrol tank averages 21.5 Miles Per Litter (MPL) when driven in town and 26.8 Miles Per Litter (MPL) when driven on the high way. Write an algorithm and draw the flowchart that calculates and display the distance the car can travel on one tank of petrol when driven in town and when driven on the high way. Hint: Distance = Numbers of litters * Average Miles Per Litter

#include <iostream> using namespace std; int main() { int petrol=20; float town_average_miles= 21.5; float highway_average_miles= 26.8; float town_ditance, highway_distance; town_ditance= petrol * town_average_miles; highway_distance= petrol * highway_average_miles; cout<<" Distance in town = "<<town_ditance<<endl; cout<<" Distance in High way = "<< highway_distance<<endl; return (0); }

Q2: Print the following pattern on the screen: * * * * * * * * * * * * * * *

#include <iostream> using namespace std; int main() { cout<<"\* * * * *\n"; cout<<"\ * * * *\n"; cout<<"\ * * *\n"; cout<<"\ * *\n"; cout<<"\ *\n"; return (0); }

Q3: Write a program to display the people category based on their age as given below Age Category Above 60 Really old >=45 and <=60 Old >=30 and <45 Middle age >=20 and <30 Adult >12 and <20 Teens Otherwise Children

// (1) Switch case // (2) switch(ch) { case 1: res=a+b; cout<<"Result of Addition is"<<res<<endl; break; case 2: res=a-b; cout<<"Result of Subtraction is"<<res<<endl; case 3: res=a*b; cout<<"Result of Multiplication is"<<res<<endl; case 4: res=a/b; cout<<"Result of Division is"<<res<<endl; default: cout<<"Enter the choice between 1 and 4"<<endl; } return(0); // (1) #include<iostream> using namespace std; int main() { float a,b,res; int ch; cout<<"Enter the Value for a"<<endl; cin>>a; cout<<"Enter the Value for b"<<endl; cin>>b; cout<<"1.Addition"<<endl; cout<<"2.Subtraction"<<endl; cout<<"3.Multiplication"<<endl; cout<<"4.Division"<<endl; cout<<"Enter your choice"<<endl; cin>>ch;

Switch case // (2) case 5: cout<<"Thursday"<<endl; break; // (1) #include<iostream> using namespace std; int main() { int day; cout<<"Enter the order of day"<<endl; cin>>day; switch(day) case 1: cout<<"Sunday"<<endl; break; case 2: cout<<"Monday"<<endl; case 3: cout<<"Tuesday"<<endl; case 4: cout<<"Wednesday"<<endl; // (2) case 5: cout<<"Thursday"<<endl; break; case 6: cout<<"Friday"<<endl; case 7: cout<<"Saturday"<<endl; default: cout<<"Enter the order between 1 and 7"<<endl; } return(0);