C Programming COSC 1304 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

C Programming COSC 1304

Description:

Memory location for a value that will be used by the program. Used for substitution ... Memory location to store what user typed ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 29
Provided by: temekori
Category:
Tags: cosc | programming

less

Transcript and Presenter's Notes

Title: C Programming COSC 1304


1
C Programming COSC 1304
  • Week 2
  • Review
  • Structured Program Development

2
Review What is C?
  • High-Level Programming Language
  • Hardware Independent
  • Function-oriented
  • Building-block approach
  • Underlying structure for object-oriented
    programming languages like C and Java

3
C Standard Library Functions
  • Software Reusability
  • Avoid reinventing the wheel
  • Functions exist for handling standard input,
    standard output, processing of system, character
    manipulation and comparison, math, etc.
  • Found in Appendix B

4
Phases of Execution
  • Edit
  • Program is typed and saved as a file with a .c
    extension (i.e. vi, pico, or emacs, Notepad,
    Symantec)
  • Preprocess
  • Obeys commands before compilation
  • Includes other files in source files to be
    compiled
  • Compile
  • Program is translated into machine language code
  • cc program.c
  • Link
  • Combines the object code with code for missing
    functions to produce an executable image
  • If compiling and linking are accurate, a.out is
    created
  • Load
  • The executable image is taken from disk and
    placed into memory before execution
  • Execute
  • Program is executed one instruction at a time
    under control of its CPU

5
Parts of the C Program
  • Preprocessor directive
  • main()
  • Curly braces
  • Semicolons at end of statement
  • Variable declarations
  • comments

6
Example 1
  • include ltstdio.hgt
  • /This program prints out a message on the
    screen./
  • main()
  • printf(Welcome to the COSC 1304.\n)
  • return 0

7
Variable Declaration
  • Memory location for a value that will be used by
    the program
  • Used for substitution
  • Case-sensitive
  • Consists of letters, digits, underscores
  • Can not begin with a digit
  • Datatype and variable name
  • int grd_avg

8
ltstdio.hgt - printf function
  • Prints to the standard output (terminal or
    screen)
  • Calls to library function of stdio.h (standard
    input/output header file)
  • Uses escape character (\)
  • Escape sequences (\n)
  • \n newline \a alert
  • \t tab \\ backslash
  • \r carriage return \ double quote

9
Example 2
  • include ltstdio.hgt
  • /This program prints out a message on the screen
    and declares variables./
  • main()
  • int grd_avg
  • int grd_midterm
  • int grd_lab, grd_final
  • printf(Welcome to the COSC 1304.\n)
  • return 0

10
ltstdio.hgt - scanf function
  • Wait for input from the user through standard
    input (keyboard, mouse)
  • Calls to library function in stdio.h standard
    input/output header
  • scanf( d,grd_project)
  • Format control string
  • Memory location to store what user typed
  • Common errors to forget the in the scanf
    function and place the in the printf function

11
Example 3
  • include ltstdio.hgt
  • /This program prints out the average grade based
    on the values provided in 3 variables./
  • main()
  • int grd_lab,grd_midterm, grd_final
  • float grd_avg
  • printf(Enter the lab grade.\n)
  • scanf(d,grd_lab)
  • printf(Enter the midterm grade.\n)
  • scanf(d,grd_midterm)
  • printf(Enter the final grade.\n)
  • scanf(d,grd_final)
  • grd_avg (grd_lab grd_midterm grd_final)/3
  • printf(The grade point average is
    .2f\n,grd_avg)
  • return 0

12
Algorithms
  • Guide to solving a problem with a series of
    actions
  • Outlines an ordered set of actions that need to
    be executed to have the desired result
  • The ordered set of actions is referred to as
    program control in programming

13
Pseudocode
  • Informal and artificial language to develop
    algorithms
  • Helps the programmer think out a program before
    attempting to write it
  • Describes executable statements that are
    performed when the program is converted from
    pseudocode to Java and is run
  • Use flowchart as graphical representation of the
    algorithm
  • Rectangle (action)
  • Oval (Begin and End)
  • Diamond (decision)

14
Comparison Operators
  • Equality
  • ! Not equal
  • gt Greater than
  • lt Less than
  • gt Greater than or equal to
  • lt Less than or equal to

15
Selection Structures
  • If
  • Single-selection structure
  • Performs an action if condition is true
  • Skips an action if condition is false
  • If/else
  • Double-selection structure
  • Selects between multiple different actions
  • Performs an action if a condition is true and
    performs a different action if condition is false

16
If structure
  • Single-entry/single-exit
  • Easy to build programs
  • Control structures are attached to one another by
    connecting the exiting pont of one control
    structure to the entry point of the next

17
Flowchart if statement
Begin
Is Lab Grade gt65?
Print a message like Student passing
True
False
End
18
Example if statement
  • include ltstdio.hgt
  • /This example demonstrates the if structure/
  • main()
  • int grd_lab
  • printf(Please enter the lab grade of
    student.\n)
  • scanf(d,grd_lab)
  • if (grd_lab gt65)
  • printf(Great..Student Passing.)
  • return 0

19
Example nested ifs
  • include ltstdio.hgt
  • /This example demonstrates the if structure/
  • main()
  • int grd_lab,grd_midterm
  • printf(Please enter the lab grade for
    student.\n)
  • scanf(d,grd_lab)
  • if (grd_lab gt65)
  • printf(Great..Student Passing.)
  • printf(Please enter the midterm grade.\n)
  • scanf(d,grd_midterm)
  • if (grd_midterm lt65)
  • printf(Student is in trouble. Must try
    harder.)
  • return 0

20
If/else structure
  • Performs an indicated action only when the given
    condition evaluates to true otherwise action is
    skipped
  • Can specify a different action to perform when
    the condition is true rather than when the
    condition is false
  • Can be nested for multiple cases
  • ? (conditional operator) can be used in this
    structure as it takes three operands
  • Boolean expression
  • Value for the expression if the condition
    evaluates to true
  • Value for the expression if the condition
    evaluates to false

21
Flowchart If/Else structure
Begin
Lab Grade gt65?
Print a message like Student passing
Print a message Like Student is Failing
False
True
End
22
Example If/Else
  • include ltstdio.hgt
  • /This example demonstrates the if structure/
  • main()
  • int grd_lab
  • printf(Please enter the lab grade of
    student.\n)
  • scanf(d,grd_lab)
  • if (grd_lab gt65)
  • printf(Great..Student Passing.)
  • else
  • printf(Uh oh..Student is failing.)
  • return 0

23
Example Multiple if/else
  • include ltstdio.hgt
  • /This example demonstrates the if structure/
  • main()
  • int grd_lab
  • printf(Please enter the lab grade of
    student.\n)
  • scanf(d,grd_lab)
  • if (grd_lab gt65)
  • printf(Great..Student Passing.)
  • else if (grd_lab gt60)
  • printf(This student is borderline.)
  • else
  • printf(Uh oh..Student is failing.)
  • return 0

24
Repetition Structure
  • Action repeated while some condition remains true
  • At the point condition is no longer met, first
    statement outside of while statement is executed
  • Utilizes mathematical functions to change value
    of repetition variable (loop counter)

25
Arithmetic Examples
  • c c 7
  • d d 10
  • e e 5
  • / f f/5
  • g g 9
  • increment
  • -- decrement

26
Repetition Structure - while
Begin
Num_grades lt4
Num_grades 1 Add grade to sum
True
False
Calculate average
End
27
Example while structure
  • include ltstdio.hgt
  • main ( )
  • int grd_lab, total
  • float avg
  • int num_grades 1
  • while (num_grades lt4)
  • printf(Please enter a lab grade for
    student.\n)
  • scanf(d,grd_lab)
  • total total grd_lab
  • num_grades 1 /num_grades num_grades
    1/
  • avg total / 4
  • printf(The lab assignment average is
    .2f,avg)
  • return 0

28
Example while (initialized at 0)
  • include ltstdio.hgt
  • main ( )
  • int grd_lab, total
  • float avg
  • int num_grades 0
  • while (num_grades lt3)
  • printf(Please enter a lab grade for
    student.\n)
  • scanf(d,grd_lab)
  • total total grd_lab
  • num_grades 1 /num_grades num_grades
    1/
  • avg total / 4
  • printf(The lab assignment average is
    .2f,avg)
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com