CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Expressions, Data Conversion, and Input
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Chapter 2 Variables.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 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 2 Variables.
CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Objects and Primitive Data
Chapter 2 Variables.
Instructor: Alexander Stoytchev
Lectures on Numerical Methods
Storing Information Each memory cell stores a set number of bits (usually 8 bits, or one byte) (byte addressable)
Instructor: Alexander Stoytchev
Chapter 2: Objects and Primitive Data
Chapter 2 Variables.
Instructor: Alexander Stoytchev
Presentation transcript:

CprE 185: Intro to Problem Solving (using C) Instructor: Alexander Stoytchev http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/

Arithmetic Expressions CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Administrative Stuff Hw2 is out Part I is Due on Monday Sep 14 @ 3pm Paper submission before the start of class. Part II is Due on Friday Sep 11 @ 8pm. Electronic submission on WebCT

Administrative Stuff No class next Monday (Labor Day)

Quick review of the last lecture

Multiple variables can be created in one declaration A variable is a name for a location in memory A variable must be declared by specifying the variable's name and the type of information that it will hold data type variable name int total; int count, temp, result; Multiple variables can be created in one declaration © 2004 Pearson Addison-Wesley. All rights reserved

Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Through a Function y = f(x); Q = sin(30); The assignment operator is still the = sign

Assignment Through scanf() int variable; scanf(“%d”, &variable); <keyboardinput> 30 There is not assignment operator in this case

Some Primitive Data Types char short int long long long float double long double

float and double analogy [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

float and double analogy [image fom http://www.simplylockers.co.uk/images/PLowLocker.gif]

Numeric Primitive Data The difference between the various numeric primitive types is their size, and therefore the values they can store: Type char short int long float double Storage 8 bits 16 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 © 2004 Pearson Addison-Wesley. All rights reserved

Computer Memory Main memory is divided into many memory locations (or cells) 9278 9279 9280 9281 9282 9283 9284 9285 9286 Each memory cell has a numeric address, which uniquely identifies it

Storing Information 9278 9279 9280 9281 9282 9283 9284 9285 9286 Each memory cell stores a set number of bits (usually 8 bits, or one byte) 10011010 Large values are stored in consecutive memory locations

Storing a char 9278 9279 9280 9281 9282 9283 9284 9285 9286 char (8 bits = 1 byte)

Storing a short 9278 9279 9280 9281 9282 9283 9284 9285 9286 short (16 bits = 2 bytes)

Storing an int 9278 9279 9280 9281 9282 9283 9284 9285 9286 int (32 bits = 4 bytes)

Storing a long long (64 bits = 8 bytes) 9278 9279 9280 9281 9282 9283 9284 9285 9286

Storing a float 9278 9279 9280 9281 9282 9283 9284 9285 9286 float (32 bits = 4 bytes)

Storing a double double (64 bits = 8 bytes) 9278 9279 9280 9281 9282 9283 9284 9285 9286

The sizeof() operator #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { printf("Size of char = %d \n", sizeof(char)); printf("Size of short = %d \n", sizeof(short)); printf("Size of int = %d \n", sizeof(int)); printf("Size of long = %d \n", sizeof(long)); printf("Size of long long = %d \n", sizeof(long long)); printf("Size of float = %d \n", sizeof(float)); printf("Size of double = %d \n", sizeof(double)); printf("Size of long double = %d \n", sizeof(long double)); system("PAUSE"); return 0; }

Binary Numbers Once information is digitized, it is represented and stored in memory using the binary number system A single binary digit (0 or 1) is called a bit Devices that store and move information are cheaper and more reliable if they have to represent only two states A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) Permutations of bits are used to store values

Bit Permutations 1 bit 1 2 bits 00 01 10 11 3 bits 000 001 010 011 100 1 2 bits 00 01 10 11 3 bits 000 001 010 011 100 101 110 111 4 bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Each additional bit doubles the number of possible permutations © 2004 Pearson Addison-Wesley. All rights reserved

Bit Permutations Each permutation can represent a particular item There are 2N permutations of N bits Therefore, N bits are needed to represent 2N unique items 1 bit ? 2 bits ? 3 bits ? 4 bits ? 5 bits ? How many items can be represented by 21 = 2 items 22 = 4 items 23 = 8 items 24 = 16 items 25 = 32 items © 2004 Pearson Addison-Wesley. All rights reserved

Relationship Between a Byte and a Bit [Figure 1.5in the textbook]

What is the value of this binary number? 0 0 1 0 1 1 0 0 0 0 1 0 1 1 0 0 0*27 + 0*26 + 1*25 + 0*24 + 1*23 + 1*22 + 0*21 + 0*20 0*128 + 0*64 + 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 32+ 8 + 4 = 44 (in decimal)

What is the maximum number that can be stored in one byte (8 bits)?

What is the maximum number that can be stored in one byte (8 bits)? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1*27 + 1*26 + 1*25 + 1*24 + 1*23 + 1*22 + 1*21 + 1*20 1*128 + 1*64 + 1*32 + 1*16 + 1*8 + 1*4 + 1*2 + 1*1 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 (in decimal) Another way is: 1*28 – 1 = 256 – 1 = 255

What would happen if we try to add 1 to the largest number that can be stored in one byte (8 bits)? 1 1 1 1 1 1 1 1 + 1 ------------------------------- 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Analogy with car odometers

Analogy with car odometers [http://www.hyperocity.com/volvo240/images/Volvo/odometerrepair/speedo999999.jpg]

Signed integers are more complicated

The story with floats is more complicated IEEE 754-1985 Standard [http://en.wikipedia.org/wiki/IEEE_754]

In the example shown above, the sign is zero so s is +1, the exponent is 124 so e is −3, and the significand m is 1.01 (in binary, which is 1.25 in decimal). The represented number is therefore +1.25 × 2−3, which is +0.15625. [http://en.wikipedia.org/wiki/IEEE_754]

On-line IEEE 754 Converters http://www.h-schmidt.net/FloatApplet/IEEE754.html http://babbage.cs.qc.edu/IEEE-754/Decimal.html

Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string literal." "123 Main Street" "X" © 2004 Pearson Addison-Wesley. All rights reserved

Characters A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' '; Note the distinction between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters © 2004 Pearson Addison-Wesley. All rights reserved

Characters The ASCII character set is older and smaller than Unicode, but is still quite popular The ASCII characters are a subset of the Unicode character set, including: uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ... © 2004 Pearson Addison-Wesley. All rights reserved

ASCII Table

Extended ASCII Codes

The Unicode Character Code http://www.unicode.org/charts/

Escape Sequences What if we wanted to print a the quote character? The following line would confuse the compiler because it would interpret the second quote as the end of the string printf ("I said "Hello" to you."); An escape sequence is a series of characters that represents a special character An escape sequence begins with a backslash character (\) printf ("I said \"Hello\" to you."); © 2004 Pearson Addison-Wesley. All rights reserved

Escape Sequences Some C escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return beep double quote single quote backslash © 2004 Pearson Addison-Wesley. All rights reserved

printf() function printf(“format string”, variable1, variable2, …); printf(“For int use %d”, myInteger); printff(“For float use %f”, myFloat); printf(“For double use %lf”, myDouble); printf(“For float or double %g”, myF_or_D); printf(“int=%d double %lf”, myInteger, myDouble);

scanf() function scanf(“format string”, &variable1, &variable2, …); scanf(“%d”, &myInteger); scanf(“%f”, &myFloat); scanf(“%lf”, &myDouble); scanf(“%d%f”, &myInteger, &myFloat);

Common Bugs Using & in a printf function call. printf(“For int use %d”, &myInteger); // wrong Using the wrong string in printf printf(“This is a float %d”, myFloat); // use %f not %d Not using & in a scanf() function call. scanf(“%d”, myInteger); // Wrong Using the wrong string in scanf() scanf(“%d”, &myFloat); // wrong; use %f instead of %d

Why do we need to specify the format of the keyboard input before we can read it?

Evaluating Expressions

Expressions An expression is a combination of one or more operators and operands Arithmetic expressions compute numeric results and make use of the arithmetic operators: Addition Subtraction Multiplication Division Remainder + - * / % If either or both operands used by an arithmetic operator are floating point, then the result is a floating point © 2004 Pearson Addison-Wesley. All rights reserved

Division and Remainder If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals 4 8 / 12 equals The remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3 equals 2 8 % 12 equals 8 © 2004 Pearson Addison-Wesley. All rights reserved

result = total + count / max - offset; Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order © 2004 Pearson Addison-Wesley. All rights reserved

Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1 © 2004 Pearson Addison-Wesley. All rights reserved

Expression Trees The evaluation of a particular expression can be shown using an expression tree The operators lower in the tree have higher precedence for that expression a + / - d b c a + (b – c) / d © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated answer = sum / 4 + MAX * lowest; 4 1 3 2 Then the result is stored in the variable on the left hand side © 2004 Pearson Addison-Wesley. All rights reserved

Effect of sum = sum + item; Figure 2.4. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Effect of scanf("%lf", &miles); Figure 2.5. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree for area = PI * radius * radius; Figure 2.8. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Step-by-Step Expression Evaluation Figure 2.9. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1); Figure 2.10. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y Figure 2.11. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Questions?

THE END