vendredi 6 mars 2015

Pointer to an array in C++


I'm a C++ noob, I guess that's the reason for the problems. I'm trying to write bubble sort using C- style arrays in C++(this is a conscious decision, so please don't tell me use vectors, I know vectors are better). Here's my code



#include <iostream>
#include <cstdlib>

void bubble_sort(int* , int);

int main(){
int a[8] = {12, 6,17,8,3,2,0,5};
//int size = sizeof(a)/sizeof(int);
bubble_sort(a,8);
}//end of main

void bubble_sort(int* a, int size){

for(int j=0; j< size-1; j++){
for(int i=0; i<size-2; i++){
if(a[i] > a[i++]){
int temp = a[i++];
a[i++] = a[i];
a[i] = temp;
}//end of if
}//end of inner for
}//end of outer for

int k=0;
while(k<size){
std::cout<< a[i] << "," ;
k++
}
std::cout<<endl;

}//end of bubble_sort


Here's my error



src.cpp: In function ‘int main()’:
src.cpp:9:19: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
bubble_sort(*a,8);
^
src.cpp:4:6: error: initializing argument 1 of ‘void bubble_sort(int*, int)’ [-fpermissive]
void bubble_sort(int* , int);
^
src.cpp: In function ‘void bubble_sort(int*, int)’:
src.cpp:26:19: error: ‘i’ was not declared in this scope
std::cout<< a[i] << "," ;
^
src.cpp:28:3: error: expected ‘;’ before ‘}’ token
}
^
src.cpp:29:14: error: ‘endl’ was not declared in this scope
std::cout<<endl;
^
src.cpp:29:14: note: suggested alternative:
In file included from /usr/include/c++/4.8/iostream:39:0,
from src.cpp:1:
/usr/include/c++/4.8/ostream:564:5: note: ‘std::endl’
endl(basic_ostream<_CharT, _Traits>& __os)


Any help appreciated.




Aucun commentaire:

Enregistrer un commentaire