CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
© 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.
Chapter 3 Data Representation.
Introduction to C Programming CE Lecture 2 Basics of C programming.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Chapter 3 Data Representation. Chapter goals Describe numbering systems and their use in data representation Compare and contrast various data representation.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Covenant College September 3, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 6: Character Strings.
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Computers Organization & Assembly Language
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Input & Output: Console
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
Presenting results to the USER in a professional manner 1. semicolon, disp(), fprintf() 2. Placeholders 3. Special characters 4. Format-modifiers Output.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
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.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Asking the USER for values to use in a software 1 Input.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Asking the USER for values to use in a software 1 Input.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
1. Normal arrays of characters 2. Converting a list of strings to a cell- arrays of strings 3. Converting a cell-array of strings to a list of strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Data Representation COE 308 Computer Architecture
28 Formatted Output.
Data Representation ICS 233
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
2.1 Parts of a C++ Program.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Coding Concepts (Data- Types)
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 2: Introduction to C++.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Data Representation COE 308 Computer Architecture
Presentation transcript:

CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS

Character and String Data Character arrays store character information A character array is produced from a string

Characters Any string represents a character array in MATLAB Each character requires 2 bytes of storage

Spaces are characters too The ‘abc’ symbol indicates a character array

How are characters stored in MATLAB? All information in computers is stored using a series of zeros and ones ASCII – Used in small computers EBCDIC – Used in mainframes and super computers You can think of this list of 0’s and 1’s as a base two number

Character and String Data Every character stored using ASCII or EBCDIC code has both a binary representation and a decimal equivalent When we ask MATLAB to change a character to a double, the number we get is the decimal equivalent in the ASCII coding system

MATLAB includes functions to change data types Use the double function to convert to a double precision floating point number Also use: >> uint8(‘a’) Use char to convert a number to a character

How are Characters Stored? >> u = double(T) % double is a built-in function. >> char(u) % performs the opposite function. >> u=double(C)‏ u = >> char(u)‏ ans = Hello Example: Conversion between Character and Numeric: Character arrays are similar to vectors, except: Each element contains a single character

Compare Strings Strings are translated into vectors of numbers (ASCII)‏ Use any logical operators However:Must be the same length >> 'fred' == 'brad' >> 'fred' == 'george'% produces error since different lengths ??? Error using ==> eq

String Comparison Matlab provides a function to compare any set of strings strcmp('string1', 'string2') Returns: 1 (true) if strings are identical 0 (false) if strings are not identical >> strcmp('fred', 'george')‏ ans = 0 Also use isequal(x,y)

Output Options Enter the name of a variable Use the disp function Use the fprintf function Also use sprintf function

Display The display (disp) function can be used to display the contents of a matrix or string

Display The disp function only takes one input you must combine arrays to make more complicated output Use the num2str(x) function to change numeric information to a string disp(['The values in the x array are: ' num2str(x)])‏

Although these characters look like numbers, they are interpreted by the computer as part of a character array – they no longer have any numeric meaning Notice that the ans matrix is listed as a character array

Display If you want to include an apostrophe in a string, you need to enter the apostrophe twice. If you don’t, MATLAB thinks the apostrophe terminates the string. For example: disp('The moon''s gravity is 1/6th that of the earth')

Formatted Output fprintf gives you more control over your output than the disp function You can combine text and numbers You can control how many digits to display, and their position

fprintf fprintf('text to display %#.#f \n' variable);  %f –displays fixed point or decimal  %s–displays string variables  %e–displays exponential notation  \n– new line  \t – tab  \f – form feed (new line, same position)‏

Variable 8 total spaces 2 after the decimal point floating point format

X is a matrix /n is a carriage return

fprintf If you want to include a percentage sign in an fprintf statement, you need to enter the % twice. If you don’t, MATLAB thinks the % is a placeholder for data. For example: fprintf('The interest rate is %5.2f % \n', 5)‏ results in: The interest rate is 5.00 %

For example… The US Naval Academy requires applicants to be at least 5’6” tall Consider this list of applicant heights 63”, 67”, 65”, 72”, 69”, 78”, 75” Which applicants meet the criteria?

The find function returns the index number for elements that meet a criteria index numbers element values

Create a more readable report