Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this bubble sort program 10 numbers in the array are sorted to print out in a

ID: 3628487 • Letter: I

Question

In this bubble sort program 10 numbers in the array are sorted to print out in asending order.

I need to print out each array after each pass.

How do I make that happen?

#include <iostream>
#include <iomanip>

int main()
{
const short arraySize = 10;
short arr [arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};
short hold;


bool changesMade = true;
for(std::size_t pass = 0; pass < arraySize && changesMade; ++pass){
changesMade = false;
for(std::size_t j = 0; j < arraySize - 1 - pass; ++j)
if(arr[ j ] > arr [ j + 1 ]);
changesMade = true;
}


std::cout <<"Data items in the original order: "<<std::endl;
for ( std::size_t i = 0; i < arraySize; ++i ) {
std::cout <<std::setw ( 4 )<< arr [ i ];
} // for i

for ( std::size_t pass = 0; pass < arraySize; ++pass ) {
for(std::size_t j = 0; j < arraySize + 1; ++j) {
if(arr [ j ] > arr [ j + 1 ]) {
hold = arr [ j ];
arr [ j ] = arr [ j + 1 ];
arr [ j + 1 ] = hold;
} // if
} // for j
}// for pass
std::cout <<" ";
std::size_t pass = 0;
for ( std::size_t i = 0; i < arraySize; ++i ) {
std::cout<<"Pass "<<pass + 1<<": "<<std::setw ( 4 ) << arr [ i ]<<std::endl;
}


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


for ( std::size_t i = 0; i < arraySize; ++i ) {
std::cout <<std::setw ( 4 ) << arr [ i ];
} // for i
std::cout <<" ";

// return 0;
system ("PAUSE");

} // BubbleSort


Explanation / Answer

#include <iostream>

#include <iomanip>
int main()

{

const short arraySize = 10;

short arr [arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37};

short hold;std::cout <<"Data items in the original order: "<<std::endl;

for ( std::size_t i = 0; i < arraySize; ++i )

{

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

} // for i
std::cout<<std::endl;

for ( std::size_t pass = 0; pass < arraySize; ++pass )

{f

or(std::size_t j = pass+1; j < arraySize ; ++j)

{

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

{

hold = arr [ j ];

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

arr [ j + 1 ] = hold;

}

// if

std::cout<<"Pass "<<pass + 1<<": ";

for ( std::size_t i = 0; i < arraySize; i++ )

{

std::cout<<std::setw ( 4 ) << arr [ i ] << " ";

}

std::cout<<std::endl;
}

// for j

}

std::cout <<" ";std::cout<<"Data items are sorted.:"<<std::endl;




} // BubbleSort

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote