Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Question
Book Icon
Chapter 14, Problem 4PP
Program Plan Intro

Sorting an array of integers

Program Plan:

  • Include requires header file.
  • Declare the function for fill array, sort, swap values and index of smallest.
  • Define main function.
    • Create prompt statement.
    • Declare variables for sample array and number used.
    • Call the function “fillArray”.
    • Prompt statement for index of minimum number.
    • Call the function “indexOfSmallest”.
    • Call the function “sort”.
    • Prompt statement for sorted numbers.
    • Display the sorted number using “for” loop.
  • Define “fillArray” function.
    • This is function is used to read the numbers from user.
  • Define “sort” function.
    • This function is used to sort the number by recursively call the function “sort”.
    • In this function, first declare the variable for index of next smallest.
    • If the start index is less than “numberUsed – 1”, then
      • Compute the index of next smallest number by calling the function “indexOfSmallest” with arguments of array, start index, and number of values in array.
      • Call the function “swapValues” with argument array of start index and array of next element index.
      • Increment the start index.
      • Then recursively call the function “sort” with argument array “a”, starting index and number of values in a given array “a”.
  • Define “swapValues” function.
    • This function is used to swap the two numbers.
  • Define “indexOfSmallest” function.
    • This function is used to compute the index of smallest number by calling the function “indexOfSmallest” recursively.
    • In this function, first assign the minimum to array of starting index.
    • Then if the starting index is equal to “numberUsed – 1”, then returns the starting index value.
    • Otherwise, recursively call the function “indexOfSmallest” with three arguments such as array “a”, increment of start index by “1” and “numberUsed” and this function is assigned to a variable “indexOfMin”.
    • If the value of “min” is greater than “a[indexOfMin]”, then return the minimum index. Otherwise, return the starting index.

Blurred answer
Students have asked these similar questions
Create a Database in JAVA Netbeans that saves name of the player and how many labyrinths did the player solve. Record the number of how many labyrinths did the player solve, and if he loses his life, then save this number together with his name into the database. Create a menu item, which displays a highscore table of the players for the 10 best scores public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private void checkGameState() {        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");            System.exit(0);        }         if (Math.abs(player.getX() - dragon.getX()) <= 1 &&            Math.abs(player.getY() - dragon.getY()) <= 1) {            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");            System.exit(0);        }    } } public…
Create a Database in JAVA OOP that saves name of the player and how many labyrinths did the player solve. Record the number of how many labyrinths did the player solve, and if he loses his life, then save this number together with his name into the database. Create a menu item, which displays a highscore table of the players for the 10 best scores. Also, create a menu item which restarts the game. public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private void checkGameState() {        if (player.getX() == 0 && player.getY() == labyrinth.getSize() - 1) {            JOptionPane.showMessageDialog(this, "You escaped! Congratulations!");            System.exit(0);        }         if (Math.abs(player.getX() - dragon.getX()) <= 1 &&            Math.abs(player.getY() - dragon.getY()) <= 1) {            JOptionPane.showMessageDialog(this, "The dragon caught you! Game Over.");…
Change the following code so that the player can see only the neighboring fields at a distance of 3 units.   public class GameGUI extends JPanel {    private final Labyrinth labyrinth;    private final Player player;    private final Dragon dragon;     private final ImageIcon playerIcon = new ImageIcon("data/images/player.png");    private final ImageIcon dragonIcon = new ImageIcon("data/images/dragon.png");    private final ImageIcon wallIcon = new ImageIcon("data/images/wall.png");    private final ImageIcon emptyIcon = new ImageIcon("data/images/empty.png");     public GameGUI(Labyrinth labyrinth, Player player, Dragon dragon) {        this.labyrinth = labyrinth;        this.player = player;        this.dragon = dragon;         setFocusable(true);        addKeyListener(new KeyAdapter() {            @Override            public void keyPressed(KeyEvent e) {                char move = switch (e.getKeyCode()) {                    case KeyEvent.VK_W -> 'W';                    case…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning