Bubble sort error?

I seem to get an error after int main (void) saying 'a function definition isnt aloowed here before { token? And then also at the end of main saying 'expexted } at end of output?

My programme is trying to create a random array of 100 ints between 0 and 250, sort them and print them.

Here is my code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>

void bubbleSort(int *array,int length)//Bubble sort function
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(array[i]>array[j])
{
int temp=array[i]; //swap
array[i]=array[j];
array[j]=temp;
}

}

}

void printElements(int *array,int length) //print array elements
{
int i=0;
for(i=0;i<10;i++)
cout<<array[i]<<endl;
}

int main(void)

{
int select[100];
srand(time(0));
for(int i=0; i<250; i++) select[i]=rand()%251;
bubbleSort (select,100);
printElements(select,100);
}

Thank you in advance!!
1) Please use code tags, to make your code more readable.

2) You haven't closed all the braces that you opened in your bubbleSort method. You'd find this sort of thing easier to spot if you adopted a consistent indentation policy.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>

void bubbleSort(int *array,int length)//Bubble sort function
{
int i,j;
for(i=0;i<100;i++)
{
for(j=0;j<i;j++)
{
if(array[i]>array[j])
{
int temp=array[i]; //swap
array[i]=array[j];
array[j]=temp;

printElements(array, length);
}

}

}
}

void printElements(int *array,int length) //print array elements
{
int i=0;
for(i=0;i<10;i++)
cout<<array[i]<<", ";
cout << endl;
}

int main(void)

{
int select[100];
srand(time(0));
for(int i=0; i<250; i++) select[i]=rand()%251;
bubbleSort (select,100);
printElements(select,100);
}


Can you explain why this wont work, Ive corrected brackets etc
Last edited on
You need to define the printElements function before the bubbleSort one
Also, in the main function, the loop over i should be from 0 to <100, since select has 100 elements.
Other comments:
- don't use stdio.h, so don't include it
- in modern compilers <stdlib.h> and <time.h> should be replaced by <cstdlib> and <ctime>
I debugged your code and took the freedom to fix the indentation etc. Always try to write readable code .

As ats15 commented + you missed the std namespace. an extra print funk bubble sort, forgot to use your parameters. tried to write 250 ints to a aray of 100 elements. you also turned to > the wrong way in bubble sort. :)


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

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstring>

using namespace std;

void printElements(int* array, int length) //print array elements
{
  int i = 0;

  for(i = 0; i < length; ++i)
    cout << array[i] << ", ";

  cout << endl;
}

void bubbleSort(int* array, int length) //Bubble sort function
{
  int i, j, temp;

  for(i = 0; i < length; ++i)
  {
    for(j = 0; j < i; ++j)
    {
      if(array[i] < array[j])
      {
        temp = array[i]; //swap
        array[i] = array[j];
        array[j] = temp;
      }
    }
  }
}


int main(void)

{
  int select[100];
  
  srand(time(0));
  
  for(int i = 0; i < 100; ++i)
  {
    select[i]=rand() % 251;
  }
  
  bubbleSort (select, 100);

  printElements(select, 100);
}

Topic archived. No new replies allowed.