Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.8 Lab: Insertion Sort (find errors) The following program is a test driver for the Insertion Sort function: It should sort an array of doubles

1.8 Lab: Insertion Sort (find errors)

The following program is a test driver for the Insertion Sort function: It should sort an array of doubles in descending order, but and it has some errors (syntax and logical)

Find and fix them as shown below:

// Function declaration void insertion Sort( double ary, int size) 
  • mark the errors
  • comment the line
  • write the correction below

Eg.

// Function declaration // void insertion Sort( double ary, int size) // Error#1: missing ; // Error#2: missing [] // Error#3: bad function name - it contains a space void insertionSort( double ary[], int size); 

Hint: read all comments carefully!

Here is what I have so far:

// Lab: Insertion Sort (find errors) #include

using namespace std;

//ERROR 1: function parameters are declared wrong //ERROR 2: missing ; //void insertionSort(double ary[], int size); void insertionSort(double numbers[], int numberSize);

int main() { double list[100] = {50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8, 30.2, 80.8, 30.5}; int size = 10; for (int i = 0; i < size; i++) { cout << list[i] << " "; } cout << endl; // A call for the insertion sort function //ERROR: function parameters wrong, don't need to declare void insertionSort(list, size);

for (int i = 0; i < size; i++) { cout << list[i] << " "; } cout << endl; return 0; }

/* ************************************************** This function sorts an array in descending order using the Insertion Sort algorithm */ //ERROR: i in the function name should be lowercase void insertionSort(double numbers[], int numberSize) { //ERROR: forgot to declare variables double temp; for (int curr = 1; curr < numberSize; curr++) { // make a copy of the current element temp = numbers[curr]; // shift elements in the sorted part of the list to make room int walk = curr - 1; while( walk >= 0 && temp < numbers[walk] ) { numbers[walk] = numbers[walk+1]; walk++; } // put the copy of the current element back into the list numbers[walk] = temp; }// end of for: move to the next element to be inserted }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Programming The Perl DBI Database Programming With Perl

Authors: Tim Bunce, Alligator Descartes

1st Edition

1565926994, 978-1565926998

More Books

Students also viewed these Databases questions