Presentation Number 5 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Presentation Number 5

Description:

Before a function can be used it should be defined. A function has: a ... function recieves two input variables: base ... be stored from the first element. ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 27
Provided by: thearbe
Category:

less

Transcript and Presenter's Notes

Title: Presentation Number 5


1
Presentation Number 5
  • C Programming Course

2
Function Definition
type function_name( type argument_1, type
argument_2 ... ) statement 1 statement 2
... ... return value
  • Before a function can be used it should be
    defined.
  • A function has
  • a name.
  • a type.
  • input variables (optional).
  • some code that makes up the functions body.

3
Example
The type is double
The name is power
The function recieves two input variables base
and exponent
  • double power(double base, int exponent)
  • int i0
  • double result1
  • for (i1 iltexponent i)
  • resultresultbase
  • return result

Inside a function we can use any of the C
commands.
The function return the appropriate value.
4
Switch
  • Kinda like many ifs in one

5
The Switch Statement
  • You can write
  • if ( letter 'a' )
  • printf( The letter is a\n)
  • else if ( letter 'b' )
  • printf( The letter is b\n)
  • else if ( letter 'c' letter 'C' )
  • printf( The letter is either c or C\n)
  • else
  • printf( This is not a familiar letter\n)

6
The Switch Statement
  • switch ( your_grade / 10 )
  • case 10
  • case 9
  • printf( A good grade\n)
  • break
  • case 8
  • case 7
  • printf( O.K. grade\n)
  • break
  • case 6
  • printf( Not so good\n)
  • break
  • default
  • printf( Ooooops!!!\n)

the switch case has to be an integer value
the switch jumps to the appropriate label
unless stopped the execution will "fall through"
to the other commands
if no appropriate label exists will jump to the
default
default is optional
7
  • switch( oper )
  • case
  • printf( Value lf\n, x y )
  • break
  • case -
  • printf( Value lf\n, x - y )
  • break
  • case
  • printf( Value lf\n, x y )
  • break
  • case /
  • printf( Value lf\n, x / y )
  • break

SimpleCalc.c
8
Arrays
  • storing more than one thing

9
Arrays
  • Arrays allow us to store a block of many
    variables of the same type and access them with a
    single name.
  • Why do we need this
  • List of students marks
  • Series of numbers entered by the user
  • Vectors
  • Matrices
  • Anything else you can think of ... it is very
    useful
  • Tic-Tac-Toe board?

10
Arrays
  • An array
  • Is a variable like any other.
  • It has a type.
  • It has a name.
  • It has a constant size.
  • double values100 int grades63
  • char str256 char ticTacToeBoard9

type array_name size
11
Arrays
  • Accessing an array element
  • All operations on array apply to single elements.
  • You use the along with an index to access an
    array element.
  • An array element is the same as a variable of the
    same type.
  • There is no way to address the entire array as a
    whole
  • You cannot assign an array.
  • You cannot compare arrays.

arr5 x
x arr5
if ( arri 10 )
12
Arrays
  • Array size
  • The array size is a constant number.
  • The size of the array cannot be changed.
  • Only the elements in the range 0 ? (size-1)
    are part of the array.

double values100
13
Reverse Order
  • int main()
  • int ctr0, arr10
  • printf( "Enter 10 numbers\n" )
  • for ( ctr 0 ctr lt 10 ctr )
  • scanf( "d", arrctr )
  • printf( "The numbers in reverse order\n" )
  • for ( ctr 9 ctr gt 0 --ctr )
  • printf( "d ", arrctr )
  • printf("\n")
  • return 0

Define an array of size 10
Scan a number into the array.
Print a number from the array.
Reverse.c
14
Initialization
  • Init during definition
  • This method works only during definition.
  • Write values inside .
  • The values will be stored from the first element.
  • If there are less values than elements, then the
    rest are filled with 0.

int iArr10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
int iArr10 0, 1, 2, 3, 4
int iArr10 0
15
Arrays and Functions
  • Arrays as function input variables
  • When passing an array to the function the actual
    array is passed.
  • When you change the value of an element inside
    the function you are changing the value of the
    element in the original array.

type function_name( type array_name, ... )
16
Arrays and Functions
  • void init_array( int arr, int size, int value )
  • int ctr0
  • for ( ctr 0 ctr lt size ctr )
  • arr ctr value
  • int main()
  • int array20
  • init_array( array, size, 7 )
  • . . .

Initialize the array elements to 7
17
Algorithms
18
Sort The Idea
We will use three indexes to run over the
array i, j and min_index
Assume the first i elements are sorted (elements
0 ... i-1)
current element
We are now sorting element i
19
Sort The Idea
Look in the unsorted elements for (index j) for
an element whose value is smaller than the
element in position i. Store its index in
min_index.
Switch between the value of the element in
position i and the element in position min_index.
current element
20
Sort
  • void sort( int arr, int num_elements )
  • int i 0, tmp 0
  • for( i 0 i lt num_elements i )
  • min_index i
  • for ( j i1 j lt num_elements j )
  • if ( arrmin_index gt arrj )
  • min_index j
  • tmp arri
  • arri arrmin_index
  • arrmin_index tmp

The initial minimal index is i.
Loop across elements i1 to n
Switch indexes if needed.
Switch minimal values.
Sort.c
21
Questions
22
Complexity
  • How complex is an algorithm?
  • What does complex mean?
  • Whats an algorithm?

An algorithm is a recipe (or a set of
instructions) that achieve a certain goal.
Complexity defines the resources (time and space)
needed to cook the recipe (achieve the goal).
23
Complexity
How many operations are needed to sort an array
of numbers? Lets assume that the size of the
array is n (an arbitrarily large number). How
many operations are needed as a factor of n?
current element
Sorting the ith element requires approximately (n
i) operations.
24
Complexity
Approximately n squared operations are needed to
sort n numbers.
25
Complexity
  • O(n) notation means that we need approximately n
    operations to achieve our goal.
  • We know that there is a constant multiplying n.
  • For a theoretical result the constant does not
    matter as long as we assume that n is much larger
    than the constant.
  • For practical results the constant may seriously
    effect the time needed to run the algorithm.

26
Sort Complexity
  • The best thing you can do for sorting is O(nlong)
    but it is really, really, really complicated to
    program.
Write a Comment
User Comments (0)
About PowerShow.com