cs 161 intro to cs i - oregon state...

13
CS 161 Intro to CS I Static/Dynamic Arrays 1

Upload: phamdieu

Post on 09-Mar-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

CS 161Intro to CS I

Static/Dynamic Arrays

1

Page 2: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Odds and Ends…

• Design due Sunday by 5pm on Canvas

• Friday recitations go to different one

• No class Friday, Happy Veterans Day!

2

Page 3: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Read/Print 1-D Array Values

• Read Values From Userfor(i=0; i<5; i++) {

cout << "Enter final grade for student: ";

cin >> student_grades[i];

}

• Print Valuesfor (i=0; i<5; i++) {

cout << "Student\'s final grade is " << student_grades[i] << endl;

}

3

Page 4: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Demo

4

Page 5: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Static vs. Dynamic 1‐D arrays…

5

0 32 64array[0] array[1] array[2]

0 32 64array[0] array[1] array[2]

0

0

array

array

0

256

Page 6: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Passing a 1-D Array (Static/Dynamic)

int main() {

int array[5];

pass_1darray(array);

}

void pass_1darray(int *a) {

cout << “Array at zero: ” << a[0] << endl;

}

OR

void pass_1darray(int a[]) {

cout << “Array at zero: ” << a[0] << endl;

}6

Page 7: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

How does freeing memory work?

int *p, *q;

p=new int;

q=new int[5];

delete p;

delete [] q;

7

Page 8: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

What are the similarities/differences?

• String Object vs. C String– Which library to include?

<string> VS. <string.h> or <cstring>

– How do we create it?string str_obj; VS. char str_arr[20];

– How do we access it?str_obj.at(3) or str_obj[3] VS. str_arr[3] or *(str_arr+3)

– How do we get the length?str_obj.size() or str_obj.length() VS. strlen(str_arr)

– How is length of string determined?Size member variable VS. ‘\0’, null character at end

8

Page 9: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Demo…

9

Page 10: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Revisit Creating Memory in Functions

10

Advantages to Dynamic Memoryint *i=NULL;//created in main function

create_mem(&i); //call in main voidvoid create_mem(int **m) {

*m = new int[5];}ORi = create_mem(); //call in mainint * create_mem() {

return new int [5];}

Page 11: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Multidimensional Arrays

• data_type array_name[rows][cols];– int array[2][3];

– int array[4][2][3];

– int array[2][4][2][3];

• What are examples of these?– 2‐D – Matrices, Spreadsheet, Minesweeper, Battleship, etc.

– 3‐D – Multiple Spreadsheets, (x, y, z) system

– 4‐D – (x, y, z, time) system

11

Page 12: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Initializing 2‐D Arrays

• Declaration: int array[2][3] = {{0,0,0},{0,0,0}};

• Individual elements: array[0][0]=0; array[0][1]=0;

array[0][2]=0; array[1][0]=0; array[1][1]=0; array[1][2]=0;

• Loop:

for(i = 0; i < 2; i++)

for(j = 0; j < 3; j++)

array[i][j]=0;

• Why do we need multiple brackets?

12

Page 13: CS 161 Intro to CS I - Oregon State Universityclasses.engr.oregonstate.edu/.../slides/slide18.pdf ·  · 2016-11-09Static/Dynamic Arrays 1. Odds and Ends ... Z\ ì [, null character

Reading/Printing 2-D Arrays• Reading Array Values

for(i = 0; i < 2; i++)

for(j = 0; j < 3; j++) {

cout << “Enter a value for ” << i << “, ” << j << “: ”;

cin >> array[i][j];

}

• Printing Array Valuesfor(i = 0; i < 2; i++)

for(j = 0; j < 3; j++)

cout << “Array: ” << array[i][j] << endl;

13