Here is my code of two arrays. they are sorted by buuble and selection sorting methods.
I have put a pass counter to display each modification but i am unable to display the array after each pass/modification in both sorting methods.
please suggest:

#include <iostream>
#include <iomanip>

using namespace std;
// Function prototypes
void showArray1(int [], int);
void bubbleSortArray1(int [], int);
void showArray2(int [], int);
void selectionSortArray2(int [], int);

 int main()
 {
 const int SIZE = 8;  //size of the arrays
 //two identical arrays of 8 integers 
 
 int array1[SIZE] = {7, 2, 3, 8, 9, 1, 5, 4};
 int array2[SIZE] = {1, 5, 7, 8, 9, 3, 2, 0};  //??
 
 //display First array
 cout << "The unsorted values in array1 are:\n";
showArray1(array1, SIZE);
  
 //*call the function to sort array 1[ Binary sort]
bubbleSortArray1(array1, SIZE);
cout << "The Bubble sorted values of array1 are:\n";
showArray1(array1, SIZE);
  
 //display second array
 cout << "The unsorted values in array2 are:\n";

 showArray2(array2, SIZE);
 
//call the function to selection sort array2 in ascending order
 
selectionSortArray2(array2, SIZE);
cout << "The Selection sorted values of array2 are:\n";

showArray2(array2, SIZE);

 cout << "End of Program.\n";
   system("PAUSE");
	return 0;
}
/**********Function to display array1***************/
void showArray1(int array1[], int elems)
{
for (int count = 0; count < elems; count++)
cout << array1[count] << " ";
cout << endl;
}
  /**********Function to display array2***************/
void showArray2(int array2[], int elems)
{
     for (int count = 0; count < elems; count++)
         cout << array2[count] << " ";
         cout << endl;
}
//**********Function to BUBBLE sort array1***************/
//***********Modified to print elements position after each pass*****/
/***********************************************************
This function performs an ascending-order bubble sort on 
 array1. The parameter elems holds the number of elements 
in the array. 
***********************************************************/
void bubbleSortArray1(int array[], int elems)
{
          int temp;
          bool swap;
  // counts number of modifications made to elements 
		int pass = 0;
     do
     { swap = false;
            for (int count = 0; count < (elems - 1); count++)
            {
               if (array[count] > array[count + 1])
                  {
                    temp = array[count];
                    array[count] = array[count + 1];
                    array[count + 1] = temp;
                    swap = true;
                    }
            //cout<<"number of pass:"<<pass++<<endl;
            }
           cout<<"number of pass:"<<pass++<<endl;
     //  showArray1(array1,SIZE);
     
     } while (swap);
     //cout<<"number of pass:"<<pass++<<endl;
} 
//**********Function to selection sort array2***************/
//***********Modified to print elements position after each pass*****/
void selectionSortArray2(int array[], int elems)
{
     int startScan, minIndex, minValue;
      int pass=0;

      for (startScan = 0; startScan < (elems - 1); startScan++)
      {
          minIndex = startScan;
          minValue = array[startScan];
             for (int index = startScan + 1; index < elems; index++)
                 {
                 if (array[index] < minValue)
                    {
                     minValue = array[index];
                     minIndex = index;
                     }
                  //cout<<"number of pass:"<<pass++<<endl;
                  
                  }
          array[minIndex] = array[startScan];
          array[startScan] = minValue;
       
      cout<<"number of pass:"<<pass++<<endl;
       
       }
}

Function for disaplying array is:

void showArray1(int array1[], int elems)
{
for (int count = 0; count < elems; count++)
cout << array1[count] << " ";
cout << endl;
}

Dani AI

Generated

The immediate cause of the compile/runtime confusion is a scope/name mismatch inside the sorting function. already pointed out that the sorting routine only knows the local parameter name (not array1 from main), so calling the wrong identifier prevents you from printing the current contents. To show the array after each pass, call the display routine from inside the outer loop of the sorter and pass the sorting function’s local array (and its length). Increment and print the pass counter where it truly represents a completed pass.

A compact, modern pattern that prints after every pass (and avoids C-style pitfalls) uses a container plus a small print helper. Example (different from the code in the thread):

#include <vector>
#include <iostream>
#include <algorithm>

void print(const std::vector<int>& v) {
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
}

void bubble_show_each_pass(std::vector<int>& v) {
    for (size_t pass = 0; pass + 1 < v.size(); ++pass) {
        bool swapped = false;
        for (size_t i = 0; i + 1 < v.size() - pass; ++i) {
            if (v[i] > v[i+1]) { std::swap(v[i], v[i+1]); swapped = true; }
        }
        std::cout << "pass " << pass+1 << ": "; print(v);
        if (!swapped) break;
    }
}

Practical tips: use one generic print function that takes a const reference, prefer std::vector or std::array over C arrays for safer size handling, use size_t for sizes/indices, and place the print call after the outer pass (for selection sort, after swapping the found minimum into place). For modern container guidance see std::vector and the range-based for loop at range-for.

Recommended Answers

All 4 Replies

line #85, the name of one of your arguments is incorrect:

showArray1(array1, SIZE);

Also, just out of curiosity, why do you have two seperate functions to display an array.. it seems to me that one showArray() function could work for any array.

line #85, the name of one of your arguments is incorrect:

showArray1(array1, SIZE);

Also, just out of curiosity, why do you have two seperate functions to display an array.. it seems to me that one showArray() function could work for any array.

on my code I use the same on line 21 and 26. What is wrong???

showArray1(array1, SIZE);

on my code I use the same on line 21 and 26. What is wrong???

Starting at line #65 you are local inside of the constraints of the function.. you are no longer inside of the main() function. Additionally, in the function parameter list (line #65) you named the variable 'array'.. but then on line #85 you try to pass an argument named 'array1' into the showarray1() function.

There is no variable named 'array1' located inside of the scope of bubblesortarray1().

I achieve that

Starting at line #65 you are local inside of the constraints of the function.. you are no longer inside of the main() function. Additionally, in the function parameter list (line #65) you named the variable 'array'.. but then on line #85 you try to pass an argument named 'array1' into the showarray1() function.

There is no variable named 'array1' located inside of the scope of bubblesortarray1().

I achieve that!!!!

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.