CSC 215 - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

CSC 215

Description:

Source code should be (grader) friendly. Object code should ... Watch out for extra semi-colons!! break. The books says that you can use a break to end a loop. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 39
Provided by: ericse
Category:
Tags: csc | colons | semicolons

less

Transcript and Presenter's Notes

Title: CSC 215


1

CSC 215 Introduction to Programming with
C Instructor Eric Sedgwick
2
HW
  • Distribute grades
  • automated mailing
  • disagree with grading? - email me
  • wrong email? - email grader
  • Comment must include your name.
  • Submission
  • to ctigrader1_at_hotmail.com
  • cc esedgwick_at_cs.depaul.edu
  • subject CSC215HWn
  • Did you get a receipt?

3
Last Time
  • Calculations
  • variables/types/memory
  • type checking
  • Branching
  • if-else
  • if
  • nested if-else
  • Looping
  • while ()
  • do while ()

4
This time
  • Programming goals (I.e., receiving a good score
    on your hw)
  • More flow control (Chapter 7)
  • Multiway branching - switch
  • More loops - for()
  • Functions (Chapter 3)
  • predefined
  • user defined?

5
Programming goals
  • Turn in assignment complete and on time
  • Source code
  • Perform task correctly and efficiently
  • Code should compile w/o errors or warnings
  • More than one right method.
  • Test your program
  • Source code should be (grader) friendly
  • Object code should be (user) friendly
  • I/O goals see end of Lecture 1

6
Friendly source code
  • Header comment
  • Including a description of the program
  • your contact info
  • Comments
  • Include comments with boolean/logical expressions
  • Comment anything that is hard to understand
  • Comment anything that is ambiguous
  • Style (follow what the book does)
  • Whitespace group related statements, separate
    unrelated ones
  • Indentation indent loop and if-else bodies
  • Braces put them on new lines and indent body
  • Constants use them.
  • Meaningful names for variables and constants

7
Multiway branching
  • (Chapter 7)

8
switch statement
  • To perform multiway branching
  • switch (controlling_expression)
  • case Constant_1
  • Statement_Sequence_1
  • break
  • case Constant_2
  • Statement_Sequence_2
  • break
  • case Constant_n
  • Statement_Sequence_n
  • break
  • default
  • Default_statement_sequence
  • Computer
  • Evaluates controlling expression
  • Matches the value with the constants.
  • Starts executing after matching constant.
  • Terminates switch when a break statement is
    encountered
  • Executes default if no constant is matched.

9
switch statement
  • weight3.cpp -gt weight4.cpp
  • Change user input to use a switch

10
switch statement
  • controlling_expression should evaluate to a char
    or int (or enum or bool)
  • Constants must be char or int constants (no
    variables allowed)
  • Dont forget breaks, otherwise subsequent cases
    are also executed.
  • Group cases without breaks, when constants are
    equivalent. E.g,
  • Case y
  • Case Y
  • Statement_sequence
  • break
  • Include a default case.

11
switch or if-else
12
Exercise
  • Using a switch, write a program that lets the
    user input any one of the first 4 letters of the
    alphabet (upper or lowercase) and informs the
    user which letter of the alphabet it is.
  • Sample output
  • Please enter a letter c
  • c is the third letter of the alphabet.

13
for loop
  • for(initialization boolean_expression
    update_action)
  • body_of_loop
  • Explanation
  • Initialization - initializes variables that are
    used in boolean_expression
  • Boolean_expression test for executing loop
  • Update_action executed at the end of the loop
    body
  • Body_of_loop - statements to be executed if
    boolean_expression is truethis can be a single
    statement or a compound statement using .
  • Example
  • n5
  • for(count1 count lt n count)
  • cout ltlt count
  • Output
  • 1 2 3 4 5

14
for loop
  • Typically used for counting
  • Maybe counting by 2s, 5s or in a more
    complicated way
  • sum.cpp
  • Modify to use a for-loop
  • Should use for loops for your HW

15
for loop pitfall
  • What is the output of this code?
  • for (count0countlt4count)cout ltlt count
  • Between the for and the is an empty statement
  • Watch out for extra semi-colons!!

16
break
  • The books says that you can use a break to end a
    loop.
  • Do not do this in this class.
  • Only rarely outside of this class.
  • In this class, use only for ending cases in a
    switch.

17
for, while or do-while ?
  • for( )
  • Typically used for counting with int variables
  • 1,2,3, 5,8,11, 99,97,95,
  • while ( )
  • More general.
  • do while( )
  • More general, always executes at least once.
    Often used for user input.

18
Exercises
  • What is the output?
  • int i,x0
  • for(i5igt0i--)
  • xii
  • cout ltlt x
  • Write a for loop that sums the even integers less
    than a given integer n.

19
Nested for loops
  • What is the output?
  • n3
  • sum 0
  • for(i1 iltn i)
  • for(j1 jlti j)
  • sum j
  • cout ltlt The sum is ltlt sum ltlt endl

20
Exercise
  • Write a program that allows the user to input the
    length of the side of square and then outputs a
    square of that size made up of s.
  • Example
  • Please enter the size of the square 5
  • Here is your 5 by 5 square
  • square.cpp

21
Exercise
  • Write a program that allows the user to input the
    length of the side of triangle and then outputs a
    triangle with of that size made up of s.
  • Example
  • Please enter the length of the triangle 5
  • Here is your triangle
  • triangle.cpp

22
Exercise
  • How do you modify the previous program so that it
    does this
  • Please enter the length of the triangle 5
  • Here is your triangle

23
Top-down design
  • Most programs are too complicated to sit down and
    write at the computer.
  • First write the steps of your program in English
    and then refine the steps until it can be easily
    written in C.

24
Class exercise Write a program that specifies
how to share pizzas
  • How many slices does your pizza have? 8
  • 1 people can eat 8 slices
  • 2 people can eat 4 slices
  • 4 people can eat 2 slices
  • 8 people can eat 1 slices
  • How many slices does your pizza have? 5
  • 1 people can eat 5 slices
  • 5 people can eat 1 slices
  • Decide on the algorithm first !!

User input
25
Top-down design
  • First, specify the big tasks the program must
    accomplish.
  • Then break each of these into subtasks, and these
    into subtasks,
  • When you know how to accomplish each subtask, you
    have an Algorithm.
  • Make the subtasks functions

26
Functions
  • Functions aid top-down design.
  • Functions separate subtasks from the main
    program.
  • Functions allow a section of code to be called
    repeatedly (and from different places in your
    program.)
  • Example an algorithm for computing grades might
    look like
  • For each student in the class
  • Compute the hw average
  • Compute the course average
  • Compute the letter grade
  • Output the final grade
  • (The steps 1-4 can be made into functions.)

27
Predefined functions
  • root.cpp
  • Example sqrt() the square root function
  • double root
  • rootsqrt(9.0) // root 3.0
  • Terminology
  • sqrt(9.0) is a function call (or invocation)
  • 9.0 is an argument to the function
  • The value computed (in this case 3.0) is called
    the value returned by the function.

28
functions
  • Arguments can be expressions. Argument is
    evaluated and then function is called. E.g.,
  • hypotenuse sqrt(basebase heightheight)
  • Functions can be used in expressions. Function
    is called and then expression evaluated.
  • Temperature 99.0 sqrt(wind_chill)
  • Be careful about types. Arguments and return
    values are of a specified type. The argument to
    sqrt() is a double and it returns a double.
  • int int_var
  • int_var sqrt(9.0) // compiler not happy

29
Predefined functions
  • Need an include directive and namespace. (This
    is different from 2nd edition) E.g.,
  • include ltcmathgt
  • include ltcstdlibgt
  • using namespace std
  • For more functions see p. 111 and Appendix 4.
  • cmath
  • x sqrt(9.0) // x 3.0
  • x pow(2.0,3.0) // x 8.0
  • x fabs(-23.0) // x 23.0
  • cstdlib
  • x abs(-23) // x 23

30
Functions that return a value
  • Todays functions all return a value. (Later we
    will see void functions that do not return
    values)
  • You must do something with that value. For
    example
  • root sqrt(7.0) // remember the value
  • cout ltlt the square root of 7 is ltlt sqrt(7.0)
  • // print it out
  • salary 34 sqrt(7.0) // use it in a calc
  • I.e., This is legal but useless
  • sqrt(7.0) // value thrown away
  • Calls to functions that return values should
    always have something on their left.

31
Type casting
  • C has type casting (type changing) functions
    that explicitly convert from one type to another.
    E.g.,
  • x double(3) // x 3.0
  • i int(3.7) // i 3

32
Programmer designed functions
  • A small program to perform a subtask. Aids in
    top-down design.
  • Example sumsquares.cpp
  • To define a function, supply
  • Function prototype before main, specifies the
    types of arguments and return values
  • Function definition after main, specifies how
    to computer the function
  • To use function, must make function call

33
function prototype
  • Before main
  • return_type function_name(parameter_list)
  • // comment on what function does
  • Example
  • int square(int number)
  • // computes the square of a number

34
function definition
  • After main, the definition. It computes and
    returns answer. return_type
    function_name(parameter_list)
  • variable_declarations
  • executable_statements // computations
  • return answer // at least one
  • The names in parameter list are called formal
    parameters, they are placeholders for values
    given to the function.

35
Formal parameters, the return and statement and
executing functions by hand
  • Function call substitutes value
  • s square(5)
  • for formal paramater. I.e., is like executing
  • int number5
  • The return statement
  • return numbernumber
  • replaces the function call with the returned
    value, I.e., like
  • s square(5) 25

36
volume.cpp
37
Class Exercise
  • Write a function that computes the number of
    divisors of a given number.
  • Write a program that generates all prime numbers
    less than a user supplied number.
  • Harder Write a program that generates the first
    n primes, where n is supplied by the user.

38
Next time
  • Functions and top down design
  • more user defined functions
  • void functions
  • call by reference
Write a Comment
User Comments (0)
About PowerShow.com