Log in

View Full Version : Rand() function seeded srand(time(0)


deadlock75
2004-11-07, 01:14 PM
I know how to make a bubble sort and the exchange sort but I do not know how to generate a small arrays of dimension 20, such that the entries are random positive integers less than 100. I am also not sure if I can combine the bubble, exchange and sort the random array of integers. Heres what I have so far:

Bubble Sort:

#include <stdio.h>

main()
{
int numbers[10], i, j, k, tmp;
printf("\nthe original numbers are:\n");
for (i=0; i<10; i=i+1)
{
numbers[i]=rand();
printf(" %d ", numbers[i]);
}
printf("\n\n");
for (i=0; i<10; i=i+1)
{
for (j=0; j<9; j=j+1)
{
if (numbers[j]<numbers[j+1])
{
tmp=numbers[j];
numbers[j]=numbers[j+1]
numbers[j+1]=tmp;
}
}
}
printf("the sorted numbers are:\n")
for (i=0; i<10; i=i+1)
{
printf(" %d ", numbers[i]);
}
printf("\n\n");
}

Exchange Sort:
#include <stdio.h>

main()
{
int numbers[10], i, j, k, tmp;
printf("\nthe original numbers are:\n");
for (i=0; i<10; i=i+1)
{
numbers[i]=rand();
printf(" %d ", numbers[i]);
}
printf("\n\n");
for (i=0; i<9; i=i+1)
{
for (j=0; j<10; j=j+1)
{
if (numbers[i]<numbers[j])
{
tmp=numbers[j];
numbers[j]=numbers[i]
numbers[i]=tmp;
}
}
}
printf("the sorted numbers are:\n")
for (i=0; i<10; i=i+1)
printf(" %d ", numbers[i]);
printf("\n\n");
return(0);
}

Dr. Geekstar
2004-11-07, 03:06 PM
great... can someone translate ??? babelfish doesnt support it.

Demosthenes
2004-11-07, 03:18 PM
great... can someone translate ??? babelfish doesnt support it.

This isn't the chat forum.

Deadlock, I'm not sure what you're askinng, but if you want it less than 100, just use an if statement to limit it, and save that value to an array.

Chruser
2004-11-07, 04:16 PM
Sorry, I'm not really sure what you asked either. I looked at your code, and the only thing I could comprehend from your question that makes sense would be that you want to fill up an array of, say, 20 slots with random integers frm 1-100, in order to sort them afterwards or whatnot.

Well, I see numbers[i]=rand();

rand()%100 will give a random integer from 0 to 99, so use this line to get 1-100 inputted into a slot of your array:

numbers[i]=rand()%100+1;

Not sure if that's what you meant. It's been too long since I dealt with this. I recall writing a new randomizer function way back that took a min and max parameter, kind of like the PHP function. Not that it really matters, it just takes some basic math skills to figure out your range.

Now that I see your other post about QuickSort, my answer feels too basic.

deadlock75
2004-11-07, 08:38 PM
I am to post to use rand() (seeded with either srand(time(0)) or with randomize(), to generate a small arrays of dimsion 20, such that the entries are random positive integers less than 100. Then I have make sure to implement the bubble sort and the exchange sort and sort the random array of integers.

deadlock75
2004-11-08, 12:29 PM
Can anyone help me I still don't get it?

deadlock75
2004-11-08, 12:35 PM
This isn't the chat forum.

Deadlock, I'm not sure what you're askinng, but if you want it less than 100, just use an if statement to limit it, and save that value to an array.

I am to post to use rand() (seeded with either srand(time(0)) or with randomize(), to generate a small arrays of dimsion 20, such that the entries are random positive integers less than 100. Then I have make sure to implement the bubble sort and the exchange sort and sort the random array of integers.