I've been given a practical where I have develop a random bubble sort algorithm and I've created one already where the user enters the elements needed to arrange the numbers, I now have to use a random number generator that uses a set of 10 random numbers from 1-10000 and then bubble sort that could anyone please help me to how I can acheive this feat.

``````#include<stdio.h>
void bubble_sort(int [],int);
int main()
{
int a[30],n,i;
printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
}

void bubble_sort(int a[],int n)

{
int i,j,k,temp;
printf("\nUnsorted Data:");
for(k=0;k<n;k++)
printf("%5d",a[k]);
for(i=1;i<n;i++)

{
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])

{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nAfter pass : %d",i);
for(k=0;k<n;k++)
printf("%5d",a[k]);
}
}``````
3
Contributors
11
Replies
12
Views
8 Years
Discussion Span
Last Post by WaltP
Featured Replies
• 1
WaltP 2,905   8 Years Ago

Excellent. You now know you have to read [I]everything[/I] (including your book) carefully.

Change the input statement into calls to `rand()` . Be sure you look up how to use it correctly.

I've done the code to random generate the numbers how do I apply that now into the bubble sort algorithm I made above?

``````#include <stdio.h>
#include  <stdlib.h>
#include <time.h>
int main()
{ const int n=10;
const int m=10000;
int i;
srand(time(NULL));
for(i=0; i<n; i++)
printf( "Random number %d is %d\n", i+1, rand()%m );
return EXIT_SUCCESS;
}``````

Instead of using a printf() to display the random numbers, store them into an array of int.

I'm unsure how to use the rand into a scanf either I think its like this

``````int main()
{
int a[30],n,i;
printf("\nEnter no of elements :");
scanf("rand()%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
scanf("rand()%d",&a[i]);
bubble_sort(a,n);
}``````

or

``````int main()
{
int a[30],n,i;
printf("\nEnter no of elements :");
scanf("rand%d",&n);
printf("\nEnter array elements :");
for(i=0;i<n;i++)
scanf("rand%d",&a[i]);
bubble_sort(a,n);
}``````

I've wanted only 10 elements but the numbers can be used between 1-10000 so far I have this

``````#include <stdio.h>
#include  <stdlib.h>
#include <time.h>
void bubble_sort(int [],int);

int main()
{
int a[0];
int n=10;
int i;
const int m=10000;
srand(time(NULL));
for(i=0;i<n;i++)
scanf("rand%m",&a[i]);
bubble_sort(a,n);
}

void bubble_sort(int a[],int n)

{
int i,j,k,temp;
printf("\nUnsorted Data:");
for(k=0;k<n;k++)
printf("%5d",a[k]);
for(i=1;i<n;i++)

{
for(j=0;j<n-1;j++)
if(a[j]>a[j+1])

{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("\nAfter pass : %d",i);
for(k=0;k<n;k++)
printf("%5d",a[k]);
}
}``````

now the code compiles the only things wrong is that first it won't run automatically till I've pressed enter or any other key and secondly it randomly generates numbers that are way to big (they go over 10000) I need to know what's wrong

Change the input statement into calls to `rand()` .

Did you actually change your input statements in to calls to `rand()` ?

Be sure you look up how to use it correctly.

Did you actually look up how to use `rand()` ?

It looks like you completely ignored both my suggestions and did your own thing.

Edited by WaltP: n/a

I've looked up rand and found how to print random numbers but not how to put it into a scanf or input statements believe me I'm taken your advice to heart and I understand what your trying to make me do, I've posted another full code of changes ive made to adjust to my practical and I know i'm close just a few errors as I've mentioned. If you have any websites on rand() that can help let me know thank you for your help so far.

No you haven't.
An input statement is `scanf()` . It puts a value into a variable entered by the user.
I said "Change the input statement into calls to `rand()` ", so if `scanf()` is an input statement, and you continue using it, you are using an input statement.

Use `rand()` to put a value into the variable. And I'm certain your source never showed you how to use it in a `scanf()` statement and not only in a `printf()` statement.

Search this forum if your source is still confusing you.

I would like to apoligize mate I totally misunderstood you and sorry but the good news is I've got it working so yeh!

Excellent. You now know you have to read everything (including your book) carefully.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.