In this IB Computer Science lesson you will be learning about:
Sometimes data comes naturally in two dimensional form:
Imagine a class of 7 students that have a quiz every week for 5 weeks
In Java, a table may be implemented as a 2D array
The subscripted variables are used just the same way any other variable or 1D array value would be used.
Important Info
Just like 1D arrays there are many ways to initialize a 2D array
An employer stores the hours worked by each of its employees for each day of the week in a file called ehours.txt. There are 3 employees and data is kept for all 7 days during the week. Your job is to add up the total hours worked by each employee and add up the total hours worked by all the employees each day of the week. Display your results in a nice way.
Solution:
Variables Needed
Reading the Data
Calculating the Sum for each Row
Calculating the Sum of each Column
Complete Solution
1. Pattern Generator
Generate Tables with the following patterns. Your program must work for any size square grid.
2. Pascal Triangle
Below is what as known as pascals triangle
• Here the entire first row is 0 except for the first column which is 1
• The entire first column is always 1
• All the other numbers are obtained by adding the number directly above it to the number diagonally above and to the left of it
o At position [8][4] the value is 70 which is 35 (The number directly above its position) + 21 (The number diagonally above and to the left of its position
Complete the following tasks:
• Use a two Dimensional array to create the Pascal triangle shown above (Assign the first row explicitly and use a nested loop to create all subsequent rows)
• Write the Pascal Triangle to a file without all the 0’s
• Modify the program so that it asks the user how many levels of the triangle to display
Check out our programing in python courses that focus on high school level coding.
These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions
Return To International Baccalaureate Computer Science Main Page