Write a Swift program to find the larger value of a given array of integers and set all the other elements with that value


Introduction

I have used Swift for windows 1.9 for debugging purpose. But you can use any Swift programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. func newarray(_ arra: [Int]) -> [Int] {
  2. var new_arra = arra
  3.  
  4. if new_arra.first! > new_arra.last!
  5. {
  6. new_arra[1] = new_arra.first!
  7. new_arra[2] = new_arra.first!
  8. }
  9. else
  10. {
  11. new_arra[0] = new_arra.last!
  12. new_arra[1] = new_arra.last!
  13. }
  14. return new_arra
  15. }
  16. print(newarray([1, 4, 6]))
  17. print(newarray([2, 5, 9]))
  18. print(newarray([3, 8, 8]))

Result

swift programming languages techstudy.org

     





Related Topics





Popular Posts