Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?

Slides:



Advertisements
Similar presentations
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.
Advertisements

More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Please pick up an attendance question and submit in 5 minutes CS 1003 Lecture #3 Sept 12, 2006 Knarig Arabshian.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
1 Exam / Homework Exam 1 in Class 10 –Open book / open notes HW3 due next class HW4 will be on-line soon. Finishing Chapter 2 of K&R. We will go through.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CprE 185: Intro to Problem Solving (using C)
Chapter 7: Expressions and Assignment Statements
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
Week 3 - Friday CS222.
Tokens in C Keywords Identifiers Constants
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 7: Expressions and Assignment Statements
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
CS 240 – Lecture 8 Bitwise Operations, Bit Manipulation, Type Conversion, Conditional Expression.
Introduction to C Programming
Chapter 12 Variables and Operators
Conversions of the type of the value of an expression
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Java - Data Types, Variables, and Arrays
More about Numerical Computation
Introduction to C Programming
Chapter-3 Operators.
Lectures on Numerical Methods
Homework Homework Continue Reading K&R Chapter 2 Questions?
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Expressions and Assignment
Homework Starting K&R Chapter 5 Good tutorial on pointers
Chapter 3 Operators and Expressions
Homework Reading Programming Assignments Finish K&R Chapter 1
2. Second Step for Learning C++ Programming • Data Type • Char • Float
C++ Programming Basics
Module 2 Variables, Data Types and Arithmetic
Chap 7. Advanced Control Statements in Java
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Homework Homework Questions? Continue Reading K&R Chapter 2
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Chapter 12 Variables and Operators
Presentation transcript:

Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?

Type Conversion /* atoi: convert character string of digits to int (base 10) */ int atoi(char s[ ]) /* name based on “ascii to integer” */ { int i, n; n = 0; for (i=0; s[i] >= '0' && s[i] <= '9'; ++i) /* "is a digit" */ n = 10*n + (s[i] - '0'); /* s[i]-'0' is char add to 10*n is int */ return n; } Show the ascii table again -> for example 5. Range of numbers in ascii table For example you ask the user his age. 29 is ascii value 2 and ascii value 9 Subtract the ascii value of 2 from offset Second part of your hw3. xtoi()

Type Conversion /* itoa: convert int n to characters in base 10 in array s */ void itoa (int n, char s[ ]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0;

Type Conversion /* generate digits in reverse order */ do { /* new loop type */ s[i++] = n % 10 + '0'; /* generate next digit */ /* what conversion takes place here? */ } while(( n /= 10) > 0); /* delete digit from end of int */ if (sign < 0) s[i++] = '-'; s[i] = '\0'; reverse (s); /* reverse digit string */ } I should add the offset to print it correctly Like itox() in your homework -> the remainder could be 10,11,…,15 so you should convert them to a,b,c,…,f

Convert Upper Case to Lower int tolower(int c) /* this is function tolower(int c) in C library. To use it, you need to #include <ctype.h> (see K&R App. B2) */ { if (c >= 'A' && c <= 'Z') return c – ‘A’ + ‘a’; /* c - 'A' is the offset number... */ /* c -'A'+'a' is the corresponding lower case letter*/ else return c; } Defining the range between A and Z

Declarations and Initialization Section 2.4. int x, a[20]; /* external vars, initialized to zero */ int w = 37, v[3] = {1, 2, 3}; /* happens once */ main ( ) . . . func ( ) /* entered 1000 times per second */ { int y, b[20]; /* automatic -- contains junk on entry */ int s = 37, t[3] = {1, 2, 3}; /* happens on each entry */ } All the variables automatically initialized to zero except local automatic -> between curly braces spaces

C language Characteristics C language never does a possibly significant amount of work without this being specified by the programmer Initialization is not done unless programmer codes it in a way that causes the initialization to be done When you want the program to do something You should very explicitly tell it what you want to do

Type Conversions -Casts K&R Sect 2.7 char c1, c2 = 15; int j = 2379; /* 2379 = 0x 0000094b */ float g = 12.1; printf ("%d\n", c2 + j); /* what type, what printed ? */ ( int, 15 + 2379 = 2394 ) c1 = j; /* what type, value of c1? */ ( char, 2379 % 256 = 75 ) printf ("%d\n", c1 + c2); /* what type, value printed? */ ( char, 90 ) Float -> 4 bytes It always cast or change the one that has the smallest storage to the biggest one before operation. 1. So here C first convert c2 to a 32bit int number and then perform the add 2. In the other hand it just uses the 8 lowest significant bits

Casts K&R Sect 2.7 printf ("%d\n", (char) j + c2); /* value? */ ( 90 ) printf ("%9.1f\n", j + g); /* type, value? */ ( float, 2379. + 12.1 = 2391.1 ) printf ("%d\n", j + (int) g); /* type, value? */ ( int, 2379 + 12 = 2391 ) Convert integer to floating point first before addition

Type Conversion Rules Precise conversion rules in Section 6 of Appendix A If either operand is long double, convert the other to long double Otherwise if either operand is double, convert the other to double Otherwise if either operand is float, convert the other to float Otherwise, convert char and short to int Then if either operand is long, convert the other to long It’s the default one. When you didn’t explicitly cast a variable Always converts to the higher precision one

Increment / Decrement Operators Section 2.8. Both ++n and n++ have effect of n = n + 1; Both --n and n-- have effect of n = n – 1; With ++n or --n in an expression n is incremented/decremented BEFORE being used With n++ or n-- in an expression value is used THEN n is incremented/decremented int arr[4] = {1, 2, 3, 4}, n = 2; printf("%d\n", arr[n++]); /* values printed? */ printf("%d\n", arr[--n]); printf("%d\n", arr[++n]); 3 3 4

Evaluation Order and Precedence For most operations, the order of evaluation is determined by precedence Exceptions: Pre- increment and decrement operators are always evaluated before their operand is considered Post- increment and decrement operators are always evaluated after their operand is considered

Increment / Decrement Operators Precedence, pg. 53, specifies the binding order, not the temporal order. Consider two statements: n = 5; m = n-- + 7; In second statement, binding is: m = ((n--) + 7); But value of n-- is evaluated last Uses value 5 for n in evaluation of the expression The value of n is not decremented to 4 until the ENTIRE EXPRESSION has been evaluated (in this case, the right side of assignment statement) Temporal order of execution is not specified by binding order of operators in precedence table

Increment / Decrement Operators Hard to predict sometimes, you need to experiment n = 3; n = (n++)*(n++); Do we get 3*3+1+1 = 11? It might be different on different machines, so don't do this! With "gcc" we get result 9!   Another example: printf ( "%d %d\n", ++n, n); ??? Unclear what is printed out. Which expression is evaluated first?

Bit-wise Operators Section 2.9 We’ve already covered these, but they can be difficult when using them for first time Bit-wise Operators & bit-wise AND | bit-wise inclusive OR ^ bit-wise exclusive OR ~ one’s complement << left shift >> right shift

Difference between the bit wise operator & and logical operator && Example … int a =0xf, b= 0xab, c, d; c = a & b; d = a && b; c = 0xb d = 0x1 because it is true True True

Bit-wise Operators Masking is the term used for selectively setting some of the bits of a variable to zero & is used to turn off bits where “mask” bit is zero n = n & 0xff resets all except 8 LSBs to zero | is used to turn on bits where “mask” bit is one N = n | 0xff sets all LSBs to one

Bit-wise Operators Other tricks with bit-wise operators: ^ can be used to zero any value n = n ^ n sets value of n to zero ~ can be used to get the negative of any value n = ~n + 1 sets value of n to –n (2’s complement)

Get a Group of Bits Get a bit field of n from position p in value (put in least significant bits with zeros above) unsigned getbits(unsigned x, int p, int n) { return (x >> (p+1-n)) & ~(~0 << n)); } 7 6 5 4 3 2 1 Position p n Bits N=3 Shift all the bits to right to the last position of n bits and then mask everything after them

Assignment Operators Section 2.10. expr1 op = expr2 means expr1 = (expr1) op (expr2) i += 3 means i = i + 3; We can also use other operators int i = 3; i += 3; /* value now? */ ( 6) i <<= 2; /* value now? */ (24) i |= 0x02; /* value now? */ (24 = 0x1, 0x18 | 0x02 = 0x1a)

K&R Exercise 2-9 Find how many 1’s in a binary number? Use the expression to set the rightmost 1-bit to a 0 x &= (x-1) Repeat until x is all zeros char x = 0xa4 has bits 10100100 (three 1-bits) x = 10100100 x = 10100000 & x-1 10100011 & x-1 10011111 10100000 10000000 x = 10000000 & x-1 01111111 00000000 Bits all counted What about char x = 0? Number of times If the number is zero, you should not use this algorithm Write exeption codes for this conditins

Conditional Expressions Section 2.11 Example: To implement z = max(a, b) if (a > b) z = a; else z = b; As long as both sides of if statement set only one variable value, it can be written: z = (a > b)? a: b; Ternary condition

Conditional Expressions Can also nest conditionals: z = (a > b)? a: ((x < y)? b: 0); Can include conditions inside an expression (e.g. to print EOL every 10th value or at end): for (i = 0; i < n; i++) printf(“%6d%c”, a[i], (i%10 = = 9 || i = = n-1) ? ‘\n’ : ‘ ’);