Newbie C++ programmer here :)
I'm supposed to bubble sort an array to where I can eventually print it ascending and descending in the correct order. I am, however, having trouble passing the parameters for the bubbleSort and printElements functions.
I get the error:
error C2664: 'bubbleSort' : cannot convert parameter 1 from 'int []' to 'int'

Thanks for any help :)

#include <stdio.h>
#include <iostream>

using namespace std;
void bubbleSort(int,int);
void printElements(int, int);

int main()
{
   
   int maxIntegers=10;
   int integers[10];
   int i=0;

   cout << "Please enter 10 integers : \n\n";

   while ( i < maxIntegers)
   {
       cin >> integers[i];
       i++;
   }
   bubbleSort(integers,10);
   printElements(integers,10);
}

void bubbleSort(int *array,int length)//Bubble sort function 
{
	int h,j;
	for(h=0;h<10;h++)
	{
		for(j=0;j<h;j++)
		{
			if(array[h]>array[j])
			{
				int temp=array[h]; //swap 
				array[h]=array[j];
				array[j]=temp;
			}

		}

	}

}

void printElements(int *array,int length) //print array elements
{
	int i=0;
	for(i=0;i<10;i++)
	{
		cout<<array[i]<<endl;
	}
}

Recommended Answers

All 3 Replies

The prototype on line 4 is incorrect. Its not the same as the actual function you coded.

Ok, so I fixed the prototype, and I am able to get it ascend and descend by changing '<' to '>', but it seems like if i change it for one of the bubblesort functions it effects both. It is also possible that something is wrong with the way I am printing. I am supposed to print both ascending and descending, so if you can see any reasons why it prints ascending twice (or if you switch the relational operator descending twice) then please feel free to share. Any help you guys can give me would be awesome. Thanks ahead of time :)

#include <stdio.h>
#include <iostream>

using namespace std;
void bubbleDescend(int*,int);
void printElements(int*, int);
void bubbleAscend(int*, int);
void printAscend(int*, int);

int main()
{
   
   
   int n;
   int i=0;
   cout << "Please enter the size of the array \n";
   cin >> n;
   int *integers=new int [n];


   cout << "Please enter the integers : \n\n";

   while ( i < n)
   {
       cin >> integers[i];
       i++;
   }
   bubbleDescend(integers,n);
   bubbleAscend(integers,n);
   printElements(integers,n);
   printAscend(integers, n);
}

void bubbleDescend(int *p,int length)//Bubble sort function 
{
	int h,j;
	for(j=0;j<length;j++)
	{
		for(h=0;h>j;h++)
		{
			if(p[j]>p[h])
			{
				int temp=p[j]; //swap 
				p[j]=p[h];
				p[h]=temp;
			}

		}

	}

}

void bubbleAscend(int *q, int length)
{
	int m,t;
	for(m=0;m<length;m++)
	{
		for(t=0;t<m;t++)
		{
			if(q[m]<q[t])
			{
				int temp=q[m]; //swap 
				q[m]=q[t];
				q[t]=temp;
			}

		}

	}

}
void printElements(int *p, int length) //print array elements
{
	int i=0;
	for(i=0;i<length;i++)
	{
		cout<<p[i]<<endl;
	}
}
void printAscend(int *q, int length) //print array elements
{
	int s=0;
	for(s=0;s<length;s++)
	{
		cout<<q[s]<<endl;
	}
}

you are sorting the same array first in ascending order then again in descending. You need to reorder lines 28-31 so that it first sorts, prints, sorts again, then prints the second time. In orderwords line 30 should follow line 28.

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.