Top Banner
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT
22

CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

CIS 101: Computer Programming and Problem Solving

Lecture 9Usman Roshan

Department of Computer Science

NJIT

Page 2: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

C++ functions

Function definition:

<return value> <function_name> (<arg1_type> <arg1>, <arg2_type> <arg2>,…,<argn_type> <argn>);

For example, int myadd(int x, int y)lint max(int x, int y);

Page 3: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

C++ functions

Compiler goes from top to bottom. So, either

(1) Declare function before main() and then define it anywhere in the file

(2) Or define it before main()

Page 4: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

myadd function

First define function

Page 5: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Same function with different parameters--overloading

Page 6: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Passing arrays as arguments

Define function before main

Page 7: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Program output

Page 8: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Pointers and reference

int x=2; int *y;

y = &x;

2 2

Memory

Memory is organized into cells. Let’s say x is in cell number 100

100

Returns memorylocation of thevariable

x

yx

y is a pointer to an integer. We set it to point to x which means it will now containthe memory location of x.

Page 9: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Pointers and references

Page 10: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Dynamic memory allocation---creating and deleting arrays of arbitrary size

int *x;

x = new int[1];

We first create an array pointer and then

create space in memory for one integer it

can point to.

Page 11: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Pointers and reference

int *x;

x = new int[1];

*x = 2;

Memory

x

50 2

*x

Page 12: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Pointers

Page 13: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Output of pointer programx is a pointer to a location in memory which is why it shows up in HEX

The memory location x points to contains 2.

Page 14: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Dynamic arrays

int *x;

x = new int(3);

x[0] = 2;

x[2] = 4;

x[3] = 6;

Memory

x

50 2

x[0]

4 6

x[1] x[2]

Page 15: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Dynamic arrays

Page 16: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Dynamic arrays

• Memory defined using new must be cleared up using delete. Otherwise your program may use up ALL the memory.

int *x = new int[10];

.

.

delete x;

Page 17: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Passing variables by value

A new variable is createdthat contains a copy of x

This means the value in the original variableis unchanged.

Page 18: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Program output

Page 19: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Passing variables by reference

& means we are receiving areference to the original variableand it can be modified.

Now the value in the original variablex can be modified.

Page 20: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Program output

Page 21: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Two dimensional arrays

2D arrays are defined asint A[10][10];This allocates space for a 2-D array of dimension 10 times 10, with un-initializedvalues.You can also doint *a[10];This creates an array of 10 integer pointers forwhich space has to be allocated using new.

Page 22: CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.

Lab problems

1. Power function to compute x^y

2. Swap function to interchange two numbers

3. Copy array function

4. Problems from midterm