Hi I am trying to write this program but I having trouble with an array. I used bubble sort to sort the array but when I try to display the array, it ends up with a bunch of garbage numbers not part of the array. If I comment out the the sorting code the array outputs just fine. I am using old style c++ not .net in MS Visual C++ 2003 .net. Any help is appreciated thanks.
Code:#include <iostream> #include "stdafx.h" #using <mscorlib.dll> using namespace std; #include "stats.h" #include <iomanip> Stats::Stats() { mean = median = mode = std= 0; } void Stats::inputList() { for (int i =0; i< 5; i++) { cout <<"Input:"; cin >> List[i]; } cout << "\n"; SortList(List); } void Stats::SortList(int List[]){ int temp =0; for (int i =0; i<4; i++) { for( int k =0; k <4; k++) if( List[i] > List[i+1] ){ temp = List[i]; List[i] = List[i+i]; List[i+1] = temp; } } display(List); } void Stats::display(int list[5]) { for (int j=0; j <5; j++) cout<<setw(4)<<list[j] ; cout<<endl; } Stats::~Stats() { }Code:#ifndef stats_H #define stats_H #include <iostream> class Stats { public: Stats(); ~Stats(); void inputList(); void SortList( int []); void calcMean(int []); void calcMedian(int []); void calcMode(int []); void calcStdDev(int []); void display(int []); private: int mean, median, mode, std; int List [5]; };Code:#include "stdafx.h" #include "stats.h" #using <mscorlib.dll> int main() { Stats stat; stat.inputList(); return 0; }



LinkBack URL
About LinkBacks


