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];
[] 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.