The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.

Slides:



Advertisements
Similar presentations
CSE 251 Dr. Charles B. Owen Programming in C1 Functions.
Advertisements

Parameter Passing Mechanisms Reference Parameters.
File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and.
True or false A variable of type char can hold the value 301. ( F )
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
Functions Chapter 4. C++ An Introduction to Programming, 3rd ed. 2 Objectives Study software development using OCD Take a first look at building functions.
Software Engineering 1 (Chap. 1) Object-Centered Design.
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.
1 Programming and Problem Solving — Software Engineering (Read Chap. 2)
Programming and Problem Solving — Software Engineering (Read Chap. 2) 1.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
1 C++ Loop Statements Repetition Revisited. 2 Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Using Classes Classes and Function Members. Review We’ve seen that the iostream library provides the objects cin, cout and cerr: These objects were not.
CSC 221: Computer Programming I Fall 2001  top-down design  approach to problem solving, program design  focus on tasks, subtasks, …  reference parameters.
Vectors of Vectors Representing Objects with Two Dimensions.
Controlling Function Behavior Sequence, Selection and Repetition.
File I/O ifstreams and ofstreams Sections 11.1 &
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Parameter Passing Mechanisms Reference Parameters Read § §
C++ An Introduction to Computing, 3rd ed. 1 Repetition Chapter 7.
Fundamental Programming: Fundamental Programming Introduction to C++
1 CS161 Introduction to Computer Science Topic #3.
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Parameter Passing Mechanisms Reference Parameters § §
Arrays and Vectors Sequential (One-Dimensional) Containers Chapter 12 1.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 4 Selection: the if-else and switch-case instructions.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
Functions Chapter 4. C++ An Introduction to Programming, 3rd ed. 2 Objectives Study software development using OCD Take a first look at building functions.
C++ Loop Statements Repetition Revisited. Problem Using OCD, design and implement a function that, given a menu, its first valid choice, and its last.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
1 CS161 Introduction to Computer Science Topic #8.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Problem Session Working in pairs of two, solve the following problem...
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Calvin College Controlling Behavior The if, switch and for Statements.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
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
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Controlling Behavior The if and for Statements. Function Behavior The behavior of a function is determined by the statements within the function. Statements.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Review 1.
ifstreams and ofstreams
Engineering Problem Solving with C++, Etter/Ingber
Introduction to C++ October 2, 2017.
Writing Reuseable Formulas
solve the following problem...
One-Dimensional Array Introduction Lesson xx
Looping III (do … while statement)
Fundamental Programming
Multiple Files Revisited
Reading from and Writing to Files
ifstreams and ofstreams
Object-Centered Design
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

The switch Statement Selection Revisited

Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion (Celsius-to-Kelvin, Kelvin-to-Celsius, Fahrenheit-to-Celsius, Celsius-to-Fahrenheit, Fahrenheit-to-Kelvin, or Kelvin-to-Fahrenheit).

Preliminary Analysis One way to solve the problem is to provide a menu of choices, from which the user selects the operation they want to perform: Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit -->

Behavior: main() Our program should display its purpose and a menu of choices. It should then read the user’s choice. It should then prompt for and read the temperature to be converted. It should then compute the converted temperature by applying to the input temperature the particular conversion specified by the user’s choice. It should then display the converted temperature.

Objects: main() Description Type Kind Name menu string constant MENU choice char variable choice input temp. double variable tempIn output temp. double variable tempOut purpose string constant --

Operations: main() Description Predefined? Library? Name display strings yes iostream << read a double yes iostream >> convert a temp. no display double yes iostream <<

Algorithm: main() 0. Display via cout the purpose of the program. 1. Loop a. Display MENU via cout. b. Read choice from cin. c. If choice is ‘q’, terminate repetition. d. Prompt for and read tempIn from cin. e. Compute tempOut by converting tempIn using choice. f. Display tempOut. End loop. End loop.

Organization We’ll need: –a main function –six conversion functions –a function to apply the right conversion The conversion functions seem likely to be reuseable someday, so we’ll create a library named Heat in which to store them.

Behavior: FahrenheitToCelsius() Our function should receive from its caller the temperature to be converted. It should return the result of subtracting 32.0 from that temperature, and then dividing the difference by 1.8.

Objects: FahrenheitToCelsius() Description Type Kind Name the converted double variable -- temperature the original double variable originalTemp temperature We can use this as a pattern for each of the temperature conversion functions -- they differ only in the particular formula they use.

Coding: Conversion Prototypes /* Heat.h *... */ double FahrenheitToCelsius(double originalTemp); double CelsiusToFahrenheit(double originalTemp); double FahrenheitToKelvin(double originalTemp); double KelvinToFahrenheit(double originalTemp); double CelsiusToKelvin(double originalTemp); double KelvinToCelsius(double originalTemp);

Operations: FahrenheitToCelsius() Description Predefined? Library? Name receive a double yes built-in subtract doubles yes built-in - divide doubles yes built-in / return a double yes built-in return

Algorithm: FahrenheitToCelsius() 0. Receive originalTemp. 1. Return (originalTemp ) / 1.8. We can use this algorithm as a pattern for each of the temperature conversion functions -- each differs only in the particular formula used.

Coding: Conversion Definitions /* Heat.cpp *... */ double FahrenheitToCelsius(double originalTemp) { return (originalTemp ) / 1.8; } double CelsiusToFahrenheit(double originalTemp) { return originalTemp * ; } double FahrenheitToKelvin(double originalTemp) { return (originalTemp ) / ; } //... other conversion function definitions

Organization (ii) Once our library is completed, we can design and implement a function to perform the particular conversion specified by the user’s menu choice. We will call this function Convert(). Since it is less likely to be reuseable than our conversion functions, we will store its prototype and definition in the same file as our main function.

Behavior: Convert() Our function should receive from its caller the temperature to be converted and the user’s menu choice. Based on the menu choice, our function should return the result of applying the appropriate temperature conversion function to the received temperature.

Objects: Convert() Description Type Kind Name the converted double variable -- temperature the temp. double variable origTemp being converted Our prototype is thus: double Convert(double origTemp, char choice); a menu choice char variable choice

Operations: Convert() Description Defined? Library? Name return a double yes built-in return convert F-to-C yes Heat... convert C-to-F yes Heat... convert F-to-K yes Heat... convert K-to-F yes Heat... convert C-to-K yes Heat... convert K-to-C yes Heat... perform selection yes built-in switch based on a char

Algorithm: Convert() 0. Receive origTemp, choice. 1. If choice is ‘a’: return FahrenheitToCelsius(origTemp); Else if choice is ‘b’: return CelsiusToFahrenheit(origTemp); Else if choice is ‘b’: return CelsiusToFahrenheit(origTemp); Else if choice is ‘c’: return FahrenheitToKelvin(origTemp); Else if choice is ‘c’: return FahrenheitToKelvin(origTemp); Else if choice is ‘d’: return KelvinToFahrenheit(origTemp); Else if choice is ‘d’: return KelvinToFahrenheit(origTemp); Else if choice is ‘e’: return CelsiusToKelvin(origTemp); Else if choice is ‘e’: return CelsiusToKelvin(origTemp); Else if choice is ‘f’: return KelvinToCelsius(origTemp); Else if choice is ‘f’: return KelvinToCelsius(origTemp); Else display error message and return 0.0. Else display error message and return 0.0. We could implement this algorithm with a multi-branch if, but let’s learn something new.

Coding: Convert() /* main.cpp *... */ // rest of main.cpp double Convert(double origTemp, char choice) { switch (choice) { case ‘a’: return FahrenheitToCelsius(origTemp); case ‘b’: return CelsiusToFahrenheit(origTemp); case ‘c’: return FahrenheitToKelvin(origTemp); case ‘d’: return KelvinToFahrenheit(origTemp); case ‘e’: return CelsiusToKelvin(origTemp); case ‘f’: return KelvinToCelsius(origTemp); default : cerr << “\nUnsupported menu choice: “ << choice << “ in Convert()” << endl; return 0.0; }

Discussion double Convert(double origTemp, char choice) { switch (choice) { case ‘a’: return FahrenheitToCelsius(origTemp); case ‘b’: return CelsiusToFahrenheit(origTemp); case ‘c’: return FahrenheitToKelvin(origTemp); case ‘d’: return KelvinToFahrenheit(origTemp); case ‘e’: return CelsiusToKelvin(origTemp); case ‘f’: return KelvinToCelsius(origTemp); default : cerr << “\nUnsupported menu choice: “ << choice << “ in Convert()” << endl; return 0.0; } The switch statement provides an alternative means of doing multi-branch selection.

Organization (iii) Once our library and conversion function have been designed and completed, we can proceed to implement the algorithm for our main function. The primary control structure is a forever loop that continues repetition until the user enters menu choice ‘q’...

Coding: main() #include // cin, cout, >,... using namespace std; #include “Heat.h” // FahrenheitToCelsius(),... double Convert(double originalTemp, char menuChoice); int main() { const MENU = “\nPlease enter:\n“ “ a - to convert Fahrenheit to Celsius\n” “ b - to convert Celsius to Fahrenheit\n” “ c - to convert Fahrenheit to Kelvin\n” “ d - to convert Kelvin to Fahrenheit\n” “ e - to convert Celsius to Kelvin\n” “ f - to convert Kelvin to Celsius\n” “ q - to quit\n” “--> “; cout << “\nThis program converts temperatures.\n”;

Coding: main() char choice; double tempIn, tempOut; for (;;) { cout << MENU; cin >> choice; if (choice == ‘q’ || choice == ‘Q’) break; cout << “\nEnter the temperature to be converted: “; cin >> tempIn; tempOut = Convert(tempIn, choice); cout << “\nThe converted temperature is “ << tempOut << endl; }

Testing This program converts temperatures. Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> a Enter the temperature to be converted: 212 The converted temperature is 100

Testing Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> b Enter the temperature to be converted: 0 The converted temperature is 32

Testing Please enter: a - to convert Fahrenheit to Celsius b - to convert Celsius to Fahrenheit c - to convert Fahrenheit to Kelvin d - to convert Kelvin to Fahrenheit e - to convert Celsius to Kelvin f - to convert Kelvin to Celsius q - to quit\n --> c Enter the temperature to be converted: 32 The converted temperature is

Summary To perform multibranch selection, C++ provides an alternative statement called the switch statement. We’ll learn more of the details of the if and switch statements next time, including when you should use which statement.