Home 2015 FRQ Question 3
Post
Cancel

2015 FRQ Question 3

Part A

For this part of the FRQ, all I had to do was to look through a 2D array to find specific values based on coordinates. I did this by using a for loop to iterate through each value in the array list, looking for the correct column and row searched for by the method being called. I then return the value at those coordinates to later print. In the main() method, I test all possible coordinates, showing that my method works correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// pre-made class
public class SparseArrayEntry {
    private int col;
    private int row;
    private int value;

    public SparseArrayEntry(int r, int c, int v) {
        this.row = r;
        this.col = c;
        this.value = v;
    }

    public int getRow() {
        return this.row;
    }

    public int getCol() {
        return this.col;
    }

    public int getValue() {
        return this.value;
    }
}

public class SparseArray {
    private int numRows;
    private int numCols;

    private List<SparseArrayEntry> entries;

    public SparseArray() {
        this.entries = new ArrayList<SparseArrayEntry>();
    }

    public int getNumRows() {
        return numRows;
    }

    public int getNumCols() {
        return numCols;
    }

    public int getValueAt(int row, int col) { // getting value at specific coordinates
        for (int i = 0; i < this.entries.size(); i++) { // using a for loop to search through the arraylist that is created
            if (this.entries.get(i).getRow() == row && this.entries.get(i).getCol() == col) { // checks if the correct row and column is reached
                return this.entries.get(i).getValue(); // returns the value
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        // adding value to the SparceArray
        SparseArray sparseArray = new SparseArray();
        sparseArray.entries.add(new SparseArrayEntry(1, 1, 5));
        sparseArray.entries.add(new SparseArrayEntry(1, 4, 4));
        sparseArray.entries.add(new SparseArrayEntry(2, 0, 1));
        sparseArray.entries.add(new SparseArrayEntry(3, 1, -9));

        // the example array
        int[][] entries = { {0, 0, 0, 0, 0},
                            {0, 5, 0, 0, 4},
                            {1, 0, 0, 0, 0}, 
                            {0, -9, 0, 0, 0},
                            {0, 0, 0, 0, 0},
                            {0, 0, 0, 0, 0} };

        // comparison to see if the values gotten from the arraylist match the values that are in the 2D array
        for (int i = 0; i < entries.length; i++) { // looks through the entries array defined above
            for (int j = 0; j < entries[i].length; j++) { // for column, we look for the expected
                int expected = entries[i][j];
                int actual = sparseArray.getValueAt(i, j); // this retrieves the value using my method
                System.out.println("Position (" + i + ", " + j + "): Expected: " + expected + ", Actual: " + actual); // compares the values
            }
        }
    }
}

SparseArray.main(null);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Position (0, 0): Expected: 0, Actual: 0
Position (0, 1): Expected: 0, Actual: 0
Position (0, 2): Expected: 0, Actual: 0
Position (0, 3): Expected: 0, Actual: 0
Position (0, 4): Expected: 0, Actual: 0
Position (1, 0): Expected: 0, Actual: 0
Position (1, 1): Expected: 5, Actual: 5
Position (1, 2): Expected: 0, Actual: 0
Position (1, 3): Expected: 0, Actual: 0
Position (1, 4): Expected: 4, Actual: 4
Position (2, 0): Expected: 1, Actual: 1
Position (2, 1): Expected: 0, Actual: 0
Position (2, 2): Expected: 0, Actual: 0
Position (2, 3): Expected: 0, Actual: 0
Position (2, 4): Expected: 0, Actual: 0
Position (3, 0): Expected: 0, Actual: 0
Position (3, 1): Expected: -9, Actual: -9
Position (3, 2): Expected: 0, Actual: 0
Position (3, 3): Expected: 0, Actual: 0
Position (3, 4): Expected: 0, Actual: 0
Position (4, 0): Expected: 0, Actual: 0
Position (4, 1): Expected: 0, Actual: 0
Position (4, 2): Expected: 0, Actual: 0
Position (4, 3): Expected: 0, Actual: 0
Position (4, 4): Expected: 0, Actual: 0
Position (5, 0): Expected: 0, Actual: 0
Position (5, 1): Expected: 0, Actual: 0
Position (5, 2): Expected: 0, Actual: 0
Position (5, 3): Expected: 0, Actual: 0
Position (5, 4): Expected: 0, Actual: 0

Part B

In this part of the question all I had to do was iterate through each row of the array and search for values that were in a certain column. Once those values are found, they are taken out and the rest of the values are shifted over by one so that the indices match up. I then use a custom method to print out the 2D array to show that the column was deleted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.ArrayList;
import java.util.List;

// premade class
public class SparseArrayEntry {
    private int col;
    private int row;
    private int value;

    SparseArrayEntry(int r, int c, int v) {
        this.row = r;
        this.col = c;
        this.value = v;
    }

    public int getRow() {
        return this.row;
    }

    public int getCol() {
        return this.col;
    }

    public int getValue() {
        return this.value;
    }
}

public class SparseArray {
    private int numRows;
    private int numCols;

    private List<SparseArrayEntry> entries;

    public SparseArray(int rows, int cols) {
        this.entries = new ArrayList<SparseArrayEntry>();
        this.numRows = rows;
        this.numCols = cols;
    }

    public int getNumRows() {
        return numRows;
    }

    public int getNumCols() {
        return numCols;
    }

    public int getValueAt(int row, int col) { // same method as before
        for (int i = 0; i < this.entries.size(); i++) {
            if (this.entries.get(i).getRow() == row && this.entries.get(i).getCol() == col) {
                return this.entries.get(i).getValue();
            }
        }
        return 0;
    }

    public void removeColumn(int col) { // removing a column
        for (int i = 0; i < this.entries.size(); i++) { // iterating through the array searching for the column items to remove
            SparseArrayEntry entry = this.entries.get(i);
            if (entry.getCol() == col) { // finding the column that needs to be deleted
                this.entries.remove(i); // removes the column
                i--; // subtracts one because array size is now one smaller
            } else if (entry.getCol() > col) { // moving all values over by one
                entry = new SparseArrayEntry(entry.getRow(), entry.getCol() - 1, entry.getValue());
                this.entries.set(i, entry); // setting new values
            }
        }
        numCols--; // removing the number of columns
    }

    public void addEntry(int row, int col, int value) {
        this.entries.add(new SparseArrayEntry(row, col, value));
    }

    public static void main(String[] args) {
        // creating array
        SparseArray sparseArray = new SparseArray(6, 5);
        sparseArray.addEntry(1, 1, 5);
        sparseArray.addEntry(1, 4, 4);
        sparseArray.addEntry(2, 0, 1);
        sparseArray.addEntry(3, 1, -9);

        // testing removing array
        System.out.println("Before removing column:");
        printSparseArray(sparseArray);

        int colToRemove = 1;
        sparseArray.removeColumn(colToRemove);

        System.out.println("\nAfter removing column " + colToRemove + ":");
        printSparseArray(sparseArray);
    }

    public static void printSparseArray(SparseArray sparseArray) {
        System.out.println("Number of Rows: " + sparseArray.getNumRows());
        System.out.println("Number of Columns: " + sparseArray.getNumCols());
        // iterating through 2D array to print array out
        for (int i = 0; i < sparseArray.getNumRows(); i++) {
            for (int j = 0; j < sparseArray.getNumCols(); j++) {
                int value = sparseArray.getValueAt(i, j);
                System.out.print(value + " ");
            }
            System.out.println();
        }
    }
    
}

SparseArray.main(null);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Before removing column:
Number of Rows: 6
Number of Columns: 5
0 0 0 0 0 
0 5 0 0 4 
1 0 0 0 0 
0 -9 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

After removing column 1:
Number of Rows: 6
Number of Columns: 4
0 0 0 0 
0 0 0 4 
1 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 
This post is licensed under CC BY 4.0 by the author.

2015 FRQ Question 2

2015 FRQ Question 4