Tutorial 5 Arrays Basics (1D, 2D) NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

Post on 02-Jan-2016

218 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

1

Tutorial 5 Arrays Basics (1D, 2D)

NUS SCHOOL OF COMPUTING

CS1010E PROGRAMMING METHODOLOGY

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

2

Quick Summary

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

3

Question 1: Tracing Arrays

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

4

Question 1: Tracing Arrays

List1 11 22 33 44 55List2 99 99 99 99 99

num 11

passElement(int num)

num 1234

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

5

Question 1: Tracing Arrays

List1 11 22 33 44 55List2 99 99 99 99 99

changeElements(int list[])

77 88

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

6

Question 1: Tracing Arrays

List1 11 22 33 44 55List2 99 99 77 99 88

copyArray(list2, list1, 5)

11 22 33 44 55

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

7

Question 2: Sieve Prime Numbers

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

8

Question 2: Sieve Prime Numbers

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

9

Question 2: Sieve Prime Numbers

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Find the next Prime Number

Use for loop to eliminate all the multiples of the prime number

10

Question 2: Sieve Prime Numbers

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

11

Question 3: Pascal Triangle

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

12

Question 3: Pascal Triangle

Element: Up

Element: UpLeft

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

13

Question 3: Pascal TriangleQuestion: is it possible to fill up the triangle without considering the border values?

Yes, we could initialize all the elements in the Pascal Triangle (2D Array) to 1 first. And our index starts from 1 instead of 0.

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

14

Question 3: Pascal Triangle

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

15

Question 3: Pascal Triangle

CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

top related