Hi,

I have recently completed a bubble sort array program, which works fine. And is here :

#include <iostream>
#include <ctime>

using namespace std;

void bubbleSort(int Array[], const int arraySize)

{
	for(int j = 0 ; j < arraySize; j++)
	{
		for(int number = j + 1; number < arraySize; number++)
		{
			if(Array[j] > Array[number]) // swap if bigger
				swap(Array[j],Array[number]);
		}
	}
}

void print(int Array[], const int size)

{
	for(int i = 0; i < size; i++)

		cout << Array[i] << " ";
		cout<<endl<<endl;
}

int main()
{
	srand(unsigned(time(0)));
	int ArrayOne[10] = {0};
	//populate with random numbers

	for(int i = 0; i < 10; i++){

		ArrayOne[i] = rand() % 100;
	}

	cout<<"Unsorted array : ";
	print(ArrayOne,10);
	cout<<"After being sortedt : ";
	bubbleSort(ArrayOne,10);
	print(ArrayOne,10);

	return 0;

}

Basically as an extension of the task we have to dynamically allocate the array, and take in a value from the user for the size of the array.
I know that this would involve

int n = 0;
	int* ArrayOne = NULL;

	cout << "Please enter number of items: ";
	cin >> n;
delete[] Array One

But im having trouble implementing this into my original program.

Any pointers/help would be greatly appreciated.

Recommended Answers

All 9 Replies

It looks like you've got it figured out. No changes need to be made to bubbleSort or print. The only difference is swapping out the array for the pointer, and the code for getting a size from the user as well as performing the memory management.

yep, well I'v changed it the main to this

int main()
{
	srand(unsigned(time(0)));
	int n = 0;
	int* ArrayOne = NULL;
	ArrayOne = int ArrayOne[n] = {0};

	for(int i = 0; i < 10; i++){

		ArrayOne[i] = rand() % 100;
	}

	cout << "Please enter number of items: ";
	cin >> n;
	cout<<"Unsorted array : ";
	print(ArrayOne);
	cout<<"After being sortedt : ";
	bubbleSort(ArrayOne);
	print(ArrayOne, siz);

	cout << "Deallocating array" << endl;
	delete[] ArrayOne;

	return 0;

}

Im still getting a few errors, do I need to keep cons int size now that I'm using n as the array size?

Oh wow. I guess you really did need that kind of help. Compare and contrast with what you just posted:

int main()
{
  srand(unsigned(time(0)));
  int n = 0;

  cout << "Please enter number of items: ";
  cin >> n;

  int* ArrayOne = new int[n];

  for(int i = 0; i < n; i++){
    ArrayOne[i] = rand() % 100;
  }

  cout<<"Unsorted array : ";
  print(ArrayOne, n);
  cout<<"After being sortedt : ";
  bubbleSort(ArrayOne, n);
  print(ArrayOne, n);

  cout << "Deallocating array" << endl;
  delete[] ArrayOne;

  return 0;
}

ha yes obviously I did! Sorry for being a dumbass, just starting out learning c++, thing is when it comes together its alright, at the moment being held back by missing simple things like that, its a pain because wierdly Im actually enjoying it! Thanks very much for your help though.

hi, back again..

im trying to adapt the program to include a new class Number Storage which will hold three functions and the two private member variables, size being the current array size and then the array pointer.

This is what I have so far

#ifndef NUMBERSTORAGE_H
#define NUMBERSTORAGE_H

#include <iostream>
#include <ctime>
using namespace std;

class BumberStorage

{
private:
	int n = 0;
	int* ArrayOne = NULL;
};

void generate(int n)
{
	srand(unsigned(time(0)));
	n = 0;
	int* ArrayOne = NULL;

	ArrayOne = new int[n];

	for(int i = 0; i < n; i++){
		ArrayOne[i] = rand() % 100;
	delete[] ArrayOne;
}




void bubbleSort(int Array[], const int arraySize)

{
	for(int j = 0 ; j < arraySize; j++)
	{
		for(int number = j + 1; number < arraySize; number++)
		{
			if(Array[j] > Array[number]) // swap if bigger
				swap(Array[j],Array[number]);
		}
	}
}

void print(int Array[], const int size)

{
	for(int i = 0; i < size; i++)

		cout << Array[i] << " ";
	cout<<endl<<endl;
}


#endif
#include <iostream>
#include "NumberStorage.h"
#include <cstdlib>
using namespace std;

int main()
{
	cout << "Please enter number of items: ";
	cin >> n;
	cout <<endl;
	cout<<"Unsorted array : ";
	print(ArrayOne, n);
	cout<<"After being sortedt : ";
	bubbleSort(ArrayOne, n);
	print(ArrayOne, n);

	return 0;
}

Quite a few errors as you can see, I understand some might be pretty simple for you guys but I just cant see where I'm going wrong.

Cheers

Do do not want to put the implementation of your code inside you header file. This file is for declarations only.
like this for example:

file.h:

class foo
{
private:
    int n;
public:
   foo();
   ~foo();
   void member_func();
};

file.cpp

#include <iostream>

foo::foo(){
    n = 0;
}

foo::~foo() 
{ // destructor code here
}

void foo::member_func(){
    std::cout << "member";
}

int main(){
   foo f;
   f.member_func();
   return 0;
}

ps. If you "borrow" code from someone else, it helps to tell us. Now we overestimated your knowledge of C++

You cannot initialize the member variables inside the class:

//WRONG
int n = 0;
int* ArrayOne = NULL;
//CORRECT
int n;
int *ArrayOne;
public : classConstructor(int _n, int Size) { n = _n; ArrayOne = new int[Size]; }

Get that done first.

Hi guys, changed it too this...hopefully more along the right lines, still getting a few errors??

#include <iostream>
#include "NumberStorage.h"
#include <cstdlib>
using namespace std;

void NumberStorage::generate(int n)
{
	srand(unsigned(time(0)));
	n = 0;
	int* ArrayOne = NULL;

	ArrayOne = new int[n];

	for(int i = 0; i < n; i++){
		ArrayOne[i] = rand() % 100;
	delete[] ArrayOne;
}

void NumberStorage::bubbleSort(int Array[], const int arraySize)

{
	for(int j = 0 ; j < arraySize; j++)
	{
		for(int number = j + 1; number < arraySize; number++)
		{
			if(Array[j] > Array[number]) // swap if bigger
				swap(Array[j],Array[number]);
		}
	}
}

void NumberStorage::print(int Array[], const int size)

{
	for(int i = 0; i < size; i++)

		cout << Array[i] << " ";
	cout<<endl<<endl;
}


int main()
int n;

{
	cout << "Please enter number of items: ";
	cin >> n;
	cout <<endl;
	cout<<"Unsorted array : ";
	print(ArrayOne, n);
	cout<<"After being sortedt : ";
	bubbleSort(ArrayOne, n);
	print(ArrayOne, n);

	return 0;
}
};

header

#ifndef NUMBERSTORAGE_H
#define NUMBERSTORAGE_H

#include <iostream>
#include <ctime>
using namespace std;

class NumberStorage
{
private :

int n;
int *ArrayOne;

public : 
void generate(int n);
void bubbleSort(int Array[], const int arraySize);
void print(int Array[], const int size);
};
#endif

This function is a total mess :

void NumberStorage::generate(int n)
{
	srand(unsigned(time(0)));
	n = 0;
	int* ArrayOne = NULL;
 
	ArrayOne = new int[n];
 
	for(int i = 0; i < n; i++){
		ArrayOne[i] = rand() % 100;
	delete[] ArrayOne;
}

Think about what you are doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.