//Lab 2: bubblesort.cpp //This program sorts an array\'s values into ascendingor
ID: 3613877 • Letter: #
Question
//Lab 2: bubblesort.cpp
//This program sorts an array's values into ascendingorder.
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
int main()
{
const int arraySize =10; // size of array a
int a[ arraySize ] = { 2, 6, 4, 8,10, 12, 89, 68, 45, 37 };
int hold; // temporarylocation used to swap array elements
cout << "Data items in originalorder ";
//output original array
for ( inti = 0; i < arraySize; i++ )
cout << setw( 4 ) << a[ i];
//bubble sort
//loop to control number of passes
/*Write a for header to loop for one iteration less than thesize
ofthe array */
for( intj =0; j < arraySize; j++ )
cout <<setw(4) << a[j];
{
//loop to control number of comparisons per pass
/*Write a for header to iterate j from 0 and keep
looping while j is less than arraySize - 1 */
for( intj=0; j < arraySize - 1; j++ )
cout <<setw(4) << a[j];
{
//compare side-by-side elements and swap them if
//first element is greater than second element
/*Write an if statement to test if element j is greaterthan
element j + 1 */
for(intj=0; j > j + 1; j++ )
cout <<setw(4) << a[j];
{
/*Write code to swap the values in element j and
element j + 1, using hold as temporary storage*/
swap( j,j + 1 )
} // end if
} // end for
} // end for
cout << " Data items in ascendingorder ";
//output sorted array
for ( intk = 0; k < arraySize; k++ )
cout << setw( 4 ) << a[ k];
cout << endl;
return 0; // indicatessuccessful termination
} // end main
Explanation / Answer
please rate - thanks the biggest problem was your nested loops both had j as theiterator any lines changed or added in red - I think I got them all // Lab 2: bubblesort.cpp // This program sorts an array's values into ascending order. #include using std::cout; using std::endl; #include using std::setw; void swap(int &,int &); int main() { const int arraySize = 10; // size of array a int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int hold; // temporary location used to swap array elements coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.