CIS 101: Computer Programming and Problem Solving Lecture 9 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CIS 101: Computer Programming and Problem Solving Lecture 9

Description:

Declare function before main() and then define it anywhere in the file. Or ... dimension 10 times 10, with un-initialized. values. You can also do. int *a[10] ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 23
Provided by: Usm16
Category:

less

Transcript and Presenter's Notes

Title: CIS 101: Computer Programming and Problem Solving Lecture 9


1
CIS 101 Computer Programming and Problem
SolvingLecture 9
  • Usman Roshan
  • Department of Computer Science
  • NJIT

2
C functions
  • Function definition
  • ltreturn valuegt ltfunction_namegt (
  • ltarg1_typegt ltarg1gt,
  • ltarg2_typegt ltarg2gt,
  • ,
  • ltargn_typegt ltargngt)
  • For example,
  • int myadd(int x, int y)l
  • int max(int x, int y)

3
C functions
  • Compiler goes from top to bottom. So, either
  • Declare function before main() and then define it
    anywhere in the file
  • Or define it before main()

4
myadd function
First define function
5
Same function with different parameters--overloadi
ng
6
Passing arrays as arguments
Define function before main
7
Program output
8
Pointers and reference
Returns memory location of the variable
  • int x2 int y
  • y x

Memory
x
100
2
2
y
x
Memory is organized into cells. Lets say x is in
cell number 100
y is a pointer to an integer. We set it to point
to x which means it will now contain the memory
location of x.
9
Pointers and references
10
Dynamic memory allocation---creating and deleting
arrays of arbitrary size
  • int x
  • x new int1
  • We first create an array pointer and then
  • create space in memory for one integer it
  • can point to.

11
Pointers and reference
  • int x
  • x new int1
  • x 2

Memory
50
2
x
x
12
Pointers
13
Output of pointer program
x is a pointer to a location in memory which is
why it shows up in HEX
The memory location x points to contains 2.
14
Dynamic arrays
  • int x
  • x new int(3)
  • x0 2
  • x2 4
  • x3 6

Memory
50
2
4
6
x0
x
x1 x2
15
Dynamic arrays
16
Dynamic arrays
  • Memory defined using new must be cleared up using
    delete. Otherwise your program may use up ALL the
    memory.
  • int x new int10
  • .
  • .
  • delete x

17
Passing variables by value
A new variable is created that contains a copy of
x
This means the value in the original variable is
unchanged.
18
Program output
19
Passing variables by reference
means we are receiving a reference to the
original variable and it can be modified.
Now the value in the original variable x can be
modified.
20
Program output
21
Two dimensional arrays
  • 2D arrays are defined as
  • int A1010
  • This allocates space for a 2-D array of
  • dimension 10 times 10, with un-initialized
  • values.
  • You can also do
  • int a10
  • This creates an array of 10 integer pointers for
  • which space has to be allocated using new.

22
Lab problems
  • Power function to compute xy
  • Swap function to interchange two numbers
  • Copy array function
  • Problems from midterm
Write a Comment
User Comments (0)
About PowerShow.com