5 Replies - 707 Views - Last Post: 02 April 2013 - 07:56 AM Rate Topic: -----

#1 boxchamp  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 22-November 10

populating an array using classes and pointers

Posted 01 April 2013 - 04:38 PM

Hey Guys and Gals,

I am doing a personal project to play around with the different types of sorting algorithms and am having a problem just populating an array with random numbers and getting the array to print using a class. I think that maybe I don't understand pointers enough to get this to work. I am originally a java programmer. I get no errors in my output and the program compiles but with no data. Any suggestions?

#ifndef ARRAY_SORT
#define ARRAY_SORT

class ArraySort{
private:
	 int SIZE;
	 int* p_arr;

public:
	ArraySort();
	~ArraySort();
	void fillArray();
	void printArray();
	void insertionSort(int* p_arr);
	void mergeSort(int* p_arr);

};
#endif


#include "Array_Sort.h"
#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;
ArraySort::ArraySort()
{
	const int SIZE = 25;
	p_arr = new int[SIZE];
}
ArraySort::~ArraySort()
{
	delete p_arr;

}
void ArraySort::fillArray()
{
	srand((unsigned)time(NULL));
	for(int i = 0; i < SIZE; i++)
	{
		p_arr[i] = 1 + rand() % 5000;
	}

}
void ArraySort::printArray()
{
	for(int i = 0; i < 25 ; i++)
	{
		cout<< p_arr[i] + " "<< endl;
	}
}
void ArraySort::insertionSort(int* p_arr)
{

}
void ArraySort::mergeSort(int* p_arr)
{

}


#include "Array_Sort.h"
#include <iostream>
using namespace std;
int main()
{
	int x = 0;
	//create new object and initialize it to random numbers between 1-5000
	ArraySort *obj = new ArraySort();

	obj->fillArray();
	obj->printArray();

	//print array -should be random

	//call insertion sort function -will be sorted
	cin>> x; // used just to stop the screen from flashing and not being able to see output
}


Is This A Good Question/Topic? 0
  • +

Replies To: populating an array using classes and pointers

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: populating an array using classes and pointers

Posted 01 April 2013 - 04:56 PM

The only thing I see wrong here is your destructor. When you allocate an array with new, you must delete it as an array, like this:

delete [] p_arr;

Was This Post Helpful? 0
  • +
  • -

#3 boxchamp  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 22-November 10

Re: populating an array using classes and pointers

Posted 01 April 2013 - 05:34 PM

View PostJackOfAllTrades, on 01 April 2013 - 04:56 PM, said:

The only thing I see wrong here is your destructor. When you allocate an array with new, you must delete it as an array, like this:

delete [] p_arr;

Thanks that was dumb on my part. Still no output. Am I doing anything wrong with the pointers? Is it better to maybe define the member variable as a stack based array and not a heap based one? In java I would declare a call as I did in this the main() but without the pointer obviously. Is it the same in C++ or is it better to just do it like:

ArraySort obj();
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3097
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: populating an array using classes and pointers

Posted 01 April 2013 - 05:36 PM

Change:
for(int i = 0; i < SIZE; i++)

to:
for(int i = 0; i < 25; i++)

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: populating an array using classes and pointers

Posted 02 April 2013 - 03:21 AM

OK, I see.

First, you are not setting your member variable SIZE, because in your constructor you're creating a shadow local variable SIZE which instead is getting set.

Second, are you sure this

cout<< p_arr[i] + " "<< endl;
is how you want to print? The + in other words?
Was This Post Helpful? 0
  • +
  • -

#6 boxchamp  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 78
  • Joined: 22-November 10

Re: populating an array using classes and pointers

Posted 02 April 2013 - 07:56 AM

View PostJackOfAllTrades, on 02 April 2013 - 03:21 AM, said:

OK, I see.

First, you are not setting your member variable SIZE, because in your constructor you're creating a shadow local variable SIZE which instead is getting set.

Second, are you sure this

cout<< p_arr[i] + " "<< endl;
is how you want to print? The + in other words?


That was the problem. changing how I printed the array made all the difference.
I dont seem to have the code blocks when I reply to your post so i will post my new print array method. I used setw()instead of the + " ". I think that I am too used to java and keep making dumb little mistakes trying to do things as I would in java.

I dont understand the first part of your post. How would I need to set the size of the array? pass it as an arg to the ctor?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1