CS 151 Fall 2003 - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

CS 151 Fall 2003

Description:

Bits - 0/1 values, that are organized into. Bytes - 8 bit quantities, that are ... It is only when it is printed that it is INTERPRETED as a printable value ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 44
Provided by: timoth90
Category:
Tags: fall | printable

less

Transcript and Presenter's Notes

Title: CS 151 Fall 2003


1
CS 151 - Fall 2003
  • Midterm 1 Review

2
Memory
  • Computer Memory consists of a sequence of
  • Bits - 0/1 values, that are organized into
  • Bytes - 8 bit quantities, that are then combined
    to form
  • Words - typically 4 byte (32 bit) values

3
Types
  • A Type tells us how to INTERPRET a sequence of
    bits. Same bits mean different things depending
    upon their type
  • char - character values
  • int - Integer values
  • float, double - floating-point values

4
Characters are just numbers
  • A character is just a number. It is only when it
    is printed that it is INTERPRETED as a printable
    value
  • char c48 / 48 is ASCII '0' /
  • printf("c d c", c, c, 48) / 0 48 0 /

5
Reading Binary Numbers
  • Binary numbers use powers of 2, not powers of 10.
  • 42 0101010

6
Structure of a Program
  • Include directives (stdio, math, etc)
  • Prototypes
  • Main program
  • Other functions

7
Include directives
  • The include statements tell the compiler the
    format for functions from a library
  • stdio.h - standard I/O
  • math.h - mathematical functions
  • other libraries

8
Main program
  • Execution always begins with a function named
    main. which (for now) takes no arguments and
    returns an integer
  • int main(void)
  • ...
  • return 0

9
printf
  • Our output is produced using printf
  • printf(format, args ... args)
  • printf("s d", "value is ", x)

10
Formatting - Special Characters
  • A few special characters can appear in formatting
    string
  • \n - newline
  • \t - tab
  • \b - backspace
  • \\ - slash

11
Formatting - type specifiers
  • Type of argument must be matched to type in
    formatting string
  • d - (decimal) integer
  • c - character
  • s - string
  • f - floating point

12
Formatting - length specifiers
  • Specifiers can have an optional length
  • 3d - max 3 decimal digits
  • 10s - exactly 10 spaces in string
  • 5.2f - 5 spaces wide, 2 values after decimal
    point

13
Variables
  • A legal variable name consists of a letter
    followed by zero or more letters (including
    underscore) or digits
  • Yes - x, y, abc, very_long_name, p17
  • No - 18x, a b, long-name-with-dashes

14
Declarations
  • New variables must be declared before they can be
    used. You are allowed to declare several
    variables at once.
  • int x
  • int a, b, c
  • char d
  • double y, z

15
Initialization
  • You can give a variable an initial value at the
    point it is declared
  • int i 0
  • double x 10.8
  • int a 5, b, c17

16
Symbolic constants
  • You can create a symbolic constant using a
    define statement right after your include files
  • define MAX_FILES 27

17
Reading Values - Scanf
  • Scanf requires arguments that are the ADDRESS of
    the variables to fill
  • scanf("d f", n, d)

18
Operators
  • Expressions can be formed using variables,
    constants, and operators, just as in mathematics.
    But there are lots more operators than just
    arithmetic.
  • x a b c
  • i

19
Assignment and Equality
  • Assignment is an operator, and is different from
    the equality testing operator
  • a b 27
  • if (a b)
  • is not the same as
  • if (a b)

20
Precedence
  • Precedence rules tell us how to interpret
    expressions involving two or more operators
  • x a b c
  • if (a b c d)

21
Associativity
  • Associativity rules tell us how to interprete
    expressions of two or more operators at the same
    precedence
  • x a - b - c
  • p q / r s

22
Increment and Decrement
  • Increment and decrement change a value
  • i --j
  • Two versions, both have same side effect, differ
    in their final result
  • int i 5, j 5
  • printf("d d", i, j) / produces 6 5 /
  • printf("d d", i, j) / produces 6 6 /

23
Integer Division
  • Division of integer values produces an integer
    result. Tosses away any remainder
  • int i 5
  • j i / 2
  • printf("d", j) / produces 2 /

24
Remainder
  • The mod operator () returns the remainder after
    division
  • int i 17
  • int j i 3
  • printf("d", j)

25
Mixed Type Operations
  • When operators are used with different types,
    characters are converted into ints, ints into
    floats, floats into doubles.
  • 'a'3
  • 5 / 2.0 vs 5 / 2

26
File I/O
  • Files use the data type FILE and pointers
  • FILE fip, fop
  • fip fopen("data.txt", "r")
  • fop fopen("data.out", "w")
  • fprintf(fop, "whatever is d", 5)
  • fscanf(fip, "d d", a, b)

27
Math Functions
  • There are a number of useful math functions
    defined in ltmath.hgt
  • abs(int)
  • fabs(double)
  • pow(double, double)
  • sin(double), etc

28
Statements
  • Statements execute one after the other, in order,
    unless changed by
  • conditional,
  • loop,
  • function calls, or
  • return statement

29
Conditional Statements
  • The simple conditional statement is the if
  • if (condition)
  • statement
  • if (condition)
  • statement
  • ...
  • statement

30
Conditions
  • Conditions can be boolean (produced by relational
    or logical operators), or
  • integer - zero is false, nonzero is true, or
  • pointer - null pointer is false, everything else
    is true
  • char - zero is false, nonzero is true

31
If then - else
  • If statement can have an else alternative
  • if (condition)
  • statement
  • else
  • statement

32
If statements can be nested
  • if (year 400 0)
  • printf("leap year")
  • else if (year 100 0)
  • printf("not leap year")
  • else if (year 4 0)
  • printf("leap year")
  • else
  • printf("not leap year")

33
The dangling else problem
  • if (a lt b) then
  • if (c lt d) then
  • p q
  • else
  • p m

34
While statements
  • while (condition) / pretest loop /
  • statement
  • do / post test loop /
  • statement
  • while (condition)

35
For statement
  • for (initialization test update)
  • statement
  • equivalent to
  • initialization
  • while (test)
  • statement
  • update

36
Nested Loops
  • Loops can be nested inside of each other. Inner
    loops can use values from outer loops
  • sum 0
  • for (i 1 i lt n i)
  • for (j 1 j lt i j)
  • sum j

37
Logical Operators
  • Logical operators are !, and (not, and, or)
  • if ((year 400 0)
    ((year 4 0) (year 100 ! 0)))
  • printf("leap year")
  • else
  • printf("not leap year")

38
Functions
  • Functions have three parts
  • Function prototype (declaration)
  • Function body (definition)
  • Function call (invocation)

39
Function Prototype
  • A prototype lists only the argument types and
    return type. The argument names are optional
  • int foo(int, double)
  • double bar (int a, double b)
  • char baz (int a, double, char)

40
Function Body
  • The definition, or function body, lists the
    statements that will be executed by the function
  • int foo (int a, double b)
  • return 2 a b

41
Pass by Value
  • Normally, arguments are passed by value. Actual
    argument is an initialization for the formal.
    Changing the formal does nothing to the actual.
  • void foo(int a) a 17
  • int x 23
  • foo(x) printf("d", x) / produces 23 /

42
Pass by Reference
  • Pass by reference is performed using pointers.
    Pass in the address of the variable. Change to
    pointer value changes actual.
  • void foo(int a) a 17
  • int x 23
  • foo(x) printf("d", x) / produces 17 /

43
Questions??
  • Now is your chance to get anything you don't
    understand cleared up.
Write a Comment
User Comments (0)
About PowerShow.com