Question

In: Computer Science

Update the code from the questions if necessary. #include #include /* Program sorts an array of...

Update the code from the questions if necessary.

#include

#include

/*

Program sorts an array of integers using a selection sort.

The general algorithm repeatedly finds the smallest number

in the array and places it at the front of the list.

*/

using namespace std;

int find_small_index (int start_index, int numbers []);

void swap_values (int index1, int index2, int numbers []);

int main(int argc, char *argv[])

{

    // array of numbers

    int numbers [10] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41};

    int start_index; // current starting spot for search

    int small_index; // index of the smallest number in the array

    int index;        // index used for print the array values

   

    start_index = 0;

    // continue finding the smallest value and placing it

    // at the front of the list

    while (start_index < 9)

    {

          small_index = find_small_index (start_index, numbers);

          swap_values (small_index, start_index, numbers);

          start_index++;

    }

   

    cout << "\n\nThe sorted array is:\n";

    for (index = 0; index < 10; index++)

        cout << numbers [index] << " ";

    cout << "\n\n";

         

    return 0;

}

int find_small_index (int start_index, int numbers [])

{

    int small_index, // smallest index to be returned

        index;       // current index being viewed

   

    small_index = start_index;

    for (index = start_index + 1; index < 10; index++)

        if (numbers [index] < numbers [small_index])

           small_index = index;

    return small_index;

}

   

void swap_values (int index1, int index2, int numbers [])

{

     int swapper;

    

     swapper = numbers [index1];

     numbers [index1] = numbers [index2];

     numbers [index2] = swapper;

}

1. What value would find_small_index return for the following array?

34

17

26

44

12

81

72

20

62

44

[0]    [1] [2]    [3]   [4]   [5]    [6]   [7]   [8] [9]

2. Assume that the array in question 1 is being used, will the value of the Boolean expression in the if statement in find_small_index be true or false when index is equal to 3 in the for loop? Explain your answer.

3.What is the point of the assignment small_index = start_index; at the beginning of find_small_index? How does this help the function to accomplish its goal?

4. start_index is increased by 1 each time through the loop in main. When find_small_index is called with start_index equal to 5, what must be true about the array values in indexes 0 through 4?

5. In the while loop in main, start_index only goes up to 8 (start_index < 9). Explain why the loop does not need to run when start_index equals 9 (the last index in the array).

6. In swap_values, swapper is declared as an int type. Why?

Solutions

Expert Solution

1). What value would find_small_index return for the following array?

34

17

26

44

12

81

72

20

62

44

[0]    [1] [2]    [3]   [4]   [5]    [6]   [7]   [8] [9]

Answer : find_small_index would return 4 for the first time this function is called

3.)What is the point of the assignment small_index = start_index; at the beginning offind_small_index? How does this help the function to accomplish its goal?

Answer : This function finds the index of the array which contains the smallest value within the specific range of array. It helps the goal in acheiving sorted array. As, this function finds the index of the smallest value. We will swap the value to the required position.

4.) start_index is increased by 1 each time through the loop in main. Whenfind_small_index is called with start_index equal to 5, what must be true about the array values in indexes 0 through 4?

Answer : Because after every time find_small_index is called , we get the smallest value. So,if we don't increase the start_index ; we will only get the same index i.e smallest value but never the second smallest value .So, we will be unable to sort the array. Due to which start_index is increased so that now the find_small_index will find the smallest value excluding the previously selected indexes.

Whenfind_small_index is called with start_index equal to 5, what must be true about the array values in indexes 0 through 4 t? Answer : array values in indexes 0 through 4 are all sorted in ascending order

5)In the while loop in main, start_index only goes up to 8 (start_index < 9). Explain why the loop does not need to run when start_index equals 9 (the last index in the array).

Answer : Because when the start index will come to 9.Then, that would be the last value in the array which will automatically be the largest number as the previous all elements are sorted.So, we need not run the loop for that element

6)In swap_values, swapper is declared as an int type. Why?

Answer : Because in swap_values, we swap the values of the array elements.And , as the array is declared as int and we are swapping the values of that array only which contains integer values. Therefore,the swapper is also declared as int type


Related Solutions

Write a Java program that sorts an array of “Student” in an aescending order of their...
Write a Java program that sorts an array of “Student” in an aescending order of their “last names”. The program should be able to apply (Insertion sort): Student [] studs = new Student[8]; s[0] = new Student("Saoud", "Mohamed", 3.61); s[1] = new Student("Abdelkader", "Farouk", 2.83); s[2] = new Student("Beshr" , "Alsharqawy", 1.99); s[3] = new Student("Nader", "Salah", 3.02); s[4] = new Student("Basem", "Hawary", 2.65); s[5] = new Student("Abdullah", "Babaker", 2.88); s[6] = new Student("Abdelaal", "Khairy", 3.13); s[7] = new Student("Mohamedain",...
Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize)...
Sorting Benchmarks Write a program that generates and sorts an array of a user-specified number (arraySize) of randomly generated numbers. To keep the values to a reasonable range by using the array size as the upper bound for the random numbers (between 1 and arraySize). Your program should call the individual functions that implement the five sorting algorithms discussed in class (see the lecture slides). Each function should keep a count of the number of comparisons/exchanges it makes. Display the...
You are expected to write a program from scratch. In the program, an array will be...
You are expected to write a program from scratch. In the program, an array will be initialized with 23 random integers between 1000 and 1999 (inclusive). The output of the program is 4 lines on the screen, specifically, All elements in the array (line 1) All elements in reverse order (line 2) Every element that is less than 1500 and also at an odd index (line 3) Every odd element that is larger than 1500 (line 4) Note that you...
verilog code of a 64 x64 array with 128 parameters please include test bench
verilog code of a 64 x64 array with 128 parameters please include test bench
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
The code that creates this program using Python: Your program must include: You will generate a...
The code that creates this program using Python: Your program must include: You will generate a random number between 1 and 100 You will repeatedly ask the user to guess a number between 1 and 100 until they guess the random number. When their guess is too high – let them know When their guess is too low – let them know If they use more than 5 guesses, tell them they lose, you only get 5 guesses. And stop...
Check errors and revise/update this java code below and update with OOp class; at least make...
Check errors and revise/update this java code below and update with OOp class; at least make two classes, use java library, validate user input, format and create UML/Pseudocode and flowchart for the code. import java.util.Scanner; public class TestScore {    public static void main(String[] args) {        String firstName;        String lastName;        int numTest;        int i;        int score;        double totalScore;        double avgScore;        String grade;        Scanner input = new Scanner(System.in);        System.out.println("Enter First Name");        firstName = input.nextLine();        System.out.println("Enter Last Name");        lastName = input.nextLine();        System.out.println("How many...
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
Create a method evenOdd() that sorts an array into even numbers on one side and the...
Create a method evenOdd() that sorts an array into even numbers on one side and the odd numbers on the other side public class ourArray { int[] Ar; int size; public int getSize() {    return size; } public ourArray() {    Ar = new int[100];    size = 0; } public void add(int e) {    Ar[size]= e;    size++; } public int remove() {    //save the last item           int save = Ar[size -1];   ...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT