package exercises; import java.util.Arrays; import java.util.Random; /** * * @author Lefteris Moussiades */ public class x01 { //askisi 1 public static void sequentialSearch() { int[] store = {2, 5, 8, 12}; int key = 5; int idx = -1; for (int i = 0; i < store.length; i++) { if (store[i] == key) { idx = i; break; } } if (idx > -1) { System.out.println("Search Element found at position " + idx); } else { System.out.println("Search Element not found"); } } //askisi 2 static void search() { int[] store = new int[100]; Random r = new Random(); for (int i = 0; i < store.length; i++) { store[i] = r.nextInt(10) + 1; } System.out.println("table ready!"); int schItem = 9; int cnt = 0; for (int i : store) { if (i == schItem) { cnt++; } } System.out.println("To item " + schItem + " brethike " + cnt + " fores"); } //askisi 3 public static void sum() { int[] tbl = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int sum = 0; for (int i : tbl) { sum += i; } System.out.println("sum=" + sum); } //askisi 4 public static void max() { int[] tbl = {1, 2, 3, 14, 5, 14, 7, 8, 9}; int max = tbl[0]; int idx = 0; for (int i = 1; i < tbl.length; i++) { if (tbl[i] >= max) { idx = i; max = tbl[i]; } } System.out.println("max=" + max + " found at " + idx); } public static void max2() { int[] tbl = {1, 2, 3, 14, 5, 14, 7, 8, 9}; //int max = tbl[0]; int idx = 0; for (int i = 1; i < tbl.length; i++) { if (tbl[i] >= tbl[idx]) { idx = i; } } System.out.println("max=" + tbl[idx] + " found at " + idx); } //askisi 5 public static void reverse() { int[] tbl = {1, 2, 3, 14, 5, 6, 7, 8, 9}; int[] reversed = new int[tbl.length]; for (int i = 0; i < tbl.length; i++) { reversed[tbl.length - 1 - i] = tbl[i]; } for (int i : reversed) { System.out.print(i + " "); } } //askisi 6 // Προσοχή!!! Σύμφωνα με την εκφώνηση, οι πίνακες είναι ίσοι αν έχουν το ίδιο μήκος και κάθε στοιχείου του ενός // περιέχεται και στον άλλον. Επομένως, η εκφώνηση δεν απαιτεί τα ίσα στοιχεία στους 2 πίνακες να βρίσκονται σε // αντίστοιχες θέσεις static void equality() { int[] tbl1 = {5, 6, 7}; int[] tbl2 = {6, 5, 7}; //int[] tbl1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}; //int[] tbl2 = {5, 6, 7, 8, 9, 101, 2, 3, 4}; boolean equal = tbl1.length == tbl2.length; if (!equal) { System.out.println("tables not equal"); } else { for (int i = 0; i < tbl1.length && equal; i++) { boolean found = false; for (int j : tbl2) { if (tbl1[i] == j) { found = true; break; } } if (!found) { System.out.println(tbl1[i]); } equal = found; } System.out.println(equal); } } //askisi 7 public static void stringEquality() { String[] tbl1 = new String[]{new String("John"), "Maria", "Kely"}; String[] tbl2 = {"John", "Kely", "Maria"}; boolean equal = tbl1.length == tbl2.length; if (!equal) { System.out.println("tables not equal"); } else { for (int i = 0; i < tbl1.length && equal; i++) { boolean found = false; for (String j : tbl2) { if (tbl1[i]==j) { found = true; break; } } equal = found; } System.out.println(equal); } } //ασκηση 8 public static void binarySearch() { int[] tbl = new int[]{1, 3, 4, 5, 7, 9, 11, 23, 34, 50}; int idx = -1; final int schElement = 24; int from = 0, to = tbl.length - 1, mid = (to - from) / 2; while (to >= from) { if (tbl[mid] == schElement) { idx = mid; break; } else if (schElement < tbl[mid]) { to = mid - 1; } else { from = mid + 1; } mid = from + (to - from) / 2; } if (idx == -1) { System.out.println("Element " + schElement + " was not found"); } else { System.out.println("Element " + schElement + " found at position " + idx); } } //άσκηση 9 public static void diagonios() { int[][] tbl = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {10, 9, 8, 7, 6}, {5, 4, 3, 2, 1}, {0, 1, 0, 1, 0} }; int[] diagonios = new int[5]; for (int i = 0; i < tbl.length; i++) { diagonios[i] = tbl[i][i]; } for (int i : diagonios) { System.out.print(i + " "); } System.out.println(); } public static void main(String[] args) { //sequentialSearch(); //search(); //binarySearch(); //diagonios(); //sum(); //max(); //reverse(); stringEquality(); //equality(); //int[] t1 = {1, 2, 3}; //int[] t2 = {1, 2, 3}; //System.out.println("..." + (t1 == t2)); } }