Download CS1101: Programming Methodology

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of trigonometry wikipedia , lookup

Mathematics and architecture wikipedia , lookup

Addition wikipedia , lookup

Pythagorean theorem wikipedia , lookup

Weber problem wikipedia , lookup

Transcript
CS1101X:
Programming Methodology
Recitation 6
Arrays I
Task 1: Learning-by-mistake
To be given out at the recitation.
CS1101X Recitation #6
2
Task 2: isSorted
A program IsSortedArray.java to test whether a
list of integers in sorted in non-decreasing order.
The method isSorted() returns true if the array is
sorted; it returns false otherwise.
Based on what we have discussed for task 1, can
you write the isSorted() method?
3 5 5 7 18 21
true
1 3 4 7 6 9 11
false
CS1101X Recitation #6
3
Task 3: Second largest
Given a list of integers, determine the second largest
value in the list. (Assuming that the list contains at
least 2 values.)
10 3 7 15 11 21
15
5 8 7 9 2 8 1 3
8
12 7 9 12 10 3
12
CS1101X Recitation #6
4
Task 4: Pascal’s Triangle (1/3)
Blaise Pascal was a scientist, mathematician par
excellence who lived in the 17th century. Some of
his mathematical work was fundamental to the
theory of probability. There is also a programming
language named after him (though in no real sense
related to him). More of him in this website (for
those who are interested):
http://www-gap.dcs.st-and.ac.uk/~history/Mathematicians/Pascal.html
CS1101X Recitation #6
5
Task 4: Pascal’s Triangle (2/3)
In this problem, you are asked to generate Pascal's
Triangle. Pascal's Triangle is useful in many areas
from probability to polynomials to setting
programming questions. It is a triangle of integers
with 1 on top and down the sides. Any number in
the interior equals the sum of the two numbers
above it. For example, here are the first 5 rows of
the triangle.
1
1
1
1
CS1101X Recitation #6
1
1
2
3
4
1
3
6
1
4
1
6
Task 4: Pascal’s Triangle (3/3)
Write a program to generate a Pascal’s Triangle as
shown. It should be observed that the next row of
the Pascal’s triangle can be generated from the
previous row. Thus, using an array to store the
values of the previous rows seems appropriate.
1
1
1
1
1
1
CS1101X Recitation #6
1
2
3
4
5
Output for n (number of rows) = 6.
1
3 1
6 4 1
10 10 5 1
7
To be continued…
More problems on arrays (including
array of objects) at the next recitation.
CS1101X Recitation #6
8
End of Recitation #6
CS1101X Recitation #6
9