Zelaron Gaming Forum  
Stats Arcade Portal Forum FAQ Community Calendar Today's Posts Search
Go Back   Zelaron Gaming Forum > The Zelaron Nexus > Science and Art > Tech Help

 
 
Thread Tools Display Modes

 
Rand() function seeded srand(time(0)
Reply
Posted 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:
Code:
#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);
}

Last edited by WetWired; 2004-11-08 at 07:01 AM. Reason: Please use the [code] tag
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75
 



 
Reply
Posted 2004-11-07, 03:06 PM in reply to deadlock75's post "Rand() function seeded srand(time(0)"
great... can someone translate ??? babelfish doesnt support it.
Old
Profile PM WWW Search
Dr. Geekstar is neither ape nor machine; has so far settled for the in-betweenDr. Geekstar is neither ape nor machine; has so far settled for the in-between
 
 
Dr. Geekstar
 



 
Reply
Posted 2004-11-07, 03:18 PM in reply to Dr. Geekstar's post starting "great... can someone translate ???..."
Dr. Geekstar said:
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.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes
 



 
Reply
Posted 2004-11-07, 04:16 PM in reply to deadlock75's post "Rand() function seeded srand(time(0)"
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.
"Stephen Wolfram is the creator of Mathematica and is widely regarded as the most important innovator in scientific and technical computing today." - Stephen Wolfram

Last edited by Chruser; 2004-11-07 at 04:21 PM.
Old
Profile PM WWW Search
Chruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed itChruser shouldn't have fed it
 
 
Chruser
 



 
Reply
Posted 2004-11-07, 08:38 PM in reply to Chruser's post starting "Sorry, I'm not really sure what you..."
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.
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75
 



 
Reply
Posted 2004-11-08, 12:29 PM in reply to deadlock75's post starting "I am to post to use rand() (seeded with..."
Can anyone help me I still don't get it?
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75
 



 
Translation
Reply
Posted 2004-11-08, 12:35 PM in reply to Demosthenes's post starting "This isn't the chat forum. Deadlock,..."
mjordan2nd said:
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.
Old
Profile PM WWW Search
deadlock75 is neither ape nor machine; has so far settled for the in-betweendeadlock75 is neither ape nor machine; has so far settled for the in-between
 
deadlock75
 
 

Bookmarks

« Previous Thread | Next Thread »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules [Forum Rules]
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -6. The time now is 09:07 PM.
'Synthesis 2' vBulletin 3.x styles and 'x79' derivative
by WetWired the Unbound and Chruser
Copyright ©2002-2008 zelaron.com
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
This site is best seen with your eyes open.