Problem compiling

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#define BUBBLE 1000
using namespace std;
int main()
{
int myArray[1000];
int i, j;
int temp = 0;
int num;

srand(time(NULL));

//fill array
for (i = 0; i < 1000; i ++)
{
myArray[i]=(rand()%250)+1;
cout<<myArray[i]<<" ";
}
cout<<endl<<endl;

for(i=0;i<999;i++){
if(myArray[i]<myArray[i+1]){
temp=myArray[i];
myArray[i]=myArray[i+1];
myArray[i+1]=temp;// switch
}
}
for(i=0;i<1000;i++){cout<<myArray[i]<<" ";
if((i+1)%1000==0) cout<<endl;
}


return 0;


trying to sort 1000 numbers between 0 and 250 into ascending order, codeblocks is telling me i,j are unused variables. if somebody could fix this for me id appreciate it :) thanks
Variable i isn't unused. But j is! Remove its definition to fix it.
Ok thank you! , now it actually runs but the bubble sorting isn't sorting it
Ok, that's another problem.... Give some more infos about what your tests and what your program should do and what it does instead.

(And please, format your code in some better readable way and use this little "<>"- button at the right side of the message composing window.)
it should sort an array of 1000 random numbers between 0 and 250 into ascending order, and the output should be that vector. Sorry for the late reply

[code]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#define BUBBLE 1000
using namespace std;
int main()
{
int myArray[1000];
int i;
int temp = 0;
int num;

srand(time(NULL));

//fill array
for (i = 0; i < 1000; i ++)
{
myArray[i]=(rand()%250)+1;
cout<<myArray[i]<<" ";
}
cout<<endl<<endl;

for(i=0;i<999;i++){
if(myArray[i]<myArray[i+1]){
temp=myArray[i];
myArray[i]=myArray[i+1];
myArray[i+1]=temp;// switch
}
}
for(i=0;i<1000;i++){cout<<myArray[i]<<" ";
if((i+1)%1000==0) cout<<endl;
}


return 0;
}[/code]
Last edited on
1. If myArray[i] is less than myArray[i+1] then they are in the expected order. So no swapping is needed in this case.

2. Try a look at Wikipedia for Bubblesort.

3. Sorry, but as you should see yourself: Your code stays unreadable for us. Format it using the "<>"-button:

3.1 Indent your code lines
3.2 Mark all code lines
3.3 Press "<>"-button
3.4 Press "Preview"-button and have a look at the resulting text
3.5 Repeat 3.1 .. 3.4 until code is readable for everyone.

4. It's not a bad idea to add some comments.

(Sorry, but I'm a little bit tired, ...)
Another tipp: Get some small pieces of paper. Write any number on each one. Distribute them randomly in a row. Now try to sort them using the BubbleSort algorithm. This may really help you to understand the procedure much better!
that's a very good idea! I'm pretty new to this, trying to get my head around it. thanks for the tips
Topic archived. No new replies allowed.