Algorithms to Practice arrays and stack in Java - 1
Mazen Mohamed
created at: tags: java, algorithms, arrays, stack, data-structure

Problem 1
Given a linear array A, write a program that maps the location in the array (L) into a memory address, given the base address of A in memory and word count per element (W)
Input:
- Base address: 3450
- Word count: 8
- Element number: 9
Output: 3514
1 2public class Main { 3 4 public static void main(String[] args) { 5 int elemAbsLoc; 6 int arrBase = 3450; 7 int wordCount = 8; 8 int elemRelLoc = 9; 9 elemAbsLoc = arrBase + wordCount * (elemRelLoc - 1); 10 System.out.println(elemAbsLoc); 11 } 12} 13
Problem 2
Given a 2-D array, A. Write a Program that maps the location in the array given by (J, K) into a memory address, given the base address of A in memory and word count per element (W) and Number of rows (M) and a number of columns (N) in the case of:
- Using column by column order.
- Using row-by-row ordering.
Input:
- Row or Column Ordering: Row
- Number of rows: 12
- Number of columns: 10
- Base address: 3450
- Word count: 4
- Element number/index: 9
- Row number: 6
Output: 3682
Using (column by column) or (row-by-row) ordering depending on the user input
1import java.util.Scanner; 2 3public class Main { 4 public static void main(String[] args) { 5 Scanner scan = new Scanner(System.in); 6 7 char majorType; 8 9 System.out.print("Row(r) or Column(c) Ordering:"); 10 majorType = scan.nextLine().charAt(0); 11 12 int elemAbsLoc; 13 int arrBase = 3450; 14 int wordCount = 4; 15 int arrRowsCount = 12; 16 int arrColumnsCount = 10; 17 int elemRelLocJ = 6; 18 int elemRelLocK = 9; 19 20 if (majorType == 'c') { 21 elemAbsLoc = arrBase + wordCount * (arrRowsCount * (elemRelLocK - 1) + (elemRelLocJ - 1)); 22 System.out.println(elemAbsLoc); 23 } 24 else if (majorType == 'r') { 25 elemAbsLoc = arrBase + wordCount * (arrColumnsCount * (elemRelLocJ - 1) + (elemRelLocK - 1)); 26 System.out.println(elemAbsLoc); 27 } 28 29 scan.close(); 30 } 31}
Problem 3
1public class Main { 2 3 public static void main(String[] args) { 4 int[][] matrix1 = { { 1, 0 }, { 2, 3 }, { 7, 6 } }; 5 int[][] matrix2 = { { 4, 6 }, { 5, 5 }, { 2, 4 } }; 6 7 int[][] matrixSum = new int[matrix1.length][matrix1[0].length]; 8 9 int i, j; 10 for (i = 0; i < matrix1.length; i++) { 11 for (j = 0; j < matrix1[0].length; j++) { 12 matrixSum[i][j] = matrix1[i][j] + matrix2[i][j]; 13 System.out.print(matrixSum[i][j] + " "); 14 } 15 System.out.println(""); 16 } 17 } 18}
Problem 4
Add method Clear() in the stack class which removes all values from the stack.
1public class MyStack { 2 int maxSize; 3 int stackTop; 4 int[] myArr; 5 int numberOfElements; 6 7 public MyStack(int maxSize) { 8 this.maxSize = maxSize; 9 this.myArr = new int[maxSize]; 10 this.stackTop = -1; 11 this.numberOfElements = 0; 12 } 13 14 public boolean isFull() { 15 return this.maxSize == this.numberOfElements; 16 } 17 18 public boolean isEmpty() { 19 return this.numberOfElements == 0; 20 } 21 22 public int push(int element) { 23 if (this.isFull()) { 24 // throw new Exception("Stack Overflow!"); 25 System.err.println("Stack Overflow!"); 26 System.exit(-1); 27 } 28 29 this.myArr[this.numberOfElements] = element; 30 this.stackTop++; 31 this.numberOfElements++; 32 33 return element; 34 } 35 36 public int pop() { 37 if (this.isEmpty()) { 38 // throw new Exception("Stack is empty!"); 39 System.err.println("Stack is empty!"); 40 System.exit(-1); 41 } 42 43 int element = this.myArr[this.stackTop]; 44 45 this.stackTop--; 46 this.numberOfElements--; 47 48 return element; 49 } 50 51 public void printAll() { 52 int i; 53 for (i = this.stackTop; i >= 0; i--) { 54 System.out.println(this.myArr[i]); 55 } 56 } 57 58 public void clearAll() { 59 this.stackTop = -1; 60 this.numberOfElements = 0; 61 62 int i; 63 for (i = this.stackTop; i > 0; i--) { 64 this.myArr[i] = 0; 65 } 66 } 67}
1public class Main { 2 public static void main(String[] args) { 3 MyStack stack = new MyStack(5); 4 5 stack.push(1); 6 stack.push(2); 7 stack.pop(); 8 stack.push(3); 9 stack.push(4); 10 stack.pop(); 11 stack.push(4); 12 stack.push(6); 13 stack.pop(); 14 stack.push(4); 15 stack.pop(); 16 stack.push(5); 17 18 System.out.println("printAll 1 starts:"); 19 stack.printAll(); 20 System.out.println("printAll 1 ends."); 21 22 stack.clearAll(); 23 24 System.out.println(); 25 26 System.out.println("printAll 2 starts:"); 27 stack.printAll(); 28 System.out.println("printAll 2 ends."); 29 } 30}
Problem 5
Implement a function that takes input parameters (decimal number) and works on it using Stack to convert it into a binary number.
Note:
Using the [
"MyStack"
]
class from the previous problem.
1public class Main { 2 public static void main(String[] args) { 3 MyStack stack = new MyStack(5); 4 System.out.println("We will convert the number 23 to binary"); 5 int num = 23; 6 7 while (num > 0) { 8 int r = num % 2; 9 10 stack.push(r); 11 num /= 2; 12 } 13 14 System.out.print("The binary equivalent to 23: "); 15 16 while (!(stack.isEmpty())) { 17 System.out.print(stack.pop()); 18 } 19 } 20}