Introduction to Programming in C - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Introduction to Programming in C

Description:

initialize_number_generator: Initializes the random * * number generator using ... When the guess is correct, prints the total number of guesses and returns. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 17
Provided by: drjames82
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming in C


1
Chapter 10
  • Introduction to Programming in C

2
Variables
  • Three types, depending upon where they are
    declared.
  • - Local variables Variables declared inside a
    function
  • - Formal parameters Variables declared in the
    definition of a function
  • - Global variables Variables declared outside
    of all functions.

3
Variables
  • - Scope the scope of a variable is the
    portion of the program text in which the variable
    can be referenced.
  • - Local variables have a block scope. It is
    visible from its point of declaration to the end
    of the enclosing function body.
  • - Automatic storage duration The portion of
    the program in which a local variable exists. A
    local variable doesnt retain its value when the
    function ends.

4
Variables use of static
  • - Putting the word static in the declaration of
    a local variable causes it to have static storage
    duration instead of automatic storage duration.
  • - A variable declared in this manner retains
    its value when the function ends.
  • - Example
  • void f(void)
  • static int i
  • - When the function ends, i doesnt lose its
    value.

5
Global Variables
  • - Referred to in our textbook as external
    variables
  • - Declared outside the body of any function
  • - They ALL have static storage duration
  • - The have file scope, which is to way that
    their scope is from the beginning to the end of
    the enclosing file

6
Scope Rules
  • main()
  • int i, j
  • i 10
  • j 100
  • if (j gt 0)
  • int i
  • i j/2
  • printf( "inner i d\n" ,i)
  • printf( outer i d\n" ,i)
  • return 0

7
Scope Rules
  • main()
  • int i, j
  • i 10
  • j 100
  • if (j gt 0)
  • int i
  • i j/2
  • printf( "inner i d\n" ,i)
  • printf( outer i d\n" ,i)
  • return 0

inner i 50 outer i 10
8
Scope Rules
  • include ltstdio.hgt
  • int i
  • void f(int i)
  • i 1
  • void g(void)
  • i 2

main() int i for (i 1
i lt 5 i ) f(i) g()
if (i 2) int i
printf("i 2\n")
9
Stack
  • A stack is a data structure which stores multiple
    data items of the same type.
  • We can push an item onto the stack, or pop the
    most recently added item from the stack.
  • push(5)
  • push(6)
  • push(27)
  • push(143)
  • x pop()

143
27
6
5
10
External or Global Variables - Example
  • define STACK_SIZE 100
  • int contentsSTACK_SIZE
  • int top 0
  • void push(int i)
  • if (top STACK_SIZE
  • printf("Error stack is full.\n")
  • return 0
  • contentstop i
  • int pop(void)
  • if (top 0)
  • printf("Error stack is empty.\n")
  • return 0
  • return contents--top

11
Pros and Cons of External Variables
  • In most cases it is better for functions to
    communicate through use of parameters rather than
    by shared variables
  • If we change an external variable during program
    modification we need to check every function to
    see how it is changed
  • If an external variable is assigned an incorrect
    value it may be difficult to identify which
    function did it
  • Functions that rely on external variables are
    difficult to use in other programs. A function
    should be self-contained.

12
The Guessing Game
  • / guess.c Asks user to guess a hidden number /
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • include lttime.hgt
  • define MAX_NUMBER 100
  • int secret_number
  • void initialize_number_generator(void)
  • void choose_new_secret_number(void)
  • void read_guesses(void)
  • main()
  • char command
  • printf("Guess the secret number between 1 and
    d.\n\n", MAX_NUMBER)
  • initialize_number_generator()
  • do
  • while (command 'y' command 'Y')
  • return 0

13
The Guessing Game
  • main()
  • char command
  • printf("Guess the secret number between 1 and
    d.\n\n", MAX_NUMBER)
  • initialize_number_generator()
  • do
  • choose_new_secret_number()
  • printf("A new number has been chosen.\n")
  • read_guesses()
  • printf("Play again? (Y/N) ")
  • scanf(" c", command)
  • printf("\n")
  • while (command 'y' command 'Y')
  • return 0

14
The Guessing Game
  • /
  • initialize_number_generator Initializes the
    random
  • number generator
    using
  • the time of day.

  • /
  • void initialize_number_generator(void)
  • srand((unsigned) time(NULL))
  • /
  • choose_new_secret_number Randomly selects a
    number
  • between 1 and
    MAX_NUMBER and
  • stores it in
    secret_number.

  • /
  • void choose_new_secret_number(void)
  • secret_number rand() MAX_NUMBER 1

15
The Guessing Game
  • /
  • read_guesses Repeatedly reads user guesses and
    tells the user whether each guess is too low too
    high, or correct. When the guess is correct,
    prints the total number of guesses and returns.

  • /
  • void read_guesses(void)
  • int guess, num_guesses 0
  • for ()
  • num_guesses
  • printf("Enter guess ")
  • scanf("d", guess)
  • if (guess secret_number)
  • printf("You won in d guesses!\n\n",
    num_guesses)
  • return
  • else if (guess lt secret_number)
  • printf("Too low try again.\n")
  • else
  • printf("Too high try again.\n")

16
Program 10
  • Write a program which will prompt the user for an
    integer value n, and then return the sum of the
    first n odd integers. The program will use a
    recursive function to find the sum of the first n
    odd integers.
  • Homework
  • P. 203, 2
Write a Comment
User Comments (0)
About PowerShow.com