Hey guys I have a couple of questions.

I am trying to build a program that will prompt for the user to enter the size of an array.

I've searched the web and several C++ books for guidance on this but have not been able to find anything of significant or easily understandable help.

So my questions are:

1. What code should be used to allow the user to enter the size of the array being used?

2. Will this code allow for the bubble sort algorithm?

Thanks everybody!

- Newb Programmer

Recommended Answers

All 4 Replies

To ask the user to enter the size of an array use:

int size;
cout<<"enter size of the array";
cin>>size;

You can then use a for loop to have the user enter the values of the array.

You can either use standard containers or use dynamic arrays. I will show
you how to use both :


using std::vectors

#include<iostream>
#include<vectors>

using namespace std;

int main()
{
      vector<int>  Array;

      cout<<"Enter the array size : ";
      int Size = 0;
      cin >> Size;

    Array.resize(Size); 

    //now populate

    for(int i = 0; i < Array.size(); i++)
           Array[i] = i;
    
    bubbleSort(vector); //where bubbleSort is on you have created

   return 0;
 
}

I suggest the above than the one below :

using dynamic array :

#include<iostream>
using namespace std;

int main()
{
    int * Array;
    cout<<"Enter array size : ";
    int Size =0;
    cin >> Size;
   
    Array = new int[Size]; //allocate memory for our array

   for(int i = 0; i < Size; i++)
           Array[i] = i;

     bubbleSort(Array, Size); //bubbleSort it
   return 0;
}

Hey guys thank you so much for the input on setting it up for user prompted array SIZE.

So far I have the program working the way I want it too until I get to the bubbleSort() algorithm.

Here is the code I got 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

Here are the errors I am getting:
------ Build started: Project: test2, Configuration: Debug Win32 ------
Compiling...
test2.cpp
.\test2.cpp(56) : error C2144: syntax error : 'int' should be preceded by ')'
.\test2.cpp(56) : error C2660: 'bubbleSort' : function does not take 0 arguments
.\test2.cpp(56) : error C2059: syntax error : ')'
Build log was saved at "file://c:\Documents and Settings\Revort\Desktop\test2\test2\Debug\BuildLog.htm"
test2 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


WHAT SHOULD I DO TO AVOID THESE ERRORS, searched through forums but still cannot find help on this.

Thanks!!

This is not a function call :

bubbleSort(int * Array, int SIZE);

this is :

bubbleSort( Array,  SIZE);
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.