Required Functions for Program 3 int readUntilValidBaseRead( ); int readNumbersReturningValue( int base ); int decimalValueOf( char chDigit ); bool isValid(

Slides:



Advertisements
Similar presentations
The Binary Numbering Systems
Advertisements

18-May-15 Numbers. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Integer Types. Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit can distinguish.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CS 1400 Feb 2007 Pick ups from chapters 2 and 3. Example; A person 15 years of age may acquire a driver’s lic. with permission from parents. A person.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4 Making Decisions
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
1 Techniques of Programming CSCI 131 Lecture 29 Search.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
CIS Computer Programming Logic
Input & Output: Console
Information and Programs. Foundations of Computing Information –Binary numbers –Integers and Floating Point –Booleans (True, False) –Characters –Variables.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Examples of comparing strings. “ABC” = “ABC”? yes “ABC” = “ ABC”? No! note the space up front “ABC” = “abc” ? No! Totally different letters “ABC” = “ABCD”?
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Characters and Strings
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Basic concepts of C++ Presented by Prof. Satyajit De
Numbers Mar 27, 2013.
BINARY CODE.
Chapter 3: Expressions and Interactivity.
Chapter 2 part #3 C++ Input / Output
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
Chapter 3: Expressions and Interactivity
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chapter 2 part #3 C++ Input / Output
Chapter 1 c++ structure C++ Input / Output
Programming Fundamental-1
Presentation transcript:

Required Functions for Program 3 int readUntilValidBaseRead( ); int readNumbersReturningValue( int base ); int decimalValueOf( char chDigit ); bool isValid( char chDigit, int base ); MUST NOT Change the Prototypes! You can have other functions. 1

Required Functions for Program 3 // // This function reads bases until a valid base is read or eof occurs. // If an invalid base is read, an error message is displayed and the // rest of the line is ignored and another attempt to read a base value // will be attempted. // -1 is returned if eof occurs otherwise a valid base value is // returned. // int readUntilValidBaseRead( ) Is base a char, int, float, or string? int! Input for Test Run 2: 1 hello 10 this is bad too -1 as well as this 2

Required Functions for Program 3 // // This function reads in a sequence of characters that represent // a number in the given base. A valid sequence is given in a // "backwards" format such that the rightmost digit is given first, // the second to the rightmost digit is next, etc. // This function returns the value of this sequence of characters if // it is a valid sequence. If it is not valid it returns -1. // params: ( ) // int readNumbersReturningValue( int base ) Do we know the base now? Yes! And it’s a valid base! Sample Input:

Pseudocode for main() Set totalSum to zero Read until a valid base is read or end of file while not at end of file write "For the given base ", base Assign to numberValue the translated number from the input if numberValue is not valid Write " the number is NOT valid!" else Accumulate numberValue into totalSum Write numberValue appropriately labelled Read until another valid base is read or end of file Write totalSum appropriately labelled 4

Calling Functions from main() Set totalSum to zero Call function readUntilValidBaseRead while not at end of file write "For the given base ", base Call function readNumbersReturningValue to get numberValue if numberValue is not valid Write " the number is NOT valid!" else Accumulate numberValue into totalSum Write numberValue appropriately labelled Call readUntilValidBaseRead to get next base Write totalSum appropriately labelled 5

6 Functions // // This function reads in a sequence of characters that represent // a number in the given base. A valid sequence is given in a // "backwards" format such that the rightmost digit is given first, // the second to the rightmost digit is next, etc. // This function returns the value of this sequence of characters if // it is a valid sequence. If it is not valid it returns -1. // params: ( ) // int readNumbersReturningValue( int base ) How to read ‘\n’? cin.get(ch) How to read the first digit? cin >> ch; Input for Run 1:

7 Functions int readNumbersReturningValue( int base ) { char ch; cin >> ch; while (ch != ‘\n’) { // process digit cin.get(ch) } } How to check range?

8 int readNumbersReturningValue( int base ) { char ch; bool valid; int total; cin >> ch; // call isValid() while (ch != ‘\n’ && valid) { // process digit // Call function decimalValueOf cin.get(ch); // call isValid() } if (! valid) { cin.ignore( MAX_LINE, '\n' ); // MAX_LINE: 256 return -1; } else return total; } How to check range? Call function! bool isValid( char chDigit, int base )

Required Functions for Program 3 // // This function returns true if chDigit is a valid digit in the given // base, it returns false otherwise. // params: (in, in) // bool isValid( char chDigit, int base ) Base 2: Valid digits: 0, 1 Base 5: Valid digits: 0, 1, 2, 3, 4 Any base: Valid digits: 0, 1,... (base – 1) 9

10 ASCII Code Table All digits 0 through 9 are together ABCDE 7FGHIJKLMNO 8PQRSTUVWXY 9Zabc 10def

11 ASCII Code Char ASCII Code ‘0’: 48 ‘5’: ? Any base: Valid digits: 0, 1,... (base – 1) >= ‘0’ and < (‘0’ + base) 0, 1 and -1 are not magic numbers! 2 and 9 are!

Required Functions for Program 3 // // This function returns the numeric value of the character digit that // is stored in chDigit. // params: (in) // int decimalValueOf( char chDigit ) Assume chDigit is a digit Base 5: chDigit ‘3’ to 3 (int) chDigit – ‘0’ 12

13 int readNumbersReturningValue( int base ) { char ch; bool valid; int total; cin >> ch; // call isValid() while (ch != ‘\n’ && valid) { // process digit // Call function decimalValueOf cin.get(ch); // call isValid() } if (! valid) { cin.ignore( MAX_LINE, '\n' ); // MAX_LINE: 256 return -1; } else return total; }

14 Binary Numbers Decimal Number: Base 2 Two digits: 0, 1

15 Binary Numbers (may not be a byte) = = = 13 Base 2 Two digits: 0, 1

16 Decimal Numbers Base 10 How many digits? Ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, = 1 * * * 10 0

17 Other Bases Base 5 How many digits? Five digits: 0, 1, 2, 3, in base 5 1 * * * 5 0 = = 48 Base = 1 * * * 10 0

18 Backwards Why? Input one char at a time! =

19 Backwards What’s the value of the following number? 143 Base 5 1 * * * 5 2 = = 96 Base 10 1 * * * 10 2 = = 341

20 Backwards What’s the value of the following number? Base 2 Invalid! Base 5 4 * * * * * 5 4 = 4 * * * * * 625 = = 1334 Base 10 20,314