Rewrite this code, using modular programming style, and correcting any errors if necessary.  2. Optimize the algorithm by making the following modifications: (a) After the first pass, the largest number is guaranteed to be in the highest- numbered element in the array; after the second pass, the two highest numbers are "in place," and so on. Instead of comparing every pair on every pass, modify the algorithm to make as few comparisons as necessary on each pass.  (b) Modify the algorithm to check at the end of each pass if any swaps have been made. If none have been made, the data must already be in the proper order, so the program should terminate. Observe the single-exit point rule when making this modification.  3. Insert appropriate statements in your code to output the state of the array after every pass.    #include #include using namespace std; using std::setw; using std::cout; using std::endl; using std::size_t; int main() {     const short arraySize = 10;    short arr [ arraySize ] = { 5, 6, 4, 9, 10, 12, 89, 68, 45, 41 };    short hold;    cout << "Data items in original order:" << endl;    for ( size_t i = 0; i < arraySize; ++i ) {     cout << setw ( 4 ) << arr [ i ]; } // for i     for ( size_t pass = 0; pass < arraySize; ++pass ) {     for ( size_t j = 0; j < arraySize; ++j ) {        if ( arr [ j ] > arr [ j + 1 ] ) {           hold = arr [ j ];           arr [ j ] = arr [ j + 1 ];           arr [ j + 1 ] = hold;        } // if     } // for j    } // for pass    cout << endl << "Data items in ascending order:" << endl;    for ( size_t i = 0; i < arraySize; ++i ) {     cout << setw ( 4 ) << arr [ i ]; } // for i cout << endl; return 0; } // BubbleSort

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
  1. Rewrite this code, using modular programming style, and correcting any errors if necessary. 

2. Optimize the algorithm by making the following modifications:

(a) After the first pass, the largest number is guaranteed to be in the highest- numbered element in the array; after the second pass, the two highest numbers are "in place," and so on. Instead of comparing every pair on every pass, modify the algorithm to make as few comparisons as necessary on each pass. 

(b) Modify the algorithm to check at the end of each pass if any swaps have been made. If none have been made, the data must already be in the proper order, so the program should terminate. Observe the single-exit point rule when making this modification. 

3. Insert appropriate statements in your code to output the state of the array after every pass. 

 

#include <iostream>

#include <iomanip>

using namespace std;

using std::setw;

using std::cout;

using std::endl;

using std::size_t;

int main() { 

   const short arraySize = 10;

   short arr [ arraySize ] = { 5, 6, 4, 9, 10, 12, 89, 68, 45, 41 };

   short hold;

   cout << "Data items in original order:" << endl;

   for ( size_t i = 0; i < arraySize; ++i ) {

    cout << setw ( 4 ) << arr [ i ];

} // for i 

   for ( size_t pass = 0; pass < arraySize; ++pass ) {

    for ( size_t j = 0; j < arraySize; ++j ) {

       if ( arr [ j ] > arr [ j + 1 ] ) {

          hold = arr [ j ];

          arr [ j ] = arr [ j + 1 ];

          arr [ j + 1 ] = hold;

       } // if

    } // for j

   } // for pass

   cout << endl << "Data items in ascending order:" << endl;

   for ( size_t i = 0; i < arraySize; ++i ) {

    cout << setw ( 4 ) << arr [ i ];

} // for i

cout << endl;

return 0;

} // BubbleSort 

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education