why don't i get a different number?
May 29, 2010 at 7:47am UTC
Using a rolltest program trying to get 6 different numbers. Activates both functions but gives me the same result 6 times. Where am I 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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rollnum();
void sortArray(int *, int );
int main()
{
int stats[6];
for (int count = 0; count < 6; count++)
{
stats[count] = 0;
stats[count] = rollnum();
}
cout << endl << endl;
for (int count = 0; count < 6; count++)
{
cout << stats[count] << endl;
}
system("PAUSE" );
return 0;
}//ends main
int rollnum()
{
int *numbers;
numbers = new int [4];
unsigned seed = time(0);
srand(seed);
for (int count = 0; count < 4; count++)
{
numbers[count] = 0;
numbers[count] = 1 + rand() % 6;
}
sortArray(numbers, 4);
//show the numbers
for (int count = 0; count < 4; count ++)
{
cout << numbers[count] << " " ;
}
//show the total
static int total = numbers[1] + numbers[2] + numbers[3];
cout << "Total stat num = " << total << endl;
delete [] numbers;
numbers = 0;
return total;
}//ends rollnum
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false ;
for (int count = 0; count < (size - 1); count ++)
{
if (*(array + count) > *(array + (count + 1)))
{
temp = *(array+count);
*(array + count) = *(array + (count + 1));
*(array + (count + 1)) = temp;
swap = true ;
}//ends if
}//ends for 1
} while (swap);//ends do
}
Ignore the pointers and etc if need. It was something I was messing around with and trying out. I have the other version not using pointers on a computer at school. But I was learning pointers in class and needed to use them somehow. Least this worked...slightly.
May 29, 2010 at 8:22am UTC
Seed the random number generator only ONCE in your program.
May 29, 2010 at 9:52am UTC
Freakin A. Thanks man.
Topic archived. No new replies allowed.