I am working on an assignment - to create and array, assign it 25 random integers, display the array, then sort the array in ascending order and display it again. I am not sure if I am passing the array to the functions correctly. It doesn't seem to be assigning them values. Could someone take a look and give me an idea of where I am going wrong?
////////////////////////////////////////////////////////////////
// //
// Lab 6: //
// sorter.cpp //
// //
// is a program that generates a list of 25 random //
// numbers and then sorts them using a bubble sort //
// algorithm. //
// //
////////////////////////////////////////////////////////////////
# include <iostream> //to include input output stream
# include <cstdlib> //to include functions from the standard
//library
# include <ctime> //to be able to use time function
usingnamespace std;
void swap ( int * x, int * y);
int rand (void);
void srand (unsignedint seed);
void pause_it ( );
void fill_er_up ( int [ ], int);
int sort_it_out ( int [ ], int);
int main ( )
{
int arraysize = 25; //a variable for the size of array
int array [ arraysize ]; //declaring an array of 25 integers
int col = 0; //declaring a variable for the number
//of columns to display
int ctr = 0; //a variable for loop counter
system ( "clear" ); //to clear the screen
fill_er_up ( array, arraysize ); //sending the array to the srand function
//to fill it with random numbers
system ( "clear" );
cout << "\t\tRANDOM NUMBERS\n\n";
for ( ctr = 0; ctr < arraysize; ctr ++)
cout << array [ ctr ] << endl;
pause_it ( );
system ( "clear" );
cout << "\t\tTada! Sorted Numbers!\n\n";
sort_it_out( array, arraysize );
for ( ctr = 0; ctr < arraysize; ctr++)
cout << array [ ctr ] << endl;
return 0;
}
//------------------------------------------------------------//
// swap function swaps the contents of two integers //
// Parameters: //
// x - address of first element //
// y - address of second element //
// Returns: //
// nothing //
//------------------------------------------------------------//
void swap ( int * x, int * y )
{
int temp; // temporarily holds first value while
// swapping
temp = * x; // saves first value
* x = * y; // copy second value to first location
* y = temp; // copy previous first value to second
// location
return;
}
//------------------------------------------------------------//
// srand function fills the array passed to it with //
// random numbers based on the system time //
//------------------------------------------------------------//
void fill_er_up ( int arrayfiller [ ], int size )
{
int i; // counter variable
srand ( (unsignedint) time (NULL) );
for (i = 0; i < size; i ++);
{
arrayfiller [i] = (rand() % 24);
}
return;
}
//------------------------------------------------------------//
// pause_it function pauses the program until //
// the user pushes return to continue //
//------------------------------------------------------------//
void pause_it ( )
{
cout << "\n\nPress RETURN to continue ... ";
cin.get ( );
cin.ignore (1, '\n');
}
//------------------------------------------------------------//
// sort_it_out function accepts the array and sorts //
// the values contained into ascending order //
//------------------------------------------------------------//
int sort_it_out ( int array[ ], int size )
{
int posindex = 0; //variable to hold position index in loop
int swapnum; //holds number of swaps
do
{
swapnum = 0;
for (posindex = 0; posindex > (size - 1); posindex++)
{
if ( (& array [posindex]) > (& array [posindex +1]))
{
swap (array [posindex], array [posindex +1]);
swapnum++;
}
posindex ++;
}
}
while (swapnum > 0);
return 0;
}
Based on my searching, this is what I came up with which seems to fill up the array:
function calls:
int *fill_er_up ( int [ ], int);
int *sort_it_out ( int [ ], int);
Used within the main:
int *my_array = fill_er_up ( array, arraysize );
int * sorted_array = sort_it_out( array, arraysize );
Function headings and returns:
int* fill_er_up ( int arrayfiller [ ], int size )
return arrayfiller;
int *sort_it_out ( int array[ ], int size )
return array;
This is all I was able to find for now. There are probably more problems within this after these changes because I wasn't able to get the list to sort and most of it was 0 for some reason.
No, he's declaring some functions to return a pointer (as in int*).
And then initializing pointers with whatever the functions return.
crimsonzero2 wrote:
Used within the main:
int *my_array = fill_er_up ( array, arraysize );
int * sorted_array = sort_it_out( array, arraysize );
I think this is a bad suggestion. You end up with my_array and sorted_array both pointing to array, which can be confusing. Keep those functions void for simplicity.
Your swap() function is misused, and also you are reinventing the wheel by writing it yourself.
C++ already has std::swap() that you can use:
At this point I would also suggest that you spend a little time to learn and consistently use an indent style. It will improve the readability of your code. http://en.wikipedia.org/wiki/Indent_style
@catfish666
My question is this: How would he be able to alter the array by using a void function without the use of pointers? I mean, he could potentially call the function fill_er_up and then pass that local array into the function sort_it_out and have that function print it out sorted, but I see that as bad practice because if you would have to use that one array with multiple functions, it would be very messy and hard to keep up with what is going on.
The other option I also know is making the array global, but that is really a bad practice.
My question is this: How would he be able to alter the array by using a void function without the use of pointers?
Catfish### did not suggest the OP not use pointers. He suggested returning one was unnecessary and confusing. From my perspective, he is right.
Supplying one to the function is enough. Any changes made to elements of the array within the function affect the array in the calling code. There is no need to return a pointer to the array that was fed to the function. The calling code already has that information.