2-D Arrays Worksheet - Java - Introduction to Computer Programming | CS 0007, Assignments of Computer Science

Material Type: Assignment; Class: INTRO TO COMPUTER PROGRAMMING; Subject: Computer Science; University: University of Pittsburgh; Term: Unknown 2003;

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-cum
koofers-user-cum 🇺🇸

4

(1)

6 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2-D Arrays Worksheet Java
1. Write the statements needed to declare and load the following arrays.
2 4 1 5 2 7
A = 6 9 7 8 B = 3 7 9 C = 6 3
0 3 2 8 1 4 8 0 4
2. Use the arrays of question number 1 to answer the following question. What is the value of
(a) A[1,2] (b) C[2,1] (c) B[0,2] (d) C[1, 2]
Trace each of the following program segments showing a neat iteration table and the output window.
3.
int[][] Q = new int[2][4];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 4; j++)
Q[i][j] = i*j;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++)
System.out.print(Q[i][j] + "^^");
System.out.println();
}
4.
int [][] nums = new int[2][2];
int r, c;
for (r = 1; r >= 0; r--)
for (c = 0; c < 2; c++)
nums[r][c] =Console.in.readInt();
for (r = 0; r < 3; r++)
System.out.println(nums[r][1]);
data
6 9 0 –4 5 -2
5. Write a method FlipArray that imports array M on the left and converts it to, and returns, array N on the
right. Array M must remain unchanged.
2 5 4 7
M = 3 1 2 9
4 6 3 0
2 3 4
N = 5 1 6
4 2 3
7 9 0
6. Write a method SumOfRows that will will import array M above and print the following output.
Row^^^^^^Sum of Row
1 18
2 15
3 13
7. Write a method SumOfCols that will import array M from #5 and print the following output.
Column^^^^Sum of Column Entries
1 9
2 12
3 9
4 16
/app/work/qkd5i7-661477-3105975-arrays2d-doc.doc
Copyright 2003, Louis Trentini. All rights reserved.
pf2

Partial preview of the text

Download 2-D Arrays Worksheet - Java - Introduction to Computer Programming | CS 0007 and more Assignments Computer Science in PDF only on Docsity!

2-D Arrays Worksheet Java

  1. Write the statements needed to declare and load the following arrays. 2 4 1 5 2 7 A = 6 9 7 8 B = 3 7 9 C = 6 3 0 3 2 8 1 4 8 0 4
  2. Use the arrays of question number 1 to answer the following question. What is the value of (a) A[1,2] (b) C[2,1] (c) B[0,2] (d) C[1, 2] Trace each of the following program segments showing a neat iteration table and the output window. 3. int[][] Q = new int[2][4]; for (int i = 0; i < 2; i++) for (int j = 0; j < 4; j++) Q[i][j] = i*j; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) System.out.print(Q[i][j] + "^^"); System.out.println(); }

int [][] nums = new int[2][2]; int r, c; for (r = 1; r >= 0; r--) for (c = 0; c < 2; c++) nums[r][c] =Console.in.readInt(); for (r = 0; r < 3; r++) System.out.println(nums[r][1]); data 6 9 0 –4 5 -

  1. Write a method FlipArray that imports array M on the left and converts it to, and returns, array N on the right. Array M must remain unchanged. 2 5 4 7 M = 3 1 2 9 4 6 3 0

N = 5 1 6

  1. Write a method SumOfRows that will will import array M above and print the following output. Row^^^^^^Sum of Row 1 18 2 15 3 13
  2. Write a method SumOfCols that will import array M from #5 and print the following output. Column^^^^Sum of Column Entries 1 9 2 12 3 9 4 16 /app/work/qkd5i7-661477-3105975-arrays2d-doc.doc Copyright 2003, Louis Trentini. All rights reserved.

For the traces below, the arrays established in an earlier exercise may/will be used in later exercises. (8) int[][] A = new int[4][3]; for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) A[i][j] = ij; (9) int[][] B = new int[2][4]; for (int i = 1; i >= 0; i--) for (int j = 0; j < 4; j++) B[i][j] = Console.in.readInt() Data 3 5 0 -2 8 -1 9 44 (10) int[][] C = new int[8][8]; for (int i=0; i<C.length; i++) { C[i][i] = 2i-1; j = C[i].length - 1; while j >= i { C[i][j] = i*j; j -= 2; } } (11) int[][] X = new int[3][4]; for (int i=0; i<X.length; i++) for (int j = 0; j < 4; j++) X[i][j] = A[j][i]; (12) System.out.println("Array X"); for (int j=0; j<X[1].length; j++) { for (int i=0; i<X.length; i++) System.out.print(X[i][j] + "^^^"); System.out.println(); }

int[][] M = new int[4][2]; for (int i = 0; i < 4; i++) for (int j = 0; j < 2; j++) M[i][i] = A[i][j] + B[j][i]; (14) int[][] V = new int[4][3]; for (int j=V[1].length-1; j>=0; j--) for (int i=V.length-1; i>=0; i--) V[i][j] = i+j-1; (15) for (int i = 0; i < 4; i++) { for (int j = 2; j >= 0; j--) System.out.print(V[i][j] + "^^"); System.out.println(); } (16) for (int j = 3; j >= 0; j--) { for (int i = 0; i < 3; i++) System.out.print(X[i][j] + "^^"); System.out.println(); } (17) for (int j = 0; j < 4; j++) { for (int i = 2; i >= 0; i--) System.out.print(X[i][j] + "^^"); System.out.println() } /app/work/qkd5i7-661477-3105975-arrays2d-doc.doc Copyright 2003, Louis Trentini. All rights reserved.