Advertisement
QwertyAvatar

Swift 6

Nov 14th, 2022
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.88 KB | Software | 0 0
  1. //
  2. //  main.swift
  3. //  Lab6.1
  4. //
  5.  
  6. import Foundation
  7.  
  8. var tab: [Double] = Array(repeating: 1.0, count: 12)
  9. for i in tab {
  10.     print(i)
  11. }
  12.  
  13. ////////////////////////////////////////////////////
  14.  
  15. //
  16. //  main.swift
  17. //  Lab6.2
  18. //
  19.  
  20. import Foundation
  21.  
  22. //a
  23. var emptyArr: [Int] = []
  24. for _ in 1...10{
  25.     let num = Int.random(in: 1..<101)
  26.     emptyArr.append(num)
  27. }
  28. for i in emptyArr {
  29.     print(i, terminator:" ")
  30. }
  31. print("\n")
  32.  
  33. //b
  34. guard let wczyt = Int(readLine()!) else
  35. {
  36.     fatalError("Zla liczba")
  37. }
  38. print("\n")
  39. emptyArr.insert(wczyt, at: 0)
  40. for i in emptyArr {
  41.     print(i, terminator:" ")
  42. }
  43.  
  44. //c, d
  45.  
  46. print("\n")
  47. let x = Int.random(in: 1..<10)
  48. emptyArr.insert(wczyt, at: x)
  49. for i in emptyArr {
  50.     print(i, terminator:" ")
  51. }
  52. print("\n")
  53.  
  54. //////////////////////////////////////////////////
  55.  
  56. //
  57. //  main.swift
  58. //  Lab6.3
  59. //
  60.  
  61. import Foundation
  62.  
  63. print("Podaj liczbe elementow")
  64. guard let liczba = Int(readLine()!) else
  65. {
  66.     fatalError("Zla liczba")
  67. }
  68.  
  69. if(liczba>0){
  70.     var tab: [Int] = []
  71.    
  72.     for i in 0..<liczba {
  73.         print("Podaj element nr \(i) :")
  74.         guard let elem = Int(readLine()!) else
  75.         {
  76.             fatalError("Zla liczba")
  77.         }
  78.         tab.append(elem)
  79.     }
  80.    
  81.     print("Podaj liczbe sprawdzenia")
  82.     guard let l1 = Int(readLine()!) else
  83.     {
  84.         fatalError("Zla liczba")
  85.     }
  86.     if(tab[0]==l1)
  87.     {
  88.         print("Liczba \(l1) jest 1szym elem tab")
  89.     }
  90.     if(tab[liczba-1]==l1)
  91.     {
  92.         print("Liczba \(l1) jest ostat elem tab")
  93.     }
  94. }
  95. else{
  96.     fatalError("Zla liczba")
  97. }
  98.  
  99. //////////////////////////////////////////////
  100.  
  101. //
  102. //  main.swift
  103. //  Lab6.4
  104. //
  105.  
  106. import Foundation
  107.  
  108. print("Podaj liczbe elementow")
  109. guard let liczba = Int(readLine()!) else
  110. {
  111.     fatalError("Zla liczba")
  112. }
  113.  
  114. if(liczba>0){
  115.     var tab1: [Int] = []
  116.     var tab2: [Int] = []
  117.    
  118.     for _ in 0..<liczba {
  119.         let elem = Int.random(in: 1...20)
  120.         tab1.append(elem)
  121.     }
  122.     for _ in 0..<liczba {
  123.         let elem = Int.random(in: 1...20)
  124.         tab2.append(elem)
  125.     }
  126.     tab1.sort()
  127.     tab2.sort()
  128.     if(tab1==tab2)
  129.     {
  130.         print("Tablice maja jednakowe elementy ")
  131.     }
  132. }
  133. else{
  134.     fatalError("Zla liczba")
  135. }
  136.  
  137. /////////////////////////////////////////////
  138.  
  139. //
  140. //  main.swift
  141. //  Lab6.5
  142. //
  143.  
  144. import Foundation
  145.  
  146. let mac: [[Int]] = [[10, 20, 30], [40, 50, 60]]
  147. for i in 0 ..< mac.count {
  148.     for j in 0 ..< mac[i].count{
  149.     print("\(mac[i][j])", terminator:" ")
  150.     }
  151. print()
  152. }
  153.  
  154. ////////////////////////////////////////////////
  155.  
  156. //
  157. //  main.swift
  158. //  Lab6.6
  159. //
  160.  
  161. import Foundation
  162.  
  163. print("Podaj liczbe wierszy")
  164. guard let w = Int(readLine()!) else
  165. {
  166.     fatalError("Zla liczba")
  167. }
  168. print("Podaj liczbe kolumn")
  169. guard let k = Int(readLine()!) else
  170. {
  171.     fatalError("Zla liczba")
  172. }
  173.  
  174. if(w>0 && k>0){
  175.     var tab: [[Double]] = [[]]
  176.    
  177.     for i in 0..<w {
  178.         for _ in 0..<k {
  179.             tab[i].append(Double.random(in: -100.0...100.0))
  180.         }
  181.         tab.append([])
  182.     }
  183.    
  184.     for x in 0 ..< tab.count {
  185.         for y in 0 ..< tab[x].count{
  186.         print("\(tab[x][y])", terminator:" ")
  187.         }
  188.     print()
  189.     }
  190.    
  191.     var maks: Double = -100.0
  192.     var mini: Double = 100.0
  193.     var ix1: Int = 0
  194.     var iy1: Int = 0
  195.     var ix2: Int = 0
  196.     var iy2: Int = 0
  197.     for x in 0 ..< tab.count {
  198.         for y in 0 ..< tab[x].count{
  199.             if(tab[x][y]<mini){
  200.                 mini=tab[x][y]
  201.                 ix1 = x
  202.                 iy1 = y
  203.             }
  204.             if(tab[x][y]>maks){
  205.                 maks=tab[x][y]
  206.                 ix2 = x
  207.                 iy2 = y
  208.             }
  209.         }
  210.     }
  211.     print("Min to: \(mini) a indeksy to: \(ix1), \(iy1)")
  212.     print("Max to: \(maks) a indeksy to: \(ix2), \(iy2)")
  213. }
  214. else{
  215.     fatalError("Zle liczby")
  216. }
  217.  
  218. /////////////////////////////////////////////
  219.  
  220.  
Tags: swift
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement