1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CSci 1130 Intro to Programming in Java
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
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?
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
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.
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,
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
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?
Homework Reading Programming Assignments
Types, Operators and Expressions CSE 2031 Fall /5/2015 3:59 PM.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
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.
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.
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.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
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.
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.
Lecture 3 Java Operators.
Chap. 2. Types, Operators, and Expressions
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Starting JavaProgramming
Types, Operators and Expressions
Building Java Programs
Homework Homework Continue Reading K&R Chapter 2 Questions?
Your questions from last session
Chapter 3 Operators and Expressions
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.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Homework Homework Questions? Continue Reading K&R Chapter 2
Problem 1 Given n, calculate 2n
Presentation transcript:

1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

2 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’ n & ‘\x1f’ & turn off all bits except bottom 5 Called "masking" the bits -- only see bits on in result where 1's found in mask value

3 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 = ~ = of required size Extends itself with 1 bits on left for length of x

4 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': | turn on MS 2 bits

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

6 Exchanging Data with Exclusive Or You can use exclusive or to exchange the values of two variables without using a temporary location for a “three way move” int i = 0x12, j = 0x34; i = i ^ j; /* i = 0x26 */ j = i ^ j; /* j = 0x12 */ i = i ^ j; /* i = 0x34 */

7 String Constants String constant: "I am a string.“ –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 With these definitions, can use: len = strlen(msg); where msg is string in a string array NOTE: Write your own string functions for homework!!

8 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

9 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}; enum months birthMonth = MAY; printf("%d\n", birthMonth); /* prints 5 */

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

11 Operators Arithmetic Operators: +Add -Subtract *Multiply /Divide % Remainder after division (modulo) 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

12 Operators Logical Operators: &&logical and ||logical or ! Not Examples: 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 inner parentheses actually not needed? See precedence table, P 53. Good to mark for exams!!

13 Relations / Comparisons We call a comparison between two arithmetic expressions a "relation" ae1 =, > ) 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)

14 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; }

15 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;

16 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 */ }

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

18 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

19 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)

20 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 Holding Variable String Array

21 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?