Announcements - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Announcements

Description:

... heads (like a totem)? you have to duplicate ... heads (totem poles)? ( let's ... other functions (have seen in the totem example and will see in the ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 30
Provided by: peopleSab
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Midterm Dates
  • First Midterm Exam November 7, 2015, Saturday,
    1400 1600
  • Second Midterm Exam December 5, 2015, Saturday,
    1000 1200
  • Extra lecture on Wednesday between 1740 1930
    in FASS G062.
  • Same lecture will be repeated on the same day
    1940 2130 in FASS G062.
  • Office Hours have started, schedule is at the TA
    web site, which is reachable from course web
    site.
  • Anyone who have not formally registered to the
    course until the end of add drop should see me
    during the break.
  • Email list is active
  • CS201-201501_at_sucourse.sabanciuniv.edu
  • Service provided by SUCourse Your sabanciuniv
    accounts are registered
  • Weve already sent some emails to this list. Did
    you get them?
  • We will make announcements to the list and
    sometimes to SUCourse. Check your emails and
    SUCourse regularly
  • Please do not subscribe this list address to some
    other lists

2
Announcements
  • Our development environment is MS Visual
    Studio/C 2012 under MS Windows.
  • Any Windows is OK but for Visual Studio you have
    to use 2012. Older or newer versions may create
    consistency problems with our grading system.
  • Other C compilers and development environments
    also have the same potential for inconsistency.
  • Thus your homework that works in your compiler
    may not work in ours.
  • If you have to use another development
    environment for some reason, you have to make
    sure that it also works under Windows and MS
    Visual Studio 2012 before submission. This is
    your responsibility.
  • The "robot" application that we will see next
    week (and also HW3 and HW5 will be about it)
    designed only for MS Windows and Visual Studio
    you will not be able to use it in another
    platform.
  • Any Mac users encountered problem during
    installation?
  • Windows 8 is no longer available at university's
    Software server?
  • Due to licence restrictions
  • You can get Windows from Microsoft Dreamspark
    service
  • To do so first you have write IT to get an
    account and then wait for Microsoft for
    activation. This process may take a couple of
    days. Details are here http//mysu.sabanciuniv.edu
    /it/tr/microsoft-software/dreamspark

3
Announcements
  • First homework is due on October 7, Wednesday,
    1700
  • Submit to SUCourse
  • Strictly follow the submission guideline provided
    in the homework specification
  • Write comments in your program (this will affect
    your grade)
  • Use meaningful identifier names and have a proper
    indentation (these will also affect your grade)
  • New (Second) homework will be assigned this week
  • Stopping at the end of the program execution
  • If you run under the debugger (CtrlF5), you will
    automatically get this prompt at the end of the
    execution and the program waits there
  • Press any key to continue . . .
  • However, if you run with F5 only, you will NOT
    get this prompt and the console screen (the black
    screen) disappears after the execution. This is
    not a problem of your code.
  • You can add the following at the end of your code
    (before return 0) to get the same effect
  • cin.ignore()
  • cin.get()

4
Announcements
  • A general question about the first homework
  • What if the total number of points cannot be
    reached with integer number of wins and draws?
  • E.g. if total points is 18, one win is 5 and one
    draw is 2 points. Max. number of wins is 3 , but
    the remaining 3 points cannot be obtained with
    integer number of draws.
  • In the homework specification we explicitly
    stated that such cases will not happen. Thus you
    can assume that there always be integer solutions
    in the test cases.
  • Another issue about the first homework
  • There must be four inputs only
  • First one is the name of the team
  • Second one is the amount of points given to a
    win
  • Third one is the amount of points given to a
    draw
  • Fourth one is the total points to be collected
  • You have to preserve this order
  • No other inputs are allowed. Otherwise, our
    system cannot process your homework

5
Chapter 2 Continued / Functions
  • Main function
  • A program is a collection of functions and
    classes
  • main and other programmer defined functions
  • only main is executed automatically when the
    program starts
  • other functions must be called to be executed
  • Programmer-defined functions
  • piece of code to do a specific job
  • must be declared before using (this is named as
    calling a function)
  • return-type function-name (parameters)
  • local variables
  • statements
  • to execute a function just write its name (and
    arguments for parameters)
  • When a function is called, execution order
    temporarily jumps to function
  • after the function ends, execution order goes
    back to the caller
  • For the time being
  • return type is void that means returns nothing
  • no parameters (we will see parametric functions
    today)

6
Functions A simple example
  • Without function
  • With function
  • include ltiostreamgt
  • using namespace std
  • //traditional first program
  • int main()
  • cout ltlt "Hello world"
  • ltlt endl
  • return 0
  • include ltiostreamgt
  • using namespace std
  • //traditional first program
  • void Hello()
  • cout ltlt "Hello world"
  • ltlt endl
  • int main()
  • Hello()
  • return 0
  • Output is the same in both case
  • Hello world

7
Why Functions?
  • Divide and Conquer technique
  • divide your problem into small ones
  • write one function for each
  • combine them in the main program
  • Provides modularity
  • when you need to replace a piece of code, you
    just change a function
  • you may not need to change the main program
  • Reuse the same code piece several times
  • avoid repeating the same code

8
With no Functions
  • include ltiostreamgt
  • using namespace std
  • int main()
  • cout ltlt " " ltlt endl
  • cout ltlt " " ltlt endl
  • cout ltlt " o o " ltlt endl
  • cout ltlt " _ _ " ltlt endl
  • cout ltlt "_ _" ltlt endl
  • cout ltlt " ______ " ltlt endl
  • cout ltlt " " ltlt endl
  • return 0
  • Prints head, but not so modular
  • changing the head style requires an update in
    main. Is this bad?
  • in this small example, need for modularity may
    not be clear, but in long programs you will need
    it.
  • Several heads (like a totem)?
  • you have to duplicate the same code

9
With Functions
  • See parts.cpp for the entire program
  • include ltiostreamgt
  • using namespace std
  • // functions appear here
  • int main()
  • Hair()
  • Sides()
  • Eyes()
  • Ears()
  • Smile()
  • Sides()
  • return 0
  • Although more complicated, what are advantages of
    this main over one in which several output
    statements appear in main?
  • modularity
  • changing the head style changing function(s)
  • what about eyeglasses? mustache? (lets do one)

10
Functions with Parameters
  • Functions are useful, parameterized functions
    might be more useful
  • You may need to pass data into a function when
    you call it
  • Consider a generic function to calculate the area
    of any circle and display it
  • how will the function know the radius?
  • solution parameters
  • function is defined without knowing the value of
    the radius
  • value of radius is passed to the function when it
    is called
  • parameters are defined similar to variables
  • type name
  • Example
  • double radius
  • You may have several parameters separated by
    comma

return-type func-name (type param1, type
param2,) local variables statements
11
Area calculation using parameterized function
  • include ltiostreamgt
  • using namespace std
  • // area calculation program that employs
    functions
  • void calculate_area (float radius)
  • float area
  • area 3.14radiusradius
  • cout ltlt "The area of a circle with radius " ltlt
    radius ltlt " is " ltlt area ltlt endl
  • int main()
  • float r
  • r 3
  • calculate_area(r)
  • calculate_area(2.5)
  • calculate_area(r/2)

12
Area calculation using parameterized function
  • include ltiostreamgt
  • using namespace std
  • // area calculation program that employs
    functions
  • void calculate_area (float radius)
  • float area
  • area 3.14radiusradius
  • cout ltlt "The area of a circle with radius " ltlt
    radius ltlt " is " ltlt area ltlt endl
  • int main()
  • float r
  • r 3
  • calculate_area(r)
  • calculate_area(2.5)
  • calculate_area(r/2)

radius is 3
Output Screen
The area of a circle with radius 3 is 28.26
13
Area calculation using parameterized function
  • include ltiostreamgt
  • using namespace std
  • // area calculation program that employs
    functions
  • void calculate_area (float radius)
  • float area
  • area 3.14radiusradius
  • cout ltlt "The area of a circle with radius " ltlt
    radius ltlt " is " ltlt area ltlt endl
  • int main()
  • float r
  • r 3
  • calculate_area(r)
  • calculate_area(2.5)
  • calculate_area(r/2)

radius is 2.5
Output Screen
The area of a circle with radius 3 is 28.26
The area of a circle with radius 2.5 is 19.625
14
Area calculation using parameterized function
  • include ltiostreamgt
  • using namespace std
  • // area calculation program that employs
    functions
  • void calculate_area (float radius)
  • float area
  • area 3.14radiusradius
  • cout ltlt "The area of a circle with radius " ltlt
    radius ltlt " is " ltlt area ltlt endl
  • int main()
  • float r
  • r 3
  • calculate_area(r)
  • calculate_area(2.5)
  • calculate_area(r/2)

radius is 1.5
Output Screen
The area of a circle with radius 3 is 28.26
The area of a circle with radius 2.5 is 19.625
The area of a circle with radius 1.5 is 7.065
15
Functions with Parameters (continued)
  • Parameters and Arguments
  • parameter is the generic name used in function
  • radius in the calculate_area function
  • argument is the value passed to function while it
    is called
  • 2.5
  • r (actually current value of r is passed)
  • r/2 (actually current value of r/2 is passed)
  • Parameter list provides type and name of the
    parameters
  • Argument type must match parameter type
  • Functions may have multiple parameters
  • corresponding arguments are separated by commas
  • in the same order of parameter list
  • be careful about the type matching
  • we will see examples later on
  • Functions may call other functions (have seen in
    the totem example and will see in the next
    example)

16
Functions with Parameters (continued)
  • Parameters versus Local Variables
  • Parameters and defined and used locally, but
    their initial value comes from the caller
    function via arguments
  • Local variables are defined and used in the
    function locally
  • But the initial value does not come from the
    caller function
  • While designing your functions, think carefully
    about local variables and parameters
  • If you need to pass the initial value from the
    caller function, then it should be a parameter.
  • If you wont pass the initial value, then it
    should be a local variable.
  • Example in calculate_area function
  • area is a local variable since we do not pass
    its initial value from main
  • radius is a parameter since we need to pass its
    initial value from main
  • Unnecessary parameters may cause problems and
    grade reduction (for homework)

17
Old McDonalds Farm
  • Aim is to have a modular program (with functions)
    to display the song
  • each verse repeats using a different animal
  • refrain parts repeat within verses
  • A partial output (with two animals only) below
  • Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
  • And on his farm he had a cow, Ee-igh, Ee-igh, oh!
  • With a moo moo here
  • And a moo moo there
  • Here a moo, there a moo, everywhere a moo moo
  • Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
  • Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
  • And on his farm he had a cat, Ee-igh, Ee-igh, oh!
  • With a meow meow here
  • And a meow meow there
  • Here a meow, there a meow, everywhere a meow meow
  • Old MacDonald had a farm, Ee-igh, Ee-igh, oh!

18
Old McDonalds Farm (Design of Functions)
  • Which functions do we need?
  • a function for only Ee-igh, Ee-igh, oh!
  • a function for the refrain (nakarat)
  • a function for And on his farm ... line
  • animal is parameter
  • a function for With a ..., And a ..., Here
    a ... lines
  • noise of the animal is a parameter
  • a function for the whole verse
  • in the main program call the verse function with
    several animal and noise arguments

19
Old McDonalds Farm (Program)
  • See oldmac2.cpp
  • Remarks
  • functions call functions
  • Refrain calls EiEio
  • Verse calls Refrain
  • Verse has two parameters
  • therefore it is called using two arguments first
    one is for animal, second is for noise
  • order of arguments is important
  • animal is used as parameter in two functions.
  • Should we need to use the same parameter name in
    both cases? Or can we use different names in two
    different functions?
  • Same questions are valid for noise as well.
  • Answer is Yes we can use different names. Lets
    do and see.
  • Can we input animal and noise?
  • of course yes (will do next)

20
Programs that Respond to Input
  • Programs without input always give the same
    results
  • values are hardcoded
  • For new values
  • change the program, compile and run again
  • not a good programming practice
  • Allow the user to input values that generate
    output
  • from keyboard, using cin
  • we did this in the first circle area calculation
    program
  • Sequential model of programming input, process,
    output
  • Input information provided from the user
    (outside world)
  • Process Information is processed
  • Output display the result

21
Mac Donalds farm with user input
  • We want the user to enter/input values for animal
    and noise
  • Enter the name of an animal sheep
  • Enter noise that a sheep makes baah
  • Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
  • And on his farm he had a sheep, Ee-igh, ee-igh,
    oh!
  • With a baah baah here
  • And a baah baah there
  • Here a baah, there a baah, everywhere a baah baah
  • Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
  • Well pass the user-entered values to the Verse
    function
  • The input stream cin takes input from the
    keyboard using operator gtgt
  • Values that are input are stored in variables and
    then passed to function verse as arguments
  • see macinput2.cpp

22
Mac Donalds farm revisited
  • // other functions goes here (see macinput2.cpp)
  • void Verse(string animal, string noise)
  • // this function doesnt change
  • // see the source code for the function code
  • int main()
  • string animal // variable for name of
    animal
  • string noise // variable for noise it
    makes
  • cout ltlt "Enter the name of an animal "
  • cin gtgt animal
  • cout ltlt "Enter noise that a " ltlt animal ltlt "
    makes "
  • cin gtgt noise
  • Verse(animal,noise)
  • return 0

23
Analysis of the Run
  1. input value sheep is stored in variable animal
  2. input value baah is stored in variable noise
  3. sheep and baah values are passed to function
    Verse as arguments as well as used in cout in
    main

24
Variables (review from previous lectures)
  • Variables are used to store values in memory
  • memory locations that are accessed using a name
    in the program
  • Each variable has a type, a name (identifier),
    and a value
  • Methods to give values to variables
  • Assignment, using
  • Input, using cin
  • Definition
  • type variable_names_separated_by
    _comma

25
Where to define variables
  • You can define variables anywhere within a
    function
  • provided that it is defined before the first use
  • Two common conventions for the place of variable
    definition
  • At the beginning of the function in which theyre
    used
  • string animal,noise
  • cout ltlt "enter animal "
  • cin gtgt animal
  • cout ltlt "enter noise a " ltlt animal ltlt " makes
    "
  • cin gtgt noise
  • Just before the first place that theyre used
  • cout ltlt "enter animal "
  • string animal
  • cin gtgt animal
  • cout ltlt "enter noise a " ltlt animal ltlt " makes
    "
  • string noise
  • cin gtgt noise

26
Where to define variables
  • NO GLOBAL VARIABLES
  • A global variable is a variable defined outside
    the function bodies
  • They can be used in all functions without
    definition
  • That is why it is the 1 enemy of parameters
  • If you use global variables, you violate the
    independence property of functions and may lose
    your control in big programs
  • Thus, IT IS FORBIDDEN TO USE GLOBAL VARIABLES
  • If you use a global variable in homework and
    exams, your grade will be reduced!
  • At the end of the course, I will formally teach
    global variable concept. After that,
  • you will need to know what global variable is and
    apply them whenever you are asked for, but not
    without permission!

27
Variable Initialization
  • Variables have garbage (junk values) until
  • they are assigned a value using assignment
    operator
  • myint 5
  • or an input is stored into them (using cin
    statement)
  • cin gtgt myint
  • Variables must be given a value before you refer
    to its value for the first time in an expression
    or an output statement or as an argument to a
    function
  • idea behind this rule you can never know what is
    inside of an uninitialized variable!
  • not a syntax error, compiler may or may not warn
    you!
  • You may initialize variables at the declaration
  • int myint 5
  • After initialization, you may change the value
    stored in a variable several times
  • that is why they are named as variable

28
Scope of a Variable and Parameter
  • Not explained in the book in this way
  • Complex rules. Now I give a simple version!
  • RULE 1 A variable or parameter can be referred
    only within the function in which it is declared
  • e.g. you cannot refer the variable animal in
    function eieio
  • RULE 2 A specific identifier can be used several
    times in different functions as variable or
    parameter names. Those are actually different
    variables/parameters.
  • e.g. animal and noise are used both in main (as
    variable) and Verse (as parameter)
  • RULE 3 A specific identifier must be unique
    within a function
  • Detailed scope rules will be given later in this
    course

29
Reading Assignment
  • Section 3.3
  • Case Study Pizza Slices
  • Similar to circle area calculation program
  • Run the program pizza.cpp
Write a Comment
User Comments (0)
About PowerShow.com