Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

CS 100: Roadmap to Computing Fall 2014 Lecture 0.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
ES 314 Advanced programming, simulation and modeling lecture: M W 9 to 10 : 50 AM 2008, Salazar Hall.
Introduction to Computers and Programming Lecture 7:
Lecture 4 Sept 8 Complete Chapter 3 exercises Chapter 4.
Lecture 3 Sept 3 Complete Chapter 3 Exercises quiz.
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
Es 314 Programming, simulation and modeling lecture: M W 3 – 4:50 PM 2008, Salazar Hall.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
String Escape Sequences
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
Chapter 3 Representing Numbers and Text in Binary Information Technology in Theory By Pelin Aksoy and Laura DeNardis.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
CIS Computer Programming Logic
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Lecture # 5 Data Representation. Today Questions: From notes/reading/life? Prepare for Quiz # 1 (Multiple Choice) 1.Introduce: How do Computers store.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Agenda ASCII char N int Character class char N String Bitwise operators homework.
Es 314 Programming, simulation and modeling lecture: M W 9 – 10: , Salazar Hall.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1.2 Primitive Data Types and Variables
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
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.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 6: Data Types Lectures # 10.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Examples of Primitive Values
An overview of Java, Data types and variables
CS 100: Roadmap to Computing
The Data Element.
The Data Element.
CS 100: Roadmap to Computing
Variables and Constants
Presentation transcript:

Lecture 3 August 31 Chapter 3

Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by default. To store data as an integer, you need to convert from double to the desired integer type. Example: To store 325 as a 16-bit signed integer assigned to variable x: >> x = int16(325); If the number being converted to an integer has a fractional part, MATLAB rounds to the nearest integer.

If the fractional part is exactly 0.5, then from the two equally nearby integers, MATLAB chooses the one for which the absolute value is larger in magnitude: >> x = ; >> int16(x) ans = 325 >> x = x +.001; >> int16(x) ans = 326 Built-in functions that convert to int Other related functions: floor, ceil

long floating-point format >> format long >> x = 1.5^2.3; >> x x = >> format short >> x x = >> x = ; >> x x = >>

Complex numbers

Strings Character: alphabetical – upper/lower (‘a’.. ‘z’, ‘A’.. ‘Z’) digits – 0, 1, …, 9 special characters - $, % etc. control characters - \n (end of line) etc. Each character is encoded by 8 bits (ASCII) or 16 bits (unicode) Unicode allows encoding of alphabets from many languages such as Hungarian, Chinese, Swahili etc. String - sequence of characters. >> greeting = ‘hello’

String operations >> length(word) ans = 5 >> strcmp(word, ‘hello!’) ans = 0 1 if the Boolean expression is true, 0 else. strcmp compares two strings.

String operations >> length(greeting) ans = 5 >> strcmp(greeting, ‘hello!’) Ans = 0 ans = 1 if the Boolean expression is true, 0 else. strcmp compares two strings. Strings are case-sensitive. strcat concatenates (glues) strings

String operations strcmp(w1, w2) returns 1 (0) if the strings w1 and w2 are the same. Example: >> a = 'word'; >> b = ['wo', 'rd']; >> strcmp(a, b) ans = 1 >> a == b ans =

String operations strcat (w1, w2) concatenates the strings w1 and w2. Example: >> a = ‘this ’, b = ‘word'; >> c = strcat(a, b); >> c C = ‘thisword’ Strcat removes trailing blanks in the first argument. To avoid this removal, we can use: > c = [a, b];

In general, arrays can be merged using [ ] operation:

String operations A string is stored as a vector of (ASCII) characters. Example: >> str = 'abcdxyz'; >> str+0 ans = ASCII for ‘a’ is 97, for z it is = 122.

Collections of numbers and plotting Two ways to create a vector:

Creating a table of x and y values for plotting A common task is to create the data showing how a variable Y (temp) changes with another variable X (time). Example:

Plot of time vs. temp

Plotting graphs by mathematical relation

Boolean expressions Relational operators:, >=, ~=, == etc. Boolean connectives: & (and) | (or) ~ (not) xor (exclusive or) all any 1 represents TRUE, 0 represents FALSE

Boolean expressions Exercise: Write a one-line statement in MATLAB that returns the number of non- negative members in an array A.

Boolean expressions Exercise: Write a one-line statement in MATLAB that returns the number of non- negative members in an array A. Answer: sum (c >= 0)

Chapter 3 – solutions to some exercises and discussions Dicussion 3.1:

Exercise 3.1: sum(2.^(0 : 4)) Exercise 3.5:

Exercise 3.6 Write a code segment in Matlab to draw the rectangle shown in the figure below:

Exercise 3.12: How many leap years are between 1780 and 2832, inclusive? Recall the definition of a leap year. Exercise 3.8. Write the MATLAB version of the boolean statement a <= b <= c.

Solution to Exercise 3.8: Solution to Exercise 3.12: (a >= b) && (b >= c)