J
Jack
I have two files:
sort_comparison.c++
my_sort.h
sort_comparison.c++ calls this code in my_sort.h:
void my_sort::fillArray(int arr[],int n)
{
// const int random_number_range=1000000;
const int random_number_range=100;
int number,xcell;
randomNumber rnd;
for (xcell=0;xcell<n;xcell++)
{
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
// arr[xcell]=rand();
}
}
The problem is that every array gets filled with the exact same data...
If I comment out:
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
and replace it with
arr[xcell]=rand();
I get random data in my arrays... The complete code for both files is
below. Can anyone tell me why
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
doesn't generate randomized data like "arr[xcell]=rand();"? Thanks.
////////////////////////////////////////////////////////////////////
//begin sort_comparison.c++
#include <ctime>
#include <iostream>
#include <string>
#include "../ftsoftds/include/d_random.h"
#include "../ftsoftds/include/d_sort.h" // Contains
selectionSort
//#include "../ftsoftds/include/d_timer.h"
#include "my_sort.h" // My sorting library.
using namespace std;
void printArray(string name,int arr[],int n)
{
int cell;
cout<<name<<" Sort Array..."<<endl;
for(cell=0;cell<n;cell++)
{
printf("%d ",arr[cell]);
}
cout<<endl;
}
////////////////////////////////////////////////////////////////////////////////
int main ()
{
// Declare five (5) integer arrays. Final array_size is 10,000 (ten
thousand).
const int array_size=25;
const int xrandom_number_range=100;
int
array_selection_sort[array_size],array_insertion_sort[array_size],
array_bubble_sort[array_size],array_exchange_sort[array_size],
array_shell_sort[array_size];
// Declare my_sort.h object (Did I reference this comment
correctly?).
my_sort my;
int cell,xnumber;
randomNumber xrnd;
/*
for(cell=0;cell<array_size;cell++)
{
xnumber=xrnd.random(xrandom_number_range);
array_selection_sort[cell]=xnumber;
}
for(cell=0;cell<array_size;cell++)
{
printf("%d ",array_selection_sort[cell]);
}
cout<<endl;
for(cell=0;cell<array_size;cell++)
{
xnumber=xrnd.random(xrandom_number_range);
array_insertion_sort[cell]=xnumber;
}
for(cell=0;cell<array_size;cell++)
{
printf("%d ",array_insertion_sort[cell]);
}
cout<<endl;
for(cell=0;cell<array_size;cell++)
{
array_exchange_sort[cell]=4;
}
for(cell=0;cell<array_size;cell++)
{
printf("%d ",array_exchange_sort[cell]);
}
cout<<endl;
*/
my.fillArray(array_selection_sort,array_size);
my.fillArray(array_insertion_sort,array_size);
my.fillArray(array_bubble_sort,array_size);
my.fillArray(array_exchange_sort,array_size);
my.fillArray(array_shell_sort,array_size);
// array_exchange_sort[0]=99;
selectionSort(array_selection_sort,array_size);
insertionSort(array_insertion_sort,array_size);
// The following is used to verify that the arrays contain like data...
printArray("Selection",array_selection_sort,array_size);
printArray("Insertion",array_insertion_sort,array_size);
printArray("Bubble",array_bubble_sort,array_size);
printArray("Exchange",array_exchange_sort,array_size);
printArray("Shell",array_shell_sort,array_size);
return 0;
}
//end sort_comparison.c++
///////////////////////////////////////////////////////////////////////
//begin my_sort.h
class my_sort
{
int x, y;
public:
void bubbleSort(int arr[],int n);
void fillArray(int arr[],int n);
};
void my_sort::bubbleSort(int arr[],int n)
{
int i,j;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++)
if (arr > arr[j]) { // swap arr and arr[j]
int tmp = arr;
arr = arr[j];
arr[j] = tmp;
}
}
////////////////////////////////////////////////////////////////////////////////
void my_sort::fillArray(int arr[],int n)
{
// const int random_number_range=1000000;
const int random_number_range=100;
int number,xcell;
randomNumber rnd;
for (xcell=0;xcell<n;xcell++)
{
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
// arr[xcell]=rand();
}
}
//end my_sort.h
sort_comparison.c++
my_sort.h
sort_comparison.c++ calls this code in my_sort.h:
void my_sort::fillArray(int arr[],int n)
{
// const int random_number_range=1000000;
const int random_number_range=100;
int number,xcell;
randomNumber rnd;
for (xcell=0;xcell<n;xcell++)
{
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
// arr[xcell]=rand();
}
}
The problem is that every array gets filled with the exact same data...
If I comment out:
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
and replace it with
arr[xcell]=rand();
I get random data in my arrays... The complete code for both files is
below. Can anyone tell me why
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
doesn't generate randomized data like "arr[xcell]=rand();"? Thanks.
////////////////////////////////////////////////////////////////////
//begin sort_comparison.c++
#include <ctime>
#include <iostream>
#include <string>
#include "../ftsoftds/include/d_random.h"
#include "../ftsoftds/include/d_sort.h" // Contains
selectionSort
//#include "../ftsoftds/include/d_timer.h"
#include "my_sort.h" // My sorting library.
using namespace std;
void printArray(string name,int arr[],int n)
{
int cell;
cout<<name<<" Sort Array..."<<endl;
for(cell=0;cell<n;cell++)
{
printf("%d ",arr[cell]);
}
cout<<endl;
}
////////////////////////////////////////////////////////////////////////////////
int main ()
{
// Declare five (5) integer arrays. Final array_size is 10,000 (ten
thousand).
const int array_size=25;
const int xrandom_number_range=100;
int
array_selection_sort[array_size],array_insertion_sort[array_size],
array_bubble_sort[array_size],array_exchange_sort[array_size],
array_shell_sort[array_size];
// Declare my_sort.h object (Did I reference this comment
correctly?).
my_sort my;
int cell,xnumber;
randomNumber xrnd;
/*
for(cell=0;cell<array_size;cell++)
{
xnumber=xrnd.random(xrandom_number_range);
array_selection_sort[cell]=xnumber;
}
for(cell=0;cell<array_size;cell++)
{
printf("%d ",array_selection_sort[cell]);
}
cout<<endl;
for(cell=0;cell<array_size;cell++)
{
xnumber=xrnd.random(xrandom_number_range);
array_insertion_sort[cell]=xnumber;
}
for(cell=0;cell<array_size;cell++)
{
printf("%d ",array_insertion_sort[cell]);
}
cout<<endl;
for(cell=0;cell<array_size;cell++)
{
array_exchange_sort[cell]=4;
}
for(cell=0;cell<array_size;cell++)
{
printf("%d ",array_exchange_sort[cell]);
}
cout<<endl;
*/
my.fillArray(array_selection_sort,array_size);
my.fillArray(array_insertion_sort,array_size);
my.fillArray(array_bubble_sort,array_size);
my.fillArray(array_exchange_sort,array_size);
my.fillArray(array_shell_sort,array_size);
// array_exchange_sort[0]=99;
selectionSort(array_selection_sort,array_size);
insertionSort(array_insertion_sort,array_size);
// The following is used to verify that the arrays contain like data...
printArray("Selection",array_selection_sort,array_size);
printArray("Insertion",array_insertion_sort,array_size);
printArray("Bubble",array_bubble_sort,array_size);
printArray("Exchange",array_exchange_sort,array_size);
printArray("Shell",array_shell_sort,array_size);
return 0;
}
//end sort_comparison.c++
///////////////////////////////////////////////////////////////////////
//begin my_sort.h
class my_sort
{
int x, y;
public:
void bubbleSort(int arr[],int n);
void fillArray(int arr[],int n);
};
void my_sort::bubbleSort(int arr[],int n)
{
int i,j;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++)
if (arr > arr[j]) { // swap arr and arr[j]
int tmp = arr;
arr = arr[j];
arr[j] = tmp;
}
}
////////////////////////////////////////////////////////////////////////////////
void my_sort::fillArray(int arr[],int n)
{
// const int random_number_range=1000000;
const int random_number_range=100;
int number,xcell;
randomNumber rnd;
for (xcell=0;xcell<n;xcell++)
{
number=rnd.random(random_number_range);
arr[xcell]=rnd.random(number);
// arr[xcell]=rand();
}
}
//end my_sort.h