Вы находитесь на странице: 1из 23

LAB #08

DATE: 2016/04/13
PRESENTER: CHIEN

<1000><

AGENDA
Basics of Pointer
Using pointers in functions
Practice #1 call by pointer
Practice #2 call by reference
Dynamic two-dimensional arrays
Practice #3

AGENDA
Basics of Pointer
Using pointers in functions
Practice #1 call by pointer
Practice #2 call by reference
Dynamic two-dimensional arrays
Practice #3

REVIEW-POINTERS

REVIEW-POINTERS

REVIEW-POINTERS

REVIEW-POINTERS

For Example:
1 int a;
2 int *ptr;
3 *ptr = &a;//Error

ptr = & a; // correct

REVIEW-POINTERS

AGENDA
Basics of Pointer
Using pointers in functions
Practice #1 call by pointer
Practice #2 call by reference
Dynamic two-dimensional arrays
Practice #3

USING POINTERS IN FUNCTIO


NS
In C++, function can only return one value a time. If you w
ant the function to return multiple values, there are 2 solut
ions.
1. Call by pointer/reference.
2. Return an array
We first practice call by pointer.

CALL BY POINTER
To call by pointers:
Declare a pointer variable as a

void swap (int* x, int* y);


int main() {
int a = 10, b = 20;
cout << a << " " << b << "\n";

parameter.
Pass a pointer variable or an
address.
Invocation becomes swap(&a,
&b);

cout << &a << "\n";


swap(&a, &b);
cout << a << " " << b << "\n";
}
void swap (int* x, int* y) {
cout << x << "\n";
int temp = *x;
*x = *y;
*y = temp;
}

PRACTICE #1 (1/2)
Given a rectangle on a chess board, compute the number
of black blocks and white blocks of the rectangle.
Example:
Given (x1,y1)=(1, 1) and (x2,y2)=( 3, 1)
Answer is 2, 1
Input Format:
x1 y1 x2 y2
Output Format:
Black White

Input Sample:
1131
Output Sample:
2 1

y
8
7
6
5
4
3
2
1
1 2 3 4 5 6 7 8

PRACTICE #1 (2/2)
The function should look like
void count(int x1, int y1, int x2, int y2, int* black, int* whit
e).
x1 and y1 form the point 1 while x2 and y2 form the point
2.
Cout number of black blocks and number of white blocks
.

AGENDA
Basics of Pointer
Using pointers in functions
Practice #1 call by pointer
Practice #2 call by reference
Dynamic two-dimensional arrays
Practice #3

REVIEW-CALL BY REFERNCE

REVIEW-CALL BY REFERNCE

PRACTICE #2
The task is the same as practice #1.
This time practice call by reference.

AGENDA
Basics of Pointer
Using pointers in functions
Practice #1 call by pointer
Practice #2 call by reference
Dynamic two-dimensional arrays
Practice #3

REVIEW
To declare a dynamic one-dimensional array.
data type* array name=new data type[size];
To declare a two-dimensional array
data type** array name=new data type*[size];

REVIEW

PRACTICE #3 (1/3)
In super market IM, there are 10 items(id= a, b, c,.j) an
d n customers. Customers may buy only one item or a lot
of items at a time.
The market manager wants to keep these buying informa
tion for future research.
Please use the two-dimensional dynamic array to store t
he inputs.

PRACTICE #3 (2/3)
Input Format:
n
Total1 item1 item2 item3 //customer 1
Total2 item1 item2 item3 //customer 2
Total3 item1 //customer 3

Totaln item

Output Format:
1 item1 item2 item3 //customer 1
2 item1 item2 item3 //customer 2
3 item1 //customer 3

//customer n

PRACTICE #3 (3/3)
Input Sample:

3 acd

2 jj

6bacade

1a

Output Sample:
1 acd
2 jj
3bacade
4a

Вам также может понравиться