Question: I have the code in Java. I need the same in C++ but by pass by reference. class BubbleSort { // function to sort array

I have the code in Java. I need the same in C++ but by pass by reference.

class BubbleSort

{

// function to sort array a using bubble sort

public static void bubble_sort(int a[])

{

int i, j, x;

for( i = 0 ; i < a.length - 1 ; i++)

{

for( j = 0 ; j < a.length - i - 1 ; ++j)

{

if (a[ j + 1 ] < a[ j ])

{

// swap the j and j + 1 th element of a

x = a[j];

a[ j ] = a[ j + 1 ];

a[ j + 1 ] = x;

}

}

}

}

public static void display(int a[])

{

int n = a.length;

for (int i=0; i<n; ++i)

System.out.print(a[i] + " ");

System.out.println();

}

public static void main(String[] args)

{

int maxSize = 10;

// initialize the array

int[] a = new int[maxSize];

// fill the array with random elements

for(int j = 0; j < maxSize; j++)

{

int n = (int)( java.lang.Math.random()*(maxSize-1) );

a[j] = n;

}

// sort array

bubble_sort( a );

System.out.print("Sorted array : ");

display(a);

System.out.println(" Maximum Value : " + a[ a.length - 1 ]);

System.out.println(" Miniimum Value : " + a[ 0 ]);

}

}

A C++ program for sorting an array and finding the maximum and minimum elements in the end. Also, the parameter passing method should be call by REFERENCE.

Please Help.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!