CSci 152: Programming II Fall 2004 - PowerPoint PPT Presentation

1 / 53
About This Presentation
Title:

CSci 152: Programming II Fall 2004

Description:

Type Conversion (Casting) float x = float(24/7) CSci 152: Prog II Fall 2004. Identifiers ... Procedure/function calls also break up sequential execution to modularize ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 54
Provided by: DerekH71
Category:

less

Transcript and Presenter's Notes

Title: CSci 152: Programming II Fall 2004


1
CSci 152 Programming IIFall 2004
  • Review

2
Fundamentals Prog II
  • What is a programming language?
  • Specifically what is the C programming
    language?
  • And how do we write programs (or recipes) in C
    in order to solve problems and perform tasks with
    the computer?

3
Programming Languages
  • Programming language A set of rules, symbols and
    special words.
  • syntax tells which statements are legal or
    accepted
  • semantics determine meanings of instructions

4
Basic Elements of C
  • A C Program (or any program in a procedural
    programming language)
  • Collection of 1 or more subprograms called
    functions
  • Which is a collection of statements that can
    accomplish something useful
  • sequence, selection, repetition
  • Special function called main

5
Review of C Components
  • Variables, Data Types Expressions
  • Control Structures
  • sequence
  • Selection or conditions
  • repetition or loops
  • Functions

6
Hello World!
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout ltlt Welcome to C Programming. ltlt
    endl
  • return 0

7
C Tokens
  • Special Symbols
  • - / . ? , lt ! gt
  • Word Symbols
  • int, float, char, void, return
  • Identifiers
  • Identifiers are names of things that appear in
    programs, such as variables, constants, and
    functions.
  • Some are predefined others are defined by you,
    the programmer.

8
C Data Types
  • def Data type A set of values together with a
    set of operations
  • simple data type
  • structure data type
  • pointers

Cs Data types
Simple
Structured
Pointers
9
Simple Data Types
  • The simple data type is the fundamental data type
    in C, building block for the structured data
    type
  • Integral, deals with integers or numbers withut a
    decimal part
  • int, bool, char, short, long, unsigned variants
  • Floating-point, deals with decimal numbers
  • float, double, long double
  • Enumeration type, which is a user-defined data
    type
  • e.g. enum olympicSports basketball, volleyball,
    diving, swimming, trackAndField, marathon,
    archery

10
Arithmetic Operators and Expressions
  • - /
  • Expressions
  • 2 5
  • 10 6
  • x y / z (0.5 w)
  • Type Conversion (Casting)
  • float x float(24/7)

11
Identifiers
  • Identifier
  • Identifier A C identifier consists of letters,
    digits, and the under score character (_), and
    must begin with a letter or underscore.
  • C is case sensitiveuppercase and lower case
    letters are different.
  • Some of the predefined identifiers are cout and
    cin.
  • Unlike reserved words, pre-defined identifiers
    may be redefined, but it would not be wise to do
    so.

12
Declaring Variables
  • Storing data in the computers memory is a two
    step process.
  • Instruct the computer to allocate memory.
  • Include statements in the program to put data
    into the allocated memory.
  • Named constant A memory location whose content
    is not allowed to change during program execution
  • const double conversion 2.54
  • const int noOfStudents 20
  • const char blank
  • Variable A memory location whose content may
    change during program execution.
  • double amountDue
  • int counter
  • char ch
  • int x, y
  • string name
  • assignment
  • variable expression
  • x 4
  • y 4 5 11

13
Declaring Variables
  • 1. In C, all identifiers must be declared
    before they can be used. If we refer to an
    identifier without declaring it, the compiler
    will generate an error message indicating that
    the identifier is not declared.
  • 2. A data type is called simple if the variable
    (or named constant) of that type can store only
    one value at a time. For example, if x is an,
    say, int variable. Then at a given time only one
    value can be stored in x.

14
Putting Data into Variables
  • In C there are two ways that data can be placed
    into a variable
  • 1. Using Cs assignment statement, and
  • 2. Use input (read) statements.

15
Assignment Statement
  • The assignment statement takes the form
  • variable expression
  • The expression is evaluated and its value is
    assigned to the variable on the left side.
  • In C, is called the assignment operator.

16
Quick Quiz
  • Get out a piece of paper
  • Declare 2 integer variables, a character variable
    and 2 floating point variables (using correct C
    syntax).
  • Assign the value of 4 to one of your integer
    variables.
  • Write an expression that multiples the previous
    integer by 3 and assigns the result to the other
    integer.
  • Assign the character A to your character
    variable.
  • Assign 3.1415 to one of your floating point
    variables.
  • Write an expression to add 1.5 to the previous
    variable and assign the result back to itself.

17
Quick Quiz Answers
  • Declare 2 integer variables, a character variable
    and 2 floating point variables (using correct C
    syntax).
  • int x, y
  • char c
  • float a, b
  • Assign the value of 4 to one of your integer
    variables.
  • x 4
  • Write an expression that multiples the previous
    integer by 3 and assigns the result to the other
    integer.
  • y 3 x
  • Assign the character A to your character
    variable.
  • c A
  • Assign 3.1415 to one of your floating point
    variables.
  • a 3.1415
  • Write an expression to add 1.5 to the previous
    variable and assign the result back to itself.
  • a 1.5 a

18
Control Structures
  • There are really only three main types of control
    structures in standard procedural programming
    languages
  • Sequence steps are normally performed in a
    sequential manner
  • Selection One of several actions is selected and
    executed (branch, other actions are not
    executed).
  • Repetition One or more steps is performed
    repeatedly.

19
Control Structures Sequence
  • Normally in procedural programs, statements are
    executed in a sequential manner.
  • selection and repetition control structures break
    up this sequential execution by introduction
    branch points and loops respectively
  • Procedure/function calls also break up sequential
    execution to modularize algorithm into several
    small pieces

20
Example of a sequence
  • int x, y
  • char c
  • float a, b
  • x 4
  • y 3 x
  • c A
  • a 3.1415
  • a 1.5 a

21
Grouping a Sequence of Expressions
  • int x, y
  • char c
  • float a, b
  • x 4
  • y 3 x
  • c A
  • a 3.1415
  • a 1.5 a

22
Control Structures Selection
  • Selection, or branch statements, chose one set of
    instructions among 2 or many to execute
    conditional on some test.
  • In C selection control structures are
  • if
  • if else
  • switch

23
Relational and Boolean Operators
  • C relational an boolean operators basis of
    conditional tests for selection control
    structures

Relational Operators
Logical (Boolean) Operators
24
Selection if and if .. else
  • if (expression)
  • statement
  • if (expression)
  • statement1
  • else
  • statement2

25
Compound (Block of) statements
  • A compound statement groups a sequence of
    expressions
  • if (age gt 18)
  • cout ltlt Eligible to vote. ltlt endl
  • cout ltlt No longer a minor. ltlt endl
  • else
  • cout ltlt Not eligible to vote. ltlt endl
  • cout ltlt Still a minor. ltlt endl

26
Multiple selections Nested if
  • if (balance gt 50000.00)
  • interestRate 0.07
  • else if (balance gt 25000.00)
  • interestRate 0.05
  • else if (balance gt 1000.00)
  • interestRate 0.03
  • else
  • interestRate 0.00

27
Multiple Selections switch statements
  • power to choose from among many alternatives
  • switch (grade)
  • case A
  • cout ltlt The grade is A.
  • break
  • case B
  • cout ltlt The grade is B.
  • break
  • case C
  • cout ltlt The grade is C.
  • break
  • case D
  • cout ltlt The grade is D.
  • break
  • case F
  • cout ltlt The grade is F.
  • break

28
Quick Quiz
  • Give an integer variable named size
  • int size
  • Write a relational expression to test that
    size is between the values of 5 and 10
    (inclusive).
  • Write a selection statement that, when size is
    greater than 10 you will output the size
    multiplied by 2, otherwise when it is less than
    or equal to 10 you output an error message saying
    size is too small.
  • Given a character variable
  • char grade
  • Write a switch statement that prints out Good
    Job if the grade is an A or a B, prints out
    Average Job if the grade is a C and prints out
    You need to do better if the grade is a D or F.
  • Determine True or False for each of the
    expressions given
  • int a10, b5
  • char cX
  • (a gt b) (b3 gt a)
  • c lt Z
  • a ! (2 b)
  • (a lt b) (b lt 10)

29
Quick Quiz
  • Give an integer variable named size
  • int size
  • Write a relational expression to test that
    size is between the values of 5 and 10
    (inclusive)
  • if ((size gt5) (size lt 10))
  • cout ltlt size is in range ltlt endl

30
Quick Quiz
  • Write a selection statement that, when size is
    greater than 10 you will output the size
    multiplied by 2, otherwise when it is less than
    or equal to 10 you output an error message saying
    size is too small.
  • if (size gt 10)
  • cout ltlt 2 size ltlt endl
  • else
  • cout size is too small ltlt endl

31
Quick Quiz
  • Given a character variable
  • char grade
  • Write a switch statement that prints out Good
    Job if the grade is an A or a B, prints out
    Average Job if the grade is a C and prints out
    You need to do better if the grade is a D or F.
  • switch (grade)
  • case A
  • case B
  • cout ltlt Good Job ltlt endl
  • break
  • case C
  • cout ltlt Average Job ltlt endl
  • break
  • case D
  • case F
  • cout ltlt You need to do better ltlt endl
  • default
  • cout ltlt Unknown letter grade ltlt grade ltlt
    endl

32
Quick Quiz
  • Determine True or False for each of the
    expressions given
  • int a10, b5
  • char cX
  • (a gt b) (b3 gt a)
  • c lt Z
  • a ! (2 b)
  • (a lt b) (b lt 10)

33
Why is repetition needed?
  • cin gtgt num1 gtgt num2 gtgt num3 gtgt num4 gtgt num5
  • int sum num1 num2 num3 num4 num5
  • int average sum / 5
  • vs.
  • int sum 0
  • int num
  • for (int i0 i lt 5 i)
  • cin gtgt num
  • sum sum num
  • int average sum / 5

34
Control Structures Repetition
  • Repetition is a common activity in algorithms,
    and very powerful
  • Repetition is another way in which we get away
    from the simple sequential execution of a
    sequence of statements
  • C looping structures
  • while
  • for

35
While looping structure
  • while (expression)
  • statement
  • i 0
  • while (i lt 20)
  • cout ltlt i ltlt
  • i i 5
  • cout ltlt endl

36
While looping structure
flag-controlled found false while
(!found) if (expression) found
true EOF-controlled cin gtgt variable while
(cin) cin gtgt variable
  • counter-controlled
  • counter 0
  • while (counter lt N)
  • counter
  • sentinel-controlled
  • cin gtgt variable
  • while (variable ! sentinel)
  • cin gtgt variable

37
for looping structure
  • while loop is general enough to implement most
    (all?) forms of repetition
  • for loop is a specialized form to simplify
    writing of count-controlled loops (since they are
    so common).

38
for looping structure
  • for (initial statement loop condition update
    statement)
  • statement
  • for (i1 ilt5 i)
  • cout ltlt Hello! ltlt endl
  • cout ltlt ltlt endl

39
Quick Quiz
  • Write a for loop that prints out the squares of
    the first 10 integers (square of 1 is 1 1,
    square of 5 is 5 5, etc.)
  • Write a while loop that inputs an integer from
    the user then prints it out and keeps doing this
    until the user enter -999 as the number.

40
Quick Quiz
  • Write a for loop that prints out the squares of
    the first 10 integers (square of 1 is 1 1,
    square of 5 is 5 5, etc.).
  • for (int i1 ilt10 i)
  • cout The square of ltlt i ltlt is
  • ltlt i i ltlt endl

41
Quick Quiz
  • Write a while loop that inputs an integer from
    the user then prints it out and keeps doing this
    until the user enter -999 as the number.
  • int num 0
  • while (num ! -999)
  • cout ltlt Enter a number (or -999 to stop)
  • cin gtgt num
  • cout ltlt You entered ltlt num ltlt endl

42
Functions
  • A C Program (or any program in a procedural
    programming language)
  • Collection of 1 or more subprograms called
    functions
  • Which is a collection of statements that can
    accomplish something useful
  • sequence, selection, repetition
  • Special function called main

43
User-Defined Functions
  • To solve a problem you must learn to write your
    own functions
  • User-defined functions in C are classified into
    two categories
  • Functions that have a data type, called
    value-returning functions
  • Functions that do not have a data type, called
    void functions.

44
Value-Returning Functions
  • functionType functionName(formal parameter List)
  • statements
  • return value
  • int min(int number1, int number2)
  • if (number1 lt number2)
  • return number1
  • else
  • return number2

45
Value-Returning Functions
  • Once a value-returning function computes the
    value, the function returns this value via the
    return statement.
  • When a return statement executes in a function,
    the function immediately terminates and returns
    control back to the caller.
  • A value-returning function must return a value,
    therefore it must have at least 1 return
    statement somewhere in it.

46
Non Value-Returning Functions
  • called void functions
  • void functionName(formal parameter List)
  • statements
  • void printStars()
  • cout lt ltlt end
  • cout lt ltlt end

47
Reference Parameters
  • Normal simple data type parameters in C and C
    are passed by value.
  • Sometimes it is useful to pass in parameters by
    reference.
  • When you want to return more than one value from
    a function.
  • When the value of the actual parameter needs to
    be changed.
  • When passing the address would save memory space
    and time relative to copying a large amount of
    data.

48
Reference Parameters
  • Value parameter A formal parameter that receives
    a copy of the content of the corresponding actual
    parameter.
  • Reference parameter A formal parameter that
    receives the location (memory address) of the
    corresponding actual parameter.

49
Reference Parameters
  • int main()
  • int courseScore
  • cout ltlt This program computes the course
    grade.
  • ltlt endl
  • getScore(courseScore)
  • printGrade(courseScore)
  • return 0
  • void getScore(int score)
  • cout ltlt Enter the course score
  • cin gtgt score
  • cout ltlt endl ltlt Course score is ltlt score ltlt
    endl

50
Quick Quiz
  • Write a function that takes and integer as a
    (value) parameter, and returns the integer
    multiplied by 5 as its result.
  • Write a function that takes 2 floating point
    values as parameters and returns the smaller of
    the two as its result.

51
Quick Quiz
  • Write a function that takes and integer as a
    (value) parameter, and returns the integer
    multiplied by 5 as its result.
  • int multByFive(int num)
  • return num 5

52
Quick Quiz
  • Write a function that takes 2 floating point
    values as parameters and returns the smaller of
    the two as its result.
  • float min(float f1, float f2)
  • if (f1 lt f2)
  • return f1
  • else
  • return f2

53
Next Class
  • We are reviewing a bit the structured data type
    arrays.
  • Read the Materials from Ch 9. p 423-450.
  • How are arrays and for loops (repetition) related?
Write a Comment
User Comments (0)
About PowerShow.com