CS1101X: Programming Methodology Recitation 6 Arrays I - PowerPoint PPT Presentation

About This Presentation
Title:

CS1101X: Programming Methodology Recitation 6 Arrays I

Description:

The method isNonNegative() returns true if the array contains all ... Blaise Pascal was a scientist, mathematician par excellence who lived in the 17th century. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 12
Provided by: aaro3
Category:

less

Transcript and Presenter's Notes

Title: CS1101X: Programming Methodology Recitation 6 Arrays I


1
CS1101X Programming MethodologyRecitation 6
Arrays I
2
Task 1 Learning-by-mistake (1/3)
A program NonNegativeArray.java to test whether a
list of integers contains a negative number is
given. The method isNonNegative() returns true
if the array contains all non-negative values it
returns false otherwise.
3 5 0 5 10 8
9 7 4 12 3 2 6
3
Task 1 Learning-by-mistake (2/3)
Which of the following is/are correct?
// version 1 public static boolean isNonNegative
(int arr) boolean flag true for (int
i 0 i lt arr.length i) flag arri
gt 0 return flag
// version 2 public static boolean isNonNegative
(int arr) boolean flag true for (int
i 0 i lt arr.length i) if (arri lt
0) flag false return flag
4
Task 1 Learning-by-mistake (3/3)
Which of the following is/are correct?
// version 3 public static boolean isNonNegative
(int arr) for (int i 0 i lt arr.length
i) if (arri gt 0) return
true return false
// version 4 public static boolean isNonNegative
(int arr) for (int i 0 i lt arr.length
i) if (arri lt 0) return
false return true
5
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.
3 5 5 7 18 21
1 3 4 7 6 9 11
6
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
5 8 7 9 2 8 1 3
12 7 9 12 10 3
7
Task 4 Pascals 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/Mathemati
cians/Pascal.html
8
Task 4 Pascals 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.
9
Task 4 Pascals Triangle (3/3)
Write a program to generate a Pascals Triangle
as shown. It should be observed that the next row
of the Pascals 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 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Output for n (number of rows) 6.
10
To be continued
More problems on arrays (including array of
objects) at the next recitation.
11
End of Recitation 6
Write a Comment
User Comments (0)
About PowerShow.com