Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Similar documents
Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Design and Debug: Essen.al Concepts Numerical Conversions CS 16: Solving Problems with Computers Lecture #7

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Binary Arithme-c CS 64: Computer Organiza-on and Design Logic Lecture #2

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

MIPS Assembly: More about MIPS Instructions Using Functions in MIPS CS 64: Computer Organization and Design Logic Lecture #8

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Dynamic Arrays Makefiles and Mul3ple File Compiles

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

File I/O CS 16: Solving Problems with Computers I Lecture #9

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

Pre- Defined Func-ons in C++ Review for Midterm #1

Final Labs and Tutors

CMPS 10 Introduction to Computer Science Lecture Notes

File I/O in Python Formats for Outputs CS 8: Introduction to Computer Science, Winter 2018 Lecture #12

Welcome to Computer Organization and Design Logic

MIPS Instruc,ons CS 64: Computer Organiza,on and Design Logic Lecture #8

Octal & Hexadecimal Number Systems. Digital Electronics

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Getting started with C++ (Part 2)

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Welcome to Solving Problems with Computers I

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

Structures and Classes CS 16: Solving Problems with Computers I Lecture #15

Unit 14. Passing Arrays & C++ Strings

Binary Arithme-c 2 Intro to Assembly CS 64: Computer Organiza-on and Design Logic Lecture #3

Welcome to Computer Organization and Design Logic CS 64: Computer Organization and Design Logic Lecture #1 Winter 2018

CS 103 Lab - Party Like A Char Star

Representation of Information

Array Elements as Function Parameters

Name EGR 2131 Lab #6 Number Representation and Arithmetic Circuits

CSE 12 Spring 2016 Week One, Lecture Two

CMSC201 Computer Science I for Majors

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

The C++ Language. Arizona State University 1

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

211: Computer Architecture Summer 2016

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CS 106B, Lecture 1 Introduction to C++

CS103L FALL 2017 UNIT 1: TYPES, VARIABLES,EXPRESSIONS,C++ BASICS

C++ Basics 1 CS 16: Solving Problems with Computers I Lecture #3

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Flow Control in C++ 1 CS 16: Solving Problems with Computers I Lecture #4

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

MIPS and Basic Assembly Language CS 64: Computer Organization and Design Logic Lecture #4

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CMPSC210 Lecture 7: Hexadecimal Numbers. Prof. John Wenskovitch 09/16/2016

C++ for Java Programmers

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

NUMERIC SYSTEMS USED IN NETWORKING

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

TOPICS. Other Number Systems. Other Number Systems 9/9/2017. Octal Hexadecimal Number conversion

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

CHAPTER 2 (b) : AND CODES

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

EE 355 Lab 4 - Party Like A Char Star

CS Programming In C

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

378: Machine Organization and Assembly Language

Lab 03 - x86-64: atoi

CSE 12 Spring 2018 Week One, Lecture Two

CS 103 Lab 6 - Party Like A Char Star

Transcription:

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8 Ziad Matni Dept. of Computer Science, UCSB

Announcements We are grading your midterms this week! Grades will be available by Tuesday next week Next lab is a required pair programming assignment You are required to work with a partner for Lab 5 (next week) Read the descripoon for Lab 5 when it comes out over the weekend carefully for the details 10/26/17 Matni, CS16, Fa17 2

Note: FuncOons Calling FuncOons A function body may contain a call to another function The called function declaration must still appear before it is called Functions cannot be defined in the body of another function Including inside the main( ) function! void order (int&, int&); void swap_values (int&, int&); int main () { order (a, b); return 0; } void order(int& n1, int& n2) { if (n1 > n2) swap_values(n1, n2); } void swap_values(int& n1, int& n2) { int temp = n2; n2 = n1; n1 = temp; } 10/26/17 Matni, CS16, Fa17 3

PosiOonal NotaOon in Binary 11011 in base 2 positional notation is: 1 x 2 4 = 1 x 16 = 16 + 1 x 2 3 = 1 x 8 = 8 + 0 x 2 2 = 1 x 4 = 0 + 1 x 2 1 = 1 x 2 = 2 + 1 x 2 0 = 1 x 1 = 1 So, 11011 in base 2 is 16 + 8 + 0 + 2 + 1 = 27 in base 10 10/26/17 Matni, CS16, Fa17 4

ConverOng Binary to Octal and Hexadecimal (or any base that s a power of 2) Binary is 1 bit Octal is 3 bits (2 3 = 8) octal is base 8 Hexadecimal is 4 bits (2 4 = 16) hex is base 16 Use the group the bits technique Always start from the least significant digit Group every 3 bits together for bin à oct Group every 4 bits together for bin à hex 10/26/17 Matni, CS16, Fa17 5

Take the example: 10100110 to octal: 1 0 1 0 0 1 1 0 2 4 6 to hexadecimal: 1 0 1 0 0 1 1 0 10 6 ConverOng Binary to Octal and Hexadecimal 246 in octal Decimal symbols A6 in hexadecimal 10/26/17 Matni, CS16, Fa17 6 0 1 2 3 4 5 6 7 8 9 A B C D E F Octal symbols Hex. symbols

ConverOng Decimal to Other Bases Algorithm for converong number in base 10 to other bases While (the quotient is not zero) 1. Divide the decimal number by the new base 2. Make the remainder the next digit to the left in the answer 3. Replace the original decimal number with the quotient 4. Repeat until your quotient is zero EXAMPLE: Convert the decimal (base 10) number 79 into hexadecimal (base 16) 79 / 16 = 4 R 15 (15 in hex is the symbol F ) 4 / 16 = 0 R 4 The answer is: 4F 10/26/17 Matni, CS16, Fa17 7

ConverOng Decimal into Binary Convert 54 (base 10) into binary and hex: 54 / 2 = 27 R 0 27 / 2 = 13 R 1 13 / 2 = 6 R 1 6 / 2 = 3 R 0 3 / 2 = 1 R 1 1 / 2 = 0 R 1 110110 54 (decimal) = (binary) = 36 (hex) Sanity check: 110110 = 2 + 4 + 16 + 32 = 54 10/26/17 Matni, CS16, Fa17 8

Class Exercises Convert these binaries into decimal AND hexadecimal: 1010101010 10100001000 Ans: 682 (dec) 2AA (hex) 1288 (dec) 508 (hex) Convert these decimals into binary: 65 333 Ans: 1000001 101001101 10/26/17 Matni, CS16, Fa17 9

An Intro to Strings 10/26/17 Matni, CS16, Fa17 10

Strings in C/C++ Recall: C++ is based on C Originally (in C), strings were defined as an array of characters Called C- Strings and are legacy data types in C++ Came with the library <cstring> Contains lots of built- in funcoons that go with C- Strings In C++, we got a new library: <string> Made improvements over the old C- String Library contains another collecoon of funcoons that work with Strings, but not C- Strings! 10/26/17 Matni, CS16, Fa17 11

Why Do We Care About C- Strings?? Their use STILL crops up in C++ Recall: command line arguments What s with the star??? * Is used here to indicate a pointer. We ll discuss these at a later date. Recall that command- line arguments, specifically argv[x] are defined as: char* [ ] That s a classic definioon of a C- String So if we want to use these argv[x], we ll have to treat them in a C- String fashion 10/26/17 Matni, CS16, Fa17 12

What is a String? Characters connected together in a sequence P i k a c h u H i M o m! 10/26/17 Matni, CS16, Fa16 13

C strings vs. C++ strings C++ is meant to be backwards compaoble with C C has one way of dealing with strings, while C++ has another C++ s use is much easier and safer with memory allocaoon This is what you ve learned so far with <string> Let s briefly look at the other (older) way with C- strings 10/26/17 Matni, CS16, Fa16 14

What s a C++ Programmer to Do?! Be aware of 3 types of variables that deal with a bunch of connected characters An ordinary array of characters (we ll deal with arrays later ) Like any other array: no special properoes that other arrays do not have A C- string (that s the old style) An array of characters that s ALWAYS terminated by the null character \0 The null character has an ASCII code of 0. Library for dealing with these types: <cstring> A C++ string object An instance of a class data type used as a black box by programmers Library for dealing with these types: <string> 10/26/17 Matni, CS16, Fa16 15

The C String The null- character terminates the actual string, but not the array necessarily Example : a C- string stores Hi Mom! in a character array of size 10 The characters of the word Hi Mom! will be in posioons with indices 0 to 6 There will be a null character at index 7, and the locaoons with indices 8 to 9 will contain some undefined value that we won t care about It's the null character at index 7 that makes this otherwise ordinary character array a C- string. s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] H i M o m! \0???? 10/26/17 Matni, CS16, Fa16 16

The C++ String There is NO null- character to worry about! There are no array- related set- ups you have to worry about! There are some cool built- in funcoons More on those later 10/26/17 Matni, CS16, Fa17 17

Declaring a String in C++ You have to include the correct library module with: #include <string> Declare them (and inioalize them) with: string MyString= ; // Note the use of double- quotes! Since strings are made up of characters, you can index individual characters in strings (starong at posioon 0), using the square brackets [ ]: If MyString = Hello! Then MyString[0] = H, MyString[1] = e, etc 10/26/17 Matni, CS16, Fa17 18

REMEMBER: vs Double quotes are used exclusively for strings Both C and C++ strings Single quotes are used exclusively for characters 10/26/17 Matni, CS16, Fa17 19

Streams and Basic File I/O Files for I/O are the same type of files used to store programs A stream is a flow of data Input stream: Data flows into the program E.g. cin Output stream: Data flows out of the program E.g. cout You can also use data streams with files

Why Use Files? NEXT WEEK: File I/Os in C++ Files allow you to store data permanently! Data output to a file lasts arer the program ends You can usually view them without the need of a C++ program An input file can be used over and over No typing of data again and again for tesong Create or read files at your convenience Files allow you to deal with larger data sets

YOUR TO- DOs q Lab 4 due Fri. 10/27 q HW5 released today, due in 1 week. q Lab 5 will be released on Friday for Monday labs. q Visit Prof s and TAs office hours if you need help! q Floss! 10/26/17 Matni, CS16, Fa17 22

10/26/17 Matni, CS16, Fa17 23