Lab Session-VI CSIT-120 Fall 2000 - PowerPoint PPT Presentation

About This Presentation
Title:

Lab Session-VI CSIT-120 Fall 2000

Description:

Next, we complete the lab session-V. Lab session-VI deals with functions (OPTIONAL) ... A car can hold 12 gallons of gasoline and it can travel 360 miles ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 30
Provided by: sunyfr
Category:
Tags: csit | fall | lab | session

less

Transcript and Presenter's Notes

Title: Lab Session-VI CSIT-120 Fall 2000


1
Lab Session-VI CSIT-120 Fall 2000
  • Let us look at C syntax rules in brief
  • Next, we complete the lab session-V
  • Lab session-VI deals with functions (OPTIONAL)
  • We will learn how to write independent functions
    that can be called from main()
  • We do not use parameter passing, however, we
    learn how functions can return values
  • Lab VI Continues (FINAL SESSION)

2
C Syntax Rules in Brief
  • include ltiostream.hgt
  • ..other include files go here
  • void main(void)
  • Declarations of variables and constants
  • Assignment and control statements

3
C Syntax Rules in Brief
  • Make it a habit to put declarations in the
    beginning. For example,
  • int employeeID
  • const int current-year2000
  • float deduction-amount
  • Once a data item has been declared, do not
    mention its data type in assignment statement
  • int employeeID23 is WRONG
  • employeeID23 is RIGHT

4
C Syntax Rules in Brief
  • If you write a control statement, its conditions
    must be in parenthesis with no semicolon after
    the parenthesis
  • For example, displaying employeeID if employeeID
    is 24
  • if (employeeID 24)
  • coutltltID is ltltemployeeIDltltendl
  • Please note double-equal in comparing, no
    semicolon after comparison statement and joining
    several displayable messages with ltlt

5
C Syntax Rules in Brief
  • Different comparison statements
  • if (employeeID ! 24) coutltltendl
  • (if employeeID is not equal to 24, do a blank
    cout)
  • if (employeeID gt 24)
  • if (employeeID lt24)
  • if (employeeID gt 24)
  • if (employeeID lt 24)
  • We could also use an else clause to capture the
    false results of comparison

6
C Syntax Rules in Brief
  • For example, if employeeID is 24, display ID is
    24 else do a blank line display
  • if (employeeID 24)
  • coutltltID is ltltemployeeIDltltendl
  • else coutltltendl
  • Use braces to show blocks of code even if only a
    single statement is there in if part or else part
    (for clarity purposes)

7
C Syntax Rules in Brief
  • In assigning values, there can be only one
    destination shown on left of the single equal
    sign
  • deductions pay- (paytax-percentage)
  • Please note the use of parenthesis to emphasize
    and make the expression very clear

8
The for Statement
  • for is a loop statement that is controlled
    through a loop control variable
  • for (lcv1 lcvlt100 lcv)
  • The above loop will start with lcv1 and it will
    run until lcv equals 100. The step size is 1
    (lcv)
  • Compare it to the above
  • for (lcv1 lcvlt100 lcv2)

9
The for Statement
  • for statement and while statement can produce
    identical loops
  • for is preferred if the number of iterations is
    known beforehand. while is preferred if the
    total iterations cannot be computed in advance

10
Logical Operators
  • Logical AND is represented by
  • Logical OR is represented by
  • Logical NOT is represented by !
  • Comparing if equal is represented by
  • Example Write a logical expression to be true if
    x is restricted between 1 and 100
  • Write a logical expression to check if x equals
  • y-50 and x is positive
  • Experiment 5.5

11
Lab Exercise
  • Develop a program that takes a number from the
    user and then enters a loop to keep displaying
    the result of successively multiplying the number
    by 2. The loop is terminated when the number
    exceeds half of INT_MAX (Do not forget to include
    the header file limits.h)
  • Example 2,4,8,16,32,64,..

12
Functions
  • Functions are subroutines, procedures,
    modules or program units.
  • Functions perform a given task, usually one
    function is dedicated to one task
  • One advantage of using functions is to avoid
    repeated coding for the same job

13
Functions Reduce Code Size
  • For example, consider a program that prints text
    in a box, something like below

  • The Journey Through Central Spine

  • We can achieve this printout with a program
    containing 9 cout statements OR we can develop a
    function to print aesterik lines

14
Program with 9 cout Lines
  • coutltltltlt
    endl
  • coutltltltlt
    endl
  • coutltltltlt
    endl
  • coutltlt
    ltltendl
  • coutltlt The Journey Through Central Spine
    ltltendl
  • coutltlt
    ltltendl
  • coutltltltlt
    endl
  • coutltltltlt
    endl
  • coutltltltlt
    endl.

15
Same Program with a Function
  • Print_three_star_lines()
  • coutltlt
    ltltendl
  • coutltlt The Journey Through Central Spine
    ltltendl
  • coutltlt
    ltltendl
  • Print_three_star_lines()
  • Now the program is more compact and readable.
    Actual function definition will be developed
    after the main() functions code
  • DEMONSTRATION OF PROGRAM

16
Function Specific Actions
  • If you plan to work with functions, make sure
    that you name your functions according to C
    naming conventions
  • (letters, underscores, digits, no digits in the
    beginning)
  • As demonstrated, function data type is specified
    before its name. If the function does not return
    anything, void is used
  • Also note the DECLARATION before the actual
    function code, ending with a

17
Function Call
  • Notice the function name is simply repeated when
    it is called from the main routine
  • print_three_star_lines()
  • The empty parenthesis indicate the fact that no
    data is passed to this function. A semicolon is a
    must to terminate any C statement (except loop
    starters)

18
The Events
  • When a function is called from the main routine,
    the following events take place
  • Control (of the running program) is transferred
    to the function as well as any data that may be
    needed
  • Function performs its task and at the end,
    transfers the control back to the calling routine

19
Why does the Function name appear so many times?
  • Count the number of times the function name is
    repeated in our program
  • The name appears each time for a different
    purpose
  • Firstly we have to declare the function. Function
    declaration is called its prototype
  • void print_three_star_lines()

20
Why does the Function name appear so many times?
  • Next, we have to define the function,i.e. its
    actual source code
  • void print_three_star_lines() .
  • Finally, we have to make the function call
  • print_three_star_lines()
  • What do you know about main function when it
    reads void main().?

21
Lab VI Continued
  • We look at some C code segments
  • We solve one programming problem
  • Lab Assignment5 due 12/7

22
Basic Concepts
  • What will be the output? Dont run this code,
    just predict
  • void main()
  • int num1,num2
  • num112345 num26789
  • coutltltnum1ltltnum2ltltendl

23
Basic Concepts
  • What is wrong with this code segment?
  • void main()
  • int num1, myloop
  • while (mylooplt24)
  • coutltltOnce in myloop, show no mercyltltendl

24
Basic Concepts
  • What is wrong with this code segment?
  • Void main()
  • int mybirthdate
  • mybirthdate 2
  • while (mybirthdatelt24)
  • coutltltEnter my birth date (date only)ltltendl
  • cingtgt mybirthdate
  • coutltltyou got it\n

25
Basic Concepts
  • What is the error in this code segment?
  • int cans
  • for(int cans1 canslt100 cans)
  • coutltltEach one has a refund of 5 cents\n
  • coutltltcount until 100 cans are done
  • cans--

26
Basic Concepts
  • Fix this code segment
  • int k
  • coutltltNumber please cingtgtk
  • if (k5) coutltltYou typed 5
  • else coutltltNothing else please

27
Lab Programming Exercise
  • Write a program that accepts a digit entered by a
    user and then displays it in words.
  • For example, user enters 8, program displays,
    You Entered Eight
  • User enters 0, program displays You Entered
    Zero
  • and so on..

28
Lab Assignment5 Due 12/7
  • A car can hold 12 gallons of gasoline and it can
    travel 360 miles without refuelling. Write a
    program the displays the trip meter of the car
    followed by amount of fuel left. Every time the
    user enters letter r, 10 miles are added to the
    trip meter and appropriate amount of fuel is
    deducted. As the amount of fuel becomes 1 gallon,
    the program prints a warning.

29
Sample Output
  • s
  • Miles so far 160 Fuel left 6.66667
  • s
  • Miles so far 160 Fuel left 6.66667
  • r
  • Miles so far 170 Fuel left 6.33333
  • r
  • Miles so far 180 Fuel left 6
Write a Comment
User Comments (0)
About PowerShow.com