Help with assigning random int's to array

Write your question here.

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
  ////////////////////////////////////////////////////////////////
//                                                            //
//      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

using namespace std;

void swap ( int * x, int * y);
int rand (void);
void srand (unsigned int 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 ( (unsigned int) 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;
} 
Last edited on
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.
@ crimsonzero2

So are you dereferencing with the use of "*"

niyona wrote:
So are you dereferencing with the use of "*"

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:

http://www.cplusplus.com/reference/utility/swap/

Otherwise, if you want to use your custom swap() function, your code must be corrected:

135
136
137
138
139
140
     if ( array [posindex] > array [posindex +1])
		{ 
		swap (&array [posindex], &array [posindex +1]);
	swapnum++;
		}
    posindex ++;


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

Last edited on
In addition, you have no business giving your own declarations for std::rand() and std::srand():

20
21
// int rand (void); // don't do this
// void srand (unsigned int seed); 


And also arraysize should be constant:

const int arraysize = 25; //a variable for the size of array

And there's a mistake in your fill_er_up() function which prevents the array from being properly assigned random values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void fill_er_up ( int arrayfiller[], int size )

{

int i;      	 // counter variable

srand ( (unsigned int) time (NULL) );

for (i = 0; i < size; i ++); // notice something wrong?
   {
	arrayfiller [i] = (rand() % 24);
}

//              return; // not needed
}


Edit: also found a mistake in your sort_it_out() function:

for (posindex = 0; posindex > (size - 1); posindex++)

Edit: your Bubble Sort is incorrect. You increment posindex a second time inside the loop.

Last edited on
@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.
Last edited on
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.
Topic archived. No new replies allowed.