Using bubble sort in an array inside a class

closed account (N04SwA7f)
Hello everybody,
I am a computer science student and I am trying to finish my first course project for this semester but I am having trouble with implementing bubble sort on the array as well as overloading cin and cout. My code is below, please help.

#include <iostream>
using namespace std;

template <typename T>
class Array {
private:
T arr[10];
int length;
public:
Array (T arr2[]=(0,0,0,0,0,0,0,0,0,0), int s=10){
for (int i=0;i<s;i++){
*(arr+i)=*(arr2+i);
}
}
void display();
void bubbleSort(T*,int);
void swap (T *p1,T *p2);
void total(T a[],int);
friend ostream& operator<<(ostream&out,Array arr);


};
ostream& operator<<(ostream&out,Array arr)
{
for (int i=0;i<10;i++){
out<<*(arr+i)<<' ';
}
}
template <typename T>
void Array<T>::total(T a[],int n){
T sum;
for (int i=0;i<n;i++){
sum+=*(a+i);
}
cout<<sum<<endl;
}
int main() {
int arr[10]={0,1,2,3,4,5,6,7,8,9},arr2[10]={2,3,5,6,7,4,3,4,5,3},arr3[10];

Array<int>a(arr,10);
Array<int>a2(arr2,10);
Array<int>a3(arr3,10);
a.display();
a.bubbleSort(arr,10);
a.total(arr,10);
cout<<arr>arr2;
//cout<<a;
a3.input();
a3.display();
a.bubbleSort(arr3,10);
return 0;
}

P.S I have left out the working functions
Last edited on
What does it do that you don't want it to do?

What does it not do that you wish it did?
[] notation for arrays is probably the way to go. You will also be using this notation with vectors, which is it sort of odd that you know template classes without knowing vector (?!).

arr[3] is the same as *(arr+3) but much, much easier to follow/read/edit/etc.

Topic archived. No new replies allowed.