bubblesort problem

Hi forum I have problem with bubble sort Im generate random float numbers to array but after sorting of bubble sort most numbers change fractional part
but i need same numbers like after sorting.Anyone who can help me please ?

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;




// global variable and constants
const int n = 50;
const int min = 0;
float *pole = new float[n];



//prototyps
void  generate_number(float min, float max);
void render_array(float pole[]);
void bubbleSort(float * array, int size);




int main(void)
{
	generate_number(min, n);
	bubbleSort(pole, n);
	render_array(pole);



	cin.get();
	return 0;
}




//function

void generate_number(float min, float max)
{
	for (int i = 0; i < n; i++)
	{
		pole[i] = (max - min) * ((((float)rand()) / (float)RAND_MAX)) + min;

	}
}


void render_array(float pole[])
{
	for (int i = 0; i < n; i++)
		printf("%d :  %.3f\n", i, pole[i]);
}



void bubbleSort(float * array, int size){
	for (int i = 0; i < size - 1; i++){
		for (int j = 0; j < size - i - 1; j++){
			if (array[j + 1] < array[j]){
				int tmp = array[j + 1];
				array[j + 1] = array[j];
				array[j] = tmp;
			}
		}
	}
}




//end of function

closed account (48T7M4Gy)
int tmp = array[j + 1];

That's where you're making a blooper.
Hint: C++ is a strongly typed language. ( Also float is more or less defunct except for legacy code I'm told, but that in itself isn't the immediate problem. )
Last edited on
Thank you.
Topic archived. No new replies allowed.