I have an assignment in my Advanced C++ class using Visual C++. I have most of it but there is a problem. After each pass it is supposed to display the results. I can get it to display the unsorted list, display the sorted list but not after each pass. Can anyone help? Here is the code I have so far. Thank you!

// This program uses the bubble sort algorithm to sort an
// array in ascending order.
#include <iostream>
using namespace std;

// Function prototypes
void sortArray(int [], int);
void showArray(int [], int);

int main()
{
   // Array of unsorted values
   int values[6] = {7, 2, 3, 8, 9, 1};

   // Display the values.
   cout << "The unsorted values are:\n";
   showArray(values, 6);

   // Sort the values.
   sortArray(values, 6);

   // Display them again.
   cout << "The sorted values are:\n";
   showArray(values, 6);
   return 0;
}

//***********************************************************
// Definition of function sortArray                         *
// This function performs an ascending order bubble sort on *
// array. size is the number of elements in the array.     *
//***********************************************************

void sortArray(int array[], int size)
{
   bool swap;
   int temp;

   do
   {
      swap = false;
      for (int count = 0; count < (size - 1); count++)
      {
         if (array[count] > array[count + 1])
         {
            temp = array[count];
            array[count] = array[count + 1];
            array[count + 1] = temp;
            swap = true;
         }
      }
   } while (swap);
}

//*************************************************************
// Definition of function showArray.                          *
// This function displays the contents of array. size is the *
// number of elements.                                        *
//*************************************************************

void showArray(int array[], int size)
{
   for (int count = 0; count < size; count++)
      cout << array[count] << " ";
   cout << endl;
}

Recommended Answers

All 3 Replies

you have to add ShowArray() at the bottom of the do loop in the sort function.

I tried putting in the arguements and it says that they are not recognized?? I am so sorry. I know it is probably easier than I am making it but wow. I am new this and just learning. I have to add these cout statements as well:
After pass #1 the values are 237819
and so on down the list.


added to the post was the error message: error C2660: 'showArray' : function does not take 0 arguments

>>I tried putting in the arguements and it says that they are not recognized??
Since I can't see your monitor you will have to post the code you write. My eyesight isn't that good you know. :)

>>error C2660: 'showArray' : function does not take 0 arguments
Of course ShowArray takes two arguments -- did you really think you can just pluck something anywhere you want and it will work? Try reading the program where you see it called in other places and do the same thing in the sort function. And notice that the sort function changed the name of values to array, so you have to change it in the ShowArray() function call too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.