Question

Create a Kotlin console program that satisfies the following 1. The program prompts a user to enter a number that is used to

4. the algorithims for sorting numbers should be managed in separate functions (e.g., sortAscending() and sortDescending())

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Create a kotlin file and save the file as sorting.kt
The below kotlin program prompts user to enter the size of items to read and then prompts user to enter the items and store the item values in an array.Then prompts user to enter the choice for sorting ,a for ascendig or d for descending sorting. Then prompt user to continue or q to quit.
//The software used to run this program is  Intelli-IDEA
//sorting.kt
fun main()
{
    var repeat=true
    while (repeat)
    {

        var numItems = 0
        print("Enter a positive integer :")
        //read size in numItems
        numItems = Integer.parseInt(readLine())
        //create an array of given size
        val item_array = IntArray(numItems)

        //read numbers and store in array 
        for (x in 0..numItems - 1) {
            print("Enter item $x : ")
            var num = Integer.parseInt(readLine())
            item_array[x] = num
        }

        print("You have entered ")
        //print numbers 
        for (x in item_array) {
            print(" $x")
        }
        println()
        print("Enter a for ascending , or d for descending :")
        //read user choice for ascending or d for descending order 
        var choice = readLine()
        if (choice.equals("a"))
            //call sortAscending function
            sortAscending(item_array)
            print("The numbers in ascending order :")
            for (x in item_array)
                print(" $x")
        println()
        
        //check if user enters d for descending sorting
        if (choice.equals("d"))
        //call sortDescending function
            sortDescending(item_array)
        print("The numbers in desending order :")
        for (x in item_array)
            print(" $x")
        println()
        print("To continue, Enter c ; To quit, enter anyther key")
       var userchoice= readLine()

        if (userchoice.equals("q"))
            repeat=false
    }
}
//The function that takes an integer array and sorts the numbres
//in asending order and returns the sorted numbers
fun sortAscending(arr :IntArray):IntArray
{
    var swap = true
    while(swap)
    {
        swap = false
        for(i in 0 until arr.size-1)
        {
            if(arr[i] > arr[i+1]){
                val temp = arr[i]
                arr[i] = arr[i+1]
                arr[i + 1] = temp

                swap = true
            }
        }
    }
    return arr
}
//The function that takes an integer array and sorts the numbres
//in descending order and returns the sorted numbers
fun sortDescending(arr :IntArray):IntArray
{
    var swap = true
    while(swap)
    {
        swap = false
        for(i in 0 until arr.size-1)
        {
            if(arr[i] < arr[i+1]){
                val temp = arr[i]
                arr[i] = arr[i+1]
                arr[i + 1] = temp

                swap = true
            }
        }
    }
    return arr
}

sample output:

C:\Program Files\Javaljdk-10\bin\java.exe -javaagent:F:\softwares Enter a positive integer :3 Enter item 0:6 Enter item 1:8

Add a comment
Know the answer?
Add Answer to:
4. the algorithims for sorting numbers should be managed in separate functions (e.g., sortAscending() and sortDescending())...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers...

    LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).

  • In the Java language APCS Sorting Numbers Lab Write a program that reads in 3 floating-point...

    In the Java language APCS Sorting Numbers Lab Write a program that reads in 3 floating-point numbers (decimal numbers) and prints the three numbers in sorted order from smallest to largest. In your main method, create a scanner and get the 3 numbers from the user. Create a separate method called sort that accepts the 3 numbers prints them in the appropriate order. Sample Run#1 Please enter first number: 4 Please enter second number: 9 Please enter third number: 2.5...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT