Can´t print bubblesort values

It refuses to give an ouput, can anyone help?

// Bubblesort.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include "stdafx.h"

using namespace std;

// Function BubbleSort
void BubbleSort(int age[10])

{
age[0] = 22;
age[1] = 21;
age[2] = 23;
age[3] = 65;
age[4] = 76;
age[5] = 54;
age[6] = 25;
age[7] = 85;
age[8] = 48;
age[9] = 52;
int max = 9;
// The outer loop, going through all list
for(int i = 0; i < max; i++)
{
//Inner loop, going through element by element
int nrLeft = max - i; // To se how many has been gone through
for (int j = 0; j < nrLeft; j++)
{
if (age[j] > age[j+1]) // Compare the elements
{
// Change place
int temp = age[j];
age[j] = age[j+1];
age[j+1] = temp;
}
}
}

// Write the list
for (int i = 0; i < 9; i++)
{
cout << age << "\n";
}
};

int main()
{
void BubbleSort(int age[]);

return 0;
};
Last edited on
 
void BubbleSort(int age[]);

is a function declaration, it doesn't do anything. I would suggest reading on functions and how to pass arrays into functions.

________________________

There's other problems too. If we change your main function so that it becomes a call to BubbleSort, it has no idea what "age" is. It looks like you're "hardcoding" the values inside the function itself, so you should define the array inside the function.
ex:
1
2
3
4
5
void BubbleSort()
{
    int age[10] = {22, 23, 65, 76, 54, 25, 85, 48, 52};
    //...the rest of your code
}

and call it like this
1
2
3
4
5
int main()
{
    BubbleSort();
    return 0;
}

again, if you want to pass an already-defined function into your bubble sort, I would read up on how to pass an array into a function.

Another problem:
1
2
3
4
    for (int i = 0; i < 9; i++)
    {
        cout << age << "\n";
    }

You're just printing out the address of age each time. You should print out age[i]. You also seem to be confused on whether you're dealing with 10 elements or 9, I would suggest making a more generalized function that can take in any array with a size argument as well.

Also please use code tags, the lack of indentation and spacing makes it harder to read.

If you change your function declaration into a function call, fix the array being passed, and print age[i] instead of age, it should work and the sort logic is correct as well.
Last edited on
Line 47: This is a function prototype, not a function call.

Should be:
 
    BubbleSort(age);


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Fixed it. Thanks guys!!!

Working code is:

// Bubblesort.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include "stdafx.h"

using namespace std;

// Function BubbleSort
void BubbleSort(int age[])

{

int max = 9;
// The outer loop, going through all list
for(int i = 0; i < max; i++)
{
//Inner loop, going through element by element
int nrLeft = max - i; // To se how many has been gone through
for (int j = 0; j < nrLeft; j++)
{
if (age[j] > age[j+1]) // Compare the elements
{
// Change place
int temp = age[j];
age[j] = age[j+1];
age[j+1] = temp;
}
}
}

// Write the list
for (int i = 0; i < 9; i++)
{
cout << age[i] << "\n";
}
};

int main()
{
//Create a list with persons and fill it:

int age[10];
age[0] = 22;
age[1] = 21;
age[2] = 23;
age[3] = 65;
age[4] = 76;
age[5] = 54;
age[6] = 25;
age[7] = 85;
age[8] = 48;
age[9] = 52;


BubbleSort(age);

return 0;
};

You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.
Topic archived. No new replies allowed.