Advertisement
Domy131097

LV3: Zadatak 1

Nov 21st, 2017
110
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None
  1. package vjezba3;
  2.  
  3. /**
  4. * @author Dominik Tkalčec
  5. * Zadatak 1
  6. */
  7. import java.util.Scanner;
  8.  
  9. public class Zadatak1 {
  10.  
  11. public static void unosVektora(int n, int []V) {
  12. Scanner input = new Scanner(System.in);
  13. for(int i = 0; i < n; i++) {
  14. System.out.println("Unesite " + (i + 1) + ". element vektora V:");
  15. V[i] = input.nextInt();
  16. }
  17. }
  18. public static void unosMatrice(int m, int [][]M) {
  19. Scanner input = new Scanner(System.in);
  20. for(int i = 0; i < m; i++) {
  21. for(int j = 0; j < m; j++) {
  22. System.out.println("Unesite element matrice M[" + i + "][" + j + "]");
  23. M[i][j] = input.nextInt();
  24. }
  25. }
  26. }
  27. public static double aritmetickaSredinaSporedneDijagonale(int m, int [][]M) {
  28. double rezultat = 0;
  29. for(int i = 0; i < m; i++) {
  30. for(int j = 0; j < m; j++) {
  31. if((i + j) == 2) rezultat += M[i][j];
  32. }
  33. }
  34. return rezultat/m;
  35. }
  36. public static int sumaNeparnihElemenata(int m, int [][]M) {
  37. int rez = 0;
  38. for(int i = 0; i < m; i++) {
  39. if((M[0][i])%2 != 0) rez += M[0][i];
  40. }
  41. for(int i = 1; i < m; i++) {
  42. if((M[i][0])%2 != 0) rez += M[i][0];
  43. }
  44. return rez;
  45. }
  46. public static void ispisivanjeBrojevaNaParnimIndeksima(int n, int []V) {
  47. System.out.print("Elementi na parnim indeksima: ");
  48. for(int i = 0; i < n; i++) {
  49. if(i%2 != 0) continue;
  50. if(i == n - 1) System.out.print(V[i] + ".");
  51. else System.out.print(V[i] + ", ");
  52. }
  53. System.out.print("\n");
  54. }
  55. public static void main(String[] args) {
  56. int m = 0, n = 0, odabir = 0;
  57. boolean provjeraVektor = false, provjeraMatrica = false;
  58. Scanner input = new Scanner(System.in);
  59. System.out.println("Unesite duljinu vektora V:");
  60. do {
  61. try {
  62. n = input.nextInt();
  63. } catch(Exception greska) {
  64. System.err.println("(GRESKA) Pogresan unos.");
  65. System.exit(1);
  66. }
  67. } while (n < 2 || n > 10);
  68. int[] V = new int[n];
  69. System.out.println("Unesite duljinu kvadratne:");
  70. do {
  71. try {
  72. m = input.nextInt();
  73. } catch(Exception greska) {
  74. System.err.println("(GRESKA) Pogresan unos.");
  75. System.exit(1);
  76. }
  77. } while (m < 1 || m > 5);
  78. int[][] M = new int[m][m];
  79. while (odabir != 6) {
  80. System.out.println("============================== IZBORNIK ==============================");
  81. System.out.println("1: Unos vektora.");
  82. System.out.println("2: Unos matrice.");
  83. System.out.println("3: Izračunavanje aritmetičke sredine sporedne dijagonale.");
  84. System.out.println("4: Izračunavanje sume neparnih elemenata 1. retka i 1. stupca matrice.");
  85. System.out.println("5: Ispisivanje brojeva na parnim indeksima vektora V.");
  86. System.out.println("6: Kraj programa");
  87. System.out.println("============================== ======== ==============================");
  88. odabir = input.nextInt();
  89. switch(odabir) {
  90. case 1:
  91. unosVektora(n, V);
  92. provjeraVektor = true;
  93. break;
  94. case 2:
  95. unosMatrice(m, M);
  96. provjeraMatrica = true;
  97. break;
  98. case 3:
  99. if(provjeraMatrica == false) {
  100. System.out.println("(GRESKA) Morate prvo unijeti elemente matrice da bi mogli koristiti ovu mogucnost. \n\n");
  101. break;
  102. }
  103. System.out.println("Aritmeticka sredina sporedne dijagonale iznosi: " + aritmetickaSredinaSporedneDijagonale(m, M));
  104. break;
  105. case 4:
  106. if(provjeraMatrica == false) {
  107. System.out.println("(GRESKA) Morate prvo unijeti elemente matrice da bi mogli koristiti ovu mogucnost.\n\n");
  108. break;
  109. }
  110. if(sumaNeparnihElemenata(m, M) == 0) System.out.println("Nema neparnih elemenata.");
  111. else System.out.println("Suma neparnih elemenata 1. retka i 1. stupca iznosi: " + sumaNeparnihElemenata(m, M));
  112. break;
  113. case 5:
  114. if(provjeraVektor == false) {
  115. System.out.println("(GRESKA) Morate prvo unijeti elemente vektora da bi mogli koristiti ovu mogucnost.\n\n");
  116. break;
  117. }
  118. ispisivanjeBrojevaNaParnimIndeksima(n, V);
  119. break;
  120. case 6:
  121. System.out.println("Kraj programa.");
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. }
  128. }
Advertisement
RAW Paste Data Copied
Advertisement