Homework Homework Questions? Continue Reading K&R Chapter 2

Slides:



Advertisements
Similar presentations
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
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.
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.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
String Escape Sequences
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
Types, Operators and Expressions CSE 2031 Fall /5/2015 3:59 PM.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Starting Chapter 2 K&R. Read ahead.. Call by Value vs Call by Reference Simple variables passed as function parameters in C are actually only copies on.
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
The Machine Model Memory
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
More important details More fun Part 3
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
University of Central Florida COP 3330 Object Oriented Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
C Basics.
C++ Simple Data Types Simple types Integral Floating
Chapter 12 Variables and Operators
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Starting JavaProgramming
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Types, Operators and Expressions
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Review Questions If the word size of a machine is 64-bits, which of the following is usually true? (pick all that apply) 64 bits is the size of a pointer.
Fundamentals 2.
Memory, Data, & Addressing II CSE 351 Winter 2018
Memory, Data, & Addressing II CSE 351 Summer 2018
Homework Homework Continue Reading K&R Chapter 2 Questions?
Chapter 16 Pointers and Arrays
Your questions from last session
CISC/CMPE320 - Prof. McLeod
Expressions and Assignment
Homework Starting K&R Chapter 5 Good tutorial on pointers
Chapter 3 Operators and Expressions
Memory, Data, & Addressing II CSE 351 Winter 2018
Homework Reading Programming Assignments Finish K&R Chapter 1
SSEA Computer Science: Track A
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Data Structures and Algorithms Introduction to Pointers
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
EECE.2160 ECE Application Programming
CISC/CMPE320 - Prof. McLeod
Just Enough Java 17-May-19.
OPERATORS in C Programming
OPERATORS in C Programming
Chapter 12 Variables and Operators
Presentation transcript:

Homework Homework Questions? Continue Reading K&R Chapter 2 We’ll go over HW2 HW3 is posted Questions?

Review HW2 Solution Let’s go over solutions to HW2 Learn to write pseudo code effectively Learn to think about the problem logically Learn to write C code from pseudo code These are key for being a good programmer

trim Pseudo code for the trim program while there is still a line to process for each character starting at the end of the line find the first non blank character or the beginning of the line if a non-blank character is found add an EOL and a terminating zero and print out the string

trim While writing the pseudo code, think about: A good strategy (e.g. scan line from the end backwards rather than trying to go forwards) Stopping each scan (e.g. not blank, not tab, etc) What’s needed to make a shorter line printable (e.g. add a new EOL and terminating zero) What indicates that it is time to stop looping on input lines (e.g. return from getline > 0 is false)

trim C code for the trim program while ((len = getline(line, MAXLINE)) > 0) { stop = 0; for (i=len-1; i>=0 && !stop; --i) { if (line[i] != ' ' && line[i] != '\t' && line[i] != '\n') { stop = 1; /* stop on any non-blank,tab,eol */ line[i+1] = '\n'; /* add an eol */ line[i+2] = '\0'; /* and a zero to term the string */ printf("%s",line); }

reverse Pseudo code for the reverse function find length of the string to reverse for each character from the beginning of the string to half way copy the character here to a holding variable copy the character the same distance from end to here copy the holding variable to the same distance from end You can define another array and copy the characters on to it Or you can define temporary location to store. So you need less space String Array Holding Variable 2 3 1

reverse While writing the pseudo code, think about: The basic steps needed (e.g., three way move) The loop conditions (e.g., go halfway through) The beginning/end as possible special cases What about a string that has an odd length? What about a string that has an even length? Does it matter if we move the middle character in an odd length string out and back in or not?

reverse C code for the reverse function int len, i; char c; len = strlen(line) - 1; /* array index = len - 1 */ for (i=0; i<=len/2; ++i) { /* go halfway through the line */ c = line[i]; /* save a character from line */ line[i] = line[len-i]; /* overwrite that character */ line[len-i] = c; /* save reversed character */ }

Forcing Groups of Bits Off Given char n, how to turn off all bits except the least significant 5 bits: n = n & ‘\x1f’ n = ‘\xa5’ 10100101 n & ‘\x1f’ 10100101 & 00011111 turn off all bits 00000101 except bottom 5 Called "masking" the bits -- only see bits on in result where 1's found in mask value Sometimes when you do C programming you’ll deal with hardware. You want to see which bit of the register is set or not Turning off the bits that are 0 in the mask

Forcing Groups of Bits Off x = x & ~077 (octal for a change) Sets least significant 6 bits in x to 0 Even if you don't know size of x (e.g. size of int) ~077 = ~00 . . . 00111111 = 11 . . . 11000000 of required size Extends itself with 1 bits on left for length of x If you are interested in upper bits of X

Forcing Groups of Bits On Given n, how to turn on the MS two bits (if already on, leave on). n = n | ‘\xc0’ n = '\xa5' n | '\xc0': 10100101 | 11000000 turn on MS 2 bits 11100101

“Encryption” with Exclusive Or Show that x ^ (x ^ y) == y char y =‘\xa5’ 10100101 (plain text bits) char x =‘\x69’ 01101001 (encryption key) x ^ y 11001100 (cipher text bits) x ^ (x ^ y) 10100101 (decrypted bits) Same as original value of y!

String String: “I am a string.” Also have string functions: An array (a pointer to a string) of char values somewhere ending with NUL = '\0', the char with zero value. "0" is not same as '0'. The value "0" can't be used in an expression - only in arguments to functions like printf(). Also have string functions: See pg. 241, Appendix B and B3, pg. 249 #include <string.h> With these definitions, can use: len = strlen(msg); where msg is string in a string array NOTE: Write your own string functions for homework!! ‘0’ -> ascii code of 0 “0” -> the string of 2 locations: ascii value of 0 and the terminating character (\0)

Enumeration Symbolic Constants enum boolean {FALSE, TRUE}; Enumerated names assigned values starting from 0 FALSE = 0 TRUE = 1 Now can declare a variable of type enum boolean: enum boolean x; x = FALSE;  Just a shorthand for creating symbolic constants instead of with #define statements Variables inside enum, doesn’t care what you called them. It assigns 0 to first, 1 to second, 2 to third, … So use enum instead of multiple #define statements

Enumeration Symbolic Constants We can assign values to some name in any order. All unassigned names get value as value of previous name plus one enum day {sunday = 1, monday, tuesday = 5, wednesday, thursday = 10, friday, saturday}; Enum variables are automatically assigned values, but Macros not 2 6 11 12

Enumeration Symbolic Constants If you define months as enum type enum months {ERR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Debugger might print value 2 as FEB Add a junk word before your variables to start from 1 or type JAN=1,

Code Example of the enum type int main() { enum month {ERR, JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; enum month this_month; this_month = FEB; … }

const "const" declaration is like "final" in Java warns compiler that variable value shouldn't change const char msg[ ] = "Warning: . . ."; Commonly used for function arguments int copy(char to[ ], const char from[ ]); If logic of the copy function attempts to modify the “from” string, compiler will give a warning Exact form of warning and actual behavior of the code is installation defined You don’t want to change something so define it as const

Operators Arithmetic Operators: Examples: + Add - Subtract * Multiply / Divide % Modulo (Remainder after division ) e.g. 5%7 = 5 7%7 = 0 10%7 = 3 Examples: int x, y; x / y truncates (no fractional part) x % y is the remainder when x is divided by y. Always true that: x == y*(x/y) + x%y Use modulo in hw3!

Operators Logical Operators: Example: && logical and || logical or ! Not Example: … int year; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) printf( "%d is a leap year\n", year); else printf( "%d is not a leap year\n", year); Why are no inner parentheses needed? See precedence table, P 53. Good questions for exams!! Bit wise operators in past Precedence table!

Precedence and Associativity of Operators sign dereference address casting Associativity ( ) [ ] -> . Left to right ! ~ ++ - - + - * & (type) sizeof right to left * / % left to right + - left to right << >> left to right < <= > >= left to right = = != left to right & left to right ^ left to right | left to right && left to right || left to right ?: right to left = += -= *= /= %= &= ^= |= <<= >>= right to left , left to right Precedence

Precedence and Associativity Operators in the same row have the same precedence Operators are in order of decreasing precedence Associativity Determine the order if the operators have the same precedence e.g x = y +=z -= 4 means x = y +=(z -= 4) x =(y +=(z -= 4))

Evaluate a Logical Expression Evaluation of a logical expression is tricky Right-hand part of the expression is evaluated conditionally on the value of the left-hand part Depending on the expression, the right-hand part may not be evaluated

Relations / Comparisons We call a comparison between two arithmetic expressions a "relation"  ae1 <= ae2 (Comparisons: <, <=, = =, !=, >=, > ) A relation is evaluated as true or false (1 or 0) based on values of given arithmetic expressions if ( i < lim-1 = = j < k) What's it mean? See precedence table P 53 Instead of c != EOF, could write !(c = = EOF)