Hey all!

I am trying to write a program that allows for:

1.) The user to enter the SIZE of an array.
2.) Prints random element numbers.
3.) Saves the random element numbers printed in the array.
4.) Bubble sorts and then prints the randomly printed numbers.

Here is what I have so far:

#include <iostream>
using namespace std;

	int * Array;
	int SIZE = 0;
	int n = 0;
	
void bubbleSort(int * Array,int SIZE)//Bubble sort function 
{
	int i,j;
	for(i=0;i<SIZE;i++)
	{
		for(j=0;j<i;j++)
		{
			if(Array[i]>Array[j])
			{
				int temp=Array[i]; //swap 
				Array[i]=Array[j];
				Array[j]=temp;
			}

		}

	}

} //End bubbleSort function


int main() 
{ 
	cout << "Please enter the \"SIZE\" (number of Elements) you want for this array: " << endl ; // Prompt user for SIZE
	cin >> SIZE; // Allow input of SIZE from user
	cout << endl; // Print blank line
	cout << "The \"SIZE\" (# of Elements) you have chosen for this array is: \"" << SIZE << "\" Elements." << endl ; //Print SIZE chosen to user
	cout << endl; // Print blank line
	cout << "Welcome to \"Array[" << SIZE << "]\"!" << endl; // Print welcome message
	cout << endl; // Prints blank line

	Array = new int[SIZE]; // Allocates memory for array

	cout << "The \"unsorted\" random Elements for \"Array[" << SIZE << "]\" are as follows:..." << endl;
	// Prints to user notification that the following is the unsorted Array SIZE elements
	cout << endl; //Prints blank line

	for(int i = 0; i < SIZE; i++)
    {
	   Array[i] = i; // Array declaration, Array is equal to i
	   cout << "Array[" << n++ << "] " << rand() << endl; // Print SIZE random numbers in array[i]
    }
	
	cout << endl;
	cout << "The \"sorted\" random Elements for \"Array[" << SIZE << "]\" are as follows:..." << endl;
	// Prints to user notification that the following is the sorted Array SIZE elements
	cout << endl;

	bubbleSort(int * Array, int SIZE);

	return 0; // Indicates success

}// End function main

Thank you!!!!

Recommended Answers

All 2 Replies

#
for(int i = 0; i < SIZE; i++)
#
{
#
Array[i] = i; // Array declaration, Array is equal to i
#
cout << "Array[" << n++ << "] " << rand() << endl; // Print SIZE random numbers in array[i]
#
}

over here u r assigning Array = i ...
so it will save the value of i into array instead of value of rand() function..

and second thing ..
rand() function will return value between 0 to 1, that mean all float value .. like .. 0.25 ,0.02 etc ...
if u want that than u should directly assign that function value to float array not int array ..

and if u want int valu than .. decide the range of that value and multiply ur result of rand() function with 10 or 100 etc... whtever u want ...
than after u should check whether that random number already ther or not in array .. if not than take that no as inout so u will get unique no of eandom values ...

1) Too much comment.

2) That is not how to call a function :

bubbleSort(int * Array, int SIZE);

it should be

bubbleSort(Array,Size);

3) you bubble sort is wrong, j should start out as i+1 until the SIZE.

4) You aren't populating your array with random numbers, here is one
way to achieve this :

for(int i = 0; i < SIZE; i++)
      Array[i] = rand()%SIZE;  // random number from 0->SIZE-1

5) You will want to seed the Random number generator like so at the beginning of main :

srand( time (0) );

To use the time function, you need to add this to your header :

#include<ctime>

The good thing is that you have some code, you used code tags, and
you are on the right track.

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.