Why does this code not function....

This is a discussion on Why does this code not function.... within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> class MyClass { public: int *array; int *tmparray; MyClass(int s) { size = s; array = new ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Why does this code not function....

    Code:
    #include <iostream.h>
    
    class MyClass {
    public:
        int *array;	
        int *tmparray;
    	
    	MyClass(int s) { 
    		size = s;
    		array = new int[size];
    	}
    
    	~MyClass() {
    		delete [] array; 
    	}
    
    	int get_length() {
    		return size;
    	}
    	
    	void resize(int s) { 
    		delete [] array;
    		size = s;
    		array = new int[size];
    	}
    	
    	void clear_all() {
    		for (c = 0; c < size; ++c)
    			array[c] = NULL;
    	}
    	
    	int get_average() { 
    		for (c = 0; c < size; ++c)
    			average+=array[c];
    		if (size != 0)
    			return (average/size);
    		else
    			return (-1);
    	}
    	
    	int find_low() { 
    		lowspot = 0;
    		for (c = 0; c < size; ++c) {
    			if (array[c] < array[lowspot])
    				lowspot = c;
    		}
    		return (lowspot);
    	}
    	
    	void output_all() { 
    		for (c = 0; c < size; ++c) 
    			cout << array[c] << endl;
    	}
    	
    	void sel_sort() { 
    		tmparray = new int [size];
    		tmpl = 0;
    		for (c = 0; c < size; ++c) {
    			 tmpl = find_low();
    			 tmparray[c] = array[c];
    			 array[c] = 99999;
    		}
    		array = tmparray;
    		delete [] tmparray;
    	}
    
    private:
    	int size;
    	int c;
    	int average;
    	int lowspot;
    	int tmpl;
    };
    
    int main()
    {
    	
    	int averg =0;
    	MyClass Array(3);
    	Array.array[1] = 6;
    	Array.array[2] = 3;
    	Array.array[3] = 9;
    	Array.output_all;
    	Array.sel_sort;
    	Array.output_all;
    
    	return(0);
    }
    I'm not very good at OOP yet, so I was wondering what I can do to this code to get rid of the error (probably from memory management) and to make the methods output_all and sel_sort work properly... =|

    If anyone could help, I'd appreciate it

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You should keep variables at the lowest scope possible. One of your problems is that you're using the same variable for all the loops, so one loop is messing up another. Also the selection sort should probably be done in place -

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    class MyClass {
    public:
        int *array;	
        	
    	MyClass(int s) { 
    		size = s;
    		array = new int[size];
    	}
    
    	~MyClass() {
    		delete [] array; 
    	}
    
    	int get_length() {
    		return size;
    	}
    	
    	void resize(int s) { 
    		delete [] array;
    		size = s;
    		array = new int[size];
    	}
    	
    	void clear_all() {
    		for (int c = 0; c < size; ++c)
    			array[c] = NULL;
    	}
    	
    	int get_average() { 
    		for (int c = 0; c < size; ++c)
    			average+=array[c];
    		if (size != 0)
    			return (average/size);
    		else
    			return (-1);
    	}
    	
    	int find_low(int start=0) { 
    		int lowspot = start;
    		for (int c = start; c < size; ++c) {
    			if (array[c] < array[lowspot])
    				lowspot = c;
    		}
    		return lowspot;
    	}
    	
    	void output_all() { 
    		for (int c = 0; c < size; ++c) 
    			cout << array[c] << endl;
    	}
    	
    	void sel_sort() { 
    		
    		for ( int c = 0; c < size; ++c) {
    			 int low = find_low(c);
    			 int temp = array[c];
    			 array[c]=array[low];
    			 array[low]=temp;
    		}
    		
    	}
    
    private:
    	int size;
    	int average;
    };
    
    int main()
    {
    	
    	int averg =0;
    	MyClass Array(5);
    	Array.array[0] = 6;
    	Array.array[1] = 3;
    	Array.array[2] = 9;
    	Array.array[3] = 22;
    	Array.array[4] = 1;
    	Array.output_all();
    	Array.sel_sort();
    	Array.output_all();
    
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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