Some help

This is a discussion on Some help within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to write this program but I having trouble with an array. I used bubble sort to ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Some help

    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > List[i] = List[i+i];
    Look very closely at that line.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    Quote Originally Posted by swoopy
    > List[i] = List[i+i];
    Look very closely at that line.
    lol i am an idiot thanks should be plus 1. Good eye swoopy. Thanks for the help.

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I wouldn't call Standard C++ the 'old style' just an observation.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Why not just use std::sort()? The Standard guarantees O(n log n) time, while your version of bubblesort is unnecessary and unoptimized.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21