0

Hi,

I have created a bubble sort as a function in C. It sorts an array of numbers. I am not sure how to print them out though(SORTED).

Thanks

3
Contributors
13
Replies
15
Views
6 Years
Discussion Span
Last Post by Adak
0

Ok. Below is what i a have so far. It is only a bubble sort which inst great but this is just to start.

#include <stdio.h>

void bubble(float* num, int length);

float num[] = {3.6, 6.3, 8.7, 1.2, 4.345566, 0.56,0.321}; 

int main()
{

	return 0;

}

void bubble(float* num, int length)
{
	int sortcounter, check;
	float temp;

	check = 1; 
	while(check) 
	{
		check = 0; 
		for(sortcounter = 0; sortcounter < length-1; sortcounter++) 
		{
			if(num[sortcounter] > num[sortcounter+1]) 
			{
				check = 1; 
				temp = num[sortcounter];
				num[sortcounter] = num[sortcounter+1];
				num[sortcounter+1] = temp;
			} 
		} 

	} 

}
0

To print the first element use code like below.

int i = 0;
fprintf(stdout, "%f\n", num[i]);

Edited by gerard4143: n/a

0

Thank You :). How can i print the whole list out in the Main section?

0

Cheers. I take that code with the for loop goes in the main part of the program and not the function?

0

Also I have just ran that in the main section and it just prints out the numbers in the same order as in the array e.g. not sorted.

0

In main() you MUST call the bubble() function, if you want the program to use that function and sort.

0

Do I call the function using this

bubble();

That is what i thought it was but it didn't work.

0

Also tried void bubble(float* num, int length); in the main. Not sure how to call the function.

0

num[] is a global array, so bubble function can access it already. Don't pass num[] to the bubble function.

BUT length is not a global, and you need to declare it as an int, in main() so it can be passed to bubble function. Give it the number of floats in num[], or it's useless.

So your bubble prototype is:
void bubble(int);

Your call to bubble is:
bubble(length);

and your bubble first line declaration is the same as your prototype, but ends with a curly brace, instead of a semi-colon, and includes the name of the int, length:
void bubble(int length) { //NO semi-colon here!


Tom, you need to study up. These are VERY basic concepts here. Work at it! ;)

0

Hi, Thanks for that explanation. You have helped loads. I have found a book to help learn the concept of functions. The code i have know is below i think it is correct but when i compile it says i is unidentified, but i stated int i;

Not sure why it comes up with this?

#include <stdio.h>

void bubble(int);

float num[] = {3.6, 6.3, 8.7, 1.2, 4.345566, 0.56,0.321}; 

int main()
{

	int length = 7;
	bubble(length);
	int i;

	for(i = 0; i < 7; i++)
	{
		fprintf(stdout, "%f\n", num[i]);
	}

	return 0;

}

void bubble(int length)
{
	int sortcounter, check;
	float temp;

	check = 1; 
	while(check) 
	{
		check = 0; 
		for(sortcounter = 0; sortcounter < length-1; sortcounter++) 
		{
			if(num[sortcounter] > num[sortcounter+1]) 
			{
				check = 1; 
				temp = num[sortcounter];
				num[sortcounter] = num[sortcounter+1];
				num[sortcounter+1] = temp;
			} 
		} 

	} 

}
0

Some (newer standard) C compilers will allow variables to be declared throughout a function.

But the old standard wouldn't allow it. Move int i, up to the rest of the variables you have declared for main(), and re-try.

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.