Help with bubble sort
Oct 26, 2012 at 11:25pm UTC
My bubble sort shows up as all 0's
This is the first algorithm i am doing. My teacher explained the algorithm in the class but i don't understand him very well because english is not my first language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include <iostream>
using namespace std;
const int N=10;
void input(double &s, int arynum[]){
s=0;
for (int i = 0; i<N; i++){
cout<<"grade" <<i+1<<" " ;
cin>>arynum[i];
s= s+arynum[i];
}
}
void swap(int &a, int &b){
int temp;
temp=a;
a=b;
b=temp;
}
void sort(int arynum[]){
for (int a=0; a>N; a++){
for (int b=0; b<N; b++){
if (arynum [a]>arynum[b]){
swap (arynum[a], arynum[b]);
}
}
}
}
double calc (double sum, int arynum[]){
double mean=sum/N;
}
void output(double sum, int arynum[]){
cout<<"your mean is: " << sum/N;
for ( int a = 0; a < N; a++ ){
cout<<" " << arynum[N];
}
}
int main(){
int numbers[N];
double mean;
double median;
double n;
double s;
input(s, numbers);
sort(numbers);
output(s, numbers);
}
Oct 27, 2012 at 12:32am UTC
You get a bunch of zeroes because you are using N instead of a as index on line 58.
If you change > to < on line 28 the sorting will work.
Topic archived. No new replies allowed.