Codelab - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Codelab

Description:

You are required to register with them and do assigned, online exercises. ... These 'freebies' make it possible for you to do CodeLab exercises on Day 1, even ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 31
Provided by: ralpht
Category:
Tags: codelab | freebies

less

Transcript and Presenter's Notes

Title: Codelab


1
Codelab
  • A very useful adjunct to this course is Codelab,
    which is run by a group called Turingscraft.
  • You are required to register with them and do
    assigned, online exercises.
  • Codelab keeps track of which exercises you have
    done, the number correct, etc.
  • It starts with very basic exercises and
    progresses from there.
  • Here is what you need to do (as specified by the
    Codelab people)
  • If you are a new user, after you log in for the
    first time, you should click PREFERENCES to
    change your password.
  • PREFERENCES also will let you set a nickname to
    use for your username instead of your email
    address, and will let you set screen size
    dimensions for your CodeLab.

2
Codelab
  • REGISTRATION FOR STUDENTS
  • go to www.turingscraft.com
  • click "register" in the upper right
    corner
  • choose "I am a student in a course ..."
    and click CONTINUE
  • enter the Section Access Code
  • SOUFLO-8287-2420
  • and click CONTINUE
  • continue filling out the forms being
    careful to enter
  • a VALID email address and first and
    last names
  • (these will appear in the
    professor's roster)

3
Codelab
  • Once you register, you will appear on the course
    roster and can log in, access the whole CodeLab
    and submit solutions to up to 10 exercises.
  • To go beyond 10 you need to get full access as
    described below.
  • These "freebies" make it possible for you to do
    CodeLab exercises on Day 1, even though it is
    still the "add/drop" period.
  • LOGIN FOR EVERYONE
  • go to www.turingscraft.com
  • click "login" in the upper right corner
  • the username is the email address given
    during registration
  • the password is the password selected
    during registration
  • GETTING FULL ACCESS
  • log in to CodeLab
  • click LOBBY
  • click the button "Get Full Access"
  • follow the directions

4
Odds and Ends
  • How to write character constants using their
    ascii code
  • \N, where N is an octal constant
  • \xN where N is a hexadecimal constant

5
Odds and Ends
  • Format specifiers for printf
  • Code Format
  • c chard signed decimal integeru unsigned
    decimal integero unsigned octalx unsigned
    hexadecimal, using lowercase charactersX unsigne
    d hexadecimal, using uppercase characters
  • f decimal floating pointe scientific notation
    using lowercase eE scientific notation using
    uppercase Eg uses f or g, whichever is
    shorterG uses f or G, whichever is shorter
  • s string
  • the character
  • Modifiers ld long decimal int hd short
    decimal int Lf long floating pt

6
Formatted Input
  • The scanf function is used for formatted input
  • This means that the function recognizes the type
    of the quantity to be input and automatically
    converts the appropriate character string to the
    binary representation of the value
  • scanf uses format specifiers similar to printf.
  • Examples
  • To read an integer value into int variable i
    scanf("d",i)
  • The use of the will be explained later. i
    effectively means the location assigned to the
    variable i
  • The only time you will not use the notation is
    for strings
  • char name10 scanf("s",name)
  • The reason is that the compiler thinks of an
    array name as its location

7
Formatted Input
  • Format specifiers for scanf
  • Code Format
  • c read a single characterd read a signed
    decimal integeru read an unsigned decimal
    integero read an octal numberx read a
    hexadecimal number
  • f read a floating point number (type
    float)g read a floating point number (type
    float)lf read a floating point number (type
    double)lg read a floating point number (type
    double)Lf read a floating point number (type
    long double)Lg read a floating point number
    (type long double)
  • s read a string (stops at any white-space
    character)
  • Note the function gets() reads a string until a
    newline character is found
  • Also scanf skips over blocks of white-space
    characters

the modifiers l (long) and h (short) may be used
here
8
Functions
  • Functions are the primary means of structuring
    programs in C
  • A function is a named subroutine
  • It accepts a number of arguments, processes
    them, and (optionally) returns a result
  • Functions also may have side effects, like I/O
    or changes to global data structures
  • In C, any subroutine is called a function,
    whether it actually returns a result or is
    only called for its side effect
  • Note A function hides its implementation
  • To use a function, we only need to know its
    interface, i.e. its name, parameters, and
    return type
  • We can improve the implementation of a function
    without affecting the rest of the program
  • Functions can be reused in the same program or
    even different programs, allowing people to build
    on existing code

9
Function Definition
  • A function definition consists of the following
    elements
  • Return type (or void if the function does
    not return a value)
  • Name of the function
  • Parameter list
  • Function body
  • The name follows the same rules as variable names
  • The parameter list is a list of coma-separated
    pairs of the form lttypegt ltnamegt
  • The body is a sequence of statements included in
    curly braces
  • Example
  • int timesX( int number, int x) return
    xnumber

Function Prototype
10
Function Declaration
  • A function declaration consists of the function
    prototype followed by a semi-colon
  • A function may be declared in one place and
    defined later in the code
  • A function declaration (or definition) must be
    declared before any call to the function is used.
  • Of course, the function definition must be found
    somewhere either in the same file or in an
    external file (more later)
  • Example
  • int timesX( int number, int x) function
    declaration
  • int main( )
  • int timesX( int number, int x) return
    xnumber

Other code
Function definition
11
Function Calls
  • A function is called from another part of the
    program by
  • writing its name, followed by
  • a comma separated list of arguments in
    parentheses
  • Each argument
  • is a variable or an expression such that
  • the type of each argument matches that of the
    corresponding parameter of the function
  • If a function is called, control passes to the
    code for the function
  • The parameters are treated as local variables
    that have been initialized to the values of the
    arguments to the call
  • Control returns from the function to the
    statement after the function call when
  • the execution reaches the end of the function
    body or
  • a return statement is executed
  • A return statement may have a single argument of
    the same type as the return type of the function.
  • If the statement is executed, the argument of
    return becomes the value returned to the caller

12
Example Printing Character Frequencies
13
Example Printing Character Frequencies (contd)
  • Assume that the previous function definition is
    inserted into the frequency counting program just
    in front of the int main(void) line
  • We can then modify main as follows
  • ...
  • for(i0 ilt128 i)
  • if (isprint(i))
  • print_freq(i, freq_counti)
  • return EXIT_SUCCESS
  • The program will then print frequency histograms
    instead of just numbers

14
Exercise
  • Rewrite the Fahrenheit2Celsius Program to use a
    function for the actual conversion

15
Assignment
  • A prime number is an integer greater than 1 that
    is evenly divisible only by 1 and itself
  • 1. Write a function isprime() that determines if
    an integer number is prime.
  • You can use the modulus operator (remainder
    operator on integers) or work with plain
    division. Use your function to implement a
    simple program primes that prints all primes
    between 2 and 10000.

16
Assignment
  • 2. The Sieve of Erathostenes
  • a more efficient (and ancient) algorithm for
    finding all primes up to a given number.
  • It starts with a list of all numbers from 2 to
    the desired limit.
  • It traverses this list, starting at two.
  • Whenever it encounteres a new number, it strikes
    all multiples of it from the list.
  • What remains at the end is a list of prime
    numbers.
  • Example
  • Initial list 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    16
  • Striking multiples of 2 2 3 5 7 9 11 13 15
  • Striking multiples of 3 2 3 5 7 11 13
  • (There are no multiples of any remaining number,
    so we skip to the end
  • Use the Sieve algorithm in a second program,
    primes_sieve, that prints all primes between 2
    and 10000. Hint Use an array!

17
Example Reading Integers
  • We want to write a function that reads a positive
    integer number from stdin, using only getchar()
  • A number is defined as a sequence of decimal
    digits, i.e., characters from the range 0 to
    9
  • We can use the function isdigit(c) from
    ctype.h to test if a character is a decimal
    digit
  • The C standard guarantees that 0 to 9
    have consecutive ascii values.
  • We can thus get the value of a single
    character c that represents a digit by the
    integer expression c -0
  • Idea We read the most significant digits first.
    So whenever we read a new digit, the
    value of what we have read so far increases
    10-fold
  • Read Value
  • 1 1
  • 13 101 3 13
  • 137 1013 7 137
  • 1375 10137 5 1375

18
Example read_int10(void)
19
Example Improved Function
  • read_int10(void) works fine, but can only read
    number in decimal notation
  • We want to have a function that can read numbers
    in any base between 2 and 10
  • Examples
  • 142 in base 8 has the value 182 481 280
    164 48 2 98
  • 101010 in base two has the value
    125024123022121020 32 8 2 42
  • 1873 is not a valid number in base 6! All
    digits have to be smaller than the base
  • The principle for our modified function is the
    same as for read_int10(void), we just use a
    parameter base instead of the hardwired value 10!

20
Do We Have a Valid Digit?
21
Reading a Number in Any Base ? 10
22
Build General Functions!
  • Good programs are build by breaking the task into
    many functions that are
  • Small at most one screen page (in your
    favourite editor)
  • Simple they only do one thing, and they do
    that well
  • General so that they can be reused at
    other parts in the program
  • Going from general to specific is (generally)
    easy

23
Recursive Functions
  • As we stated above, functions can call other
    functions. They can also call themselves
    recursively
  • A recursive function always has to handle at
    least two cases
  • The base case handles a simple situation
    without further calls to the same function
  • The recursive cases may do some work, and in
    between make recursive calls to the function
    for smaller (in some sense) subtasks
  • Recursion is one of the most important
    programming principles!

24
Example Printing Integers
  • We now want to print positive integer numbers to
    stdout, using only putchar()
  • Consider a number in base 10 421 4210 1
  • We can split the task into two subtasks
  • Print everything but the last digit
    (recursively)
  • Print the last digit
  • Base case There are no digits to print any more
  • Basic operations
  • To get the last digit, we use the modulus
    operator
  • To get rid of the last digit, we divide the
    number by the desired base
  • remember integer division truncates

25
Example Decimal Representation of 421
  • Lets do an example we want to print the number
    421 in base 10
  • Step 1 42110 1 and 421/10 42.
  • Hence the last digit to print is 1
  • Before that, we need to print 42
  • Step 2 4210 2 and 42/10 4. The second
    last digit is 2, the rest is 4
  • Step 3 410 4 and 4/10 0. The next digit
    is 4
  • Step 4 Since the upper part of the number is
    0, the recursion stops and there is
    nothing to do but printing the digits in the
    right order
  • The same principle applies for other bases (just
    replace 10 by your base)

26
Writing a Number in Any Base ? 10
27
Putting Things Together a Simple Base Converter
  • We now use the defined function to write a
    program that reads two integers number and base.
  • number is considered to be a decimal number
  • base should be a decimal number between 2 and
    10 (inclusive)
  • number and base are separated by a single,
    arbitrary character
  • The program terminates, if one of the numbers
    is invalid
  • Otherwise, it prints the value of number in the
    indicated base

28
The Base Converter
29
Example Outputs for the Base Converter
30
Exercises
  • 1. Extend the base converter to work with base
    16, using 0-9 and A-F as digits (allow both
    upper and lower case!)
  • Extend the base converter to accept triplets
    input_base, value, output_base,where
  • value is interpreted in input_base
  • input_base is a single hexadecimal digit gt2 and
  • output_base is a single hexadecimal digit gt2
  • Add reasonably robust error handling!
Write a Comment
User Comments (0)
About PowerShow.com