Hi there.


I've written a program to sort a few numbers. However, I get an error missing function header. Can you please guide me where did it go wrong?


Thanks

// 

#include "stdio.h"

int sort(double top, double middle, double bottom);


int main()
{
	double x[20];
	x[1]=0;
	x[2]=-0.07;
	x[3]=0.08;
        x[4]=-0.01;


printf("%.1d,%.1d",x[1],x[2]);

	sort(x[1],x[2],x[3]);
	printf("%.1d,",x[2]);
	sort(x[2],x[3],x[4]);
	printf("%.1d,",x[3]);

	return 0;

}

int sort(double top, double middle, double bottom);
{
		double top, middle, bottom, temp;


		if (top>middle){
			 temp=middle;
		middle=top;
		top=temp;}
		if (middle>bottom){
		temp = middle;
		middle = bottom;
		bottom = temp;}

	return middle;
}

>int sort(double top, double middle, double bottom);
>{
Notice the semicolon before the opening brace in your function definition. You need to remove it.

>double top, middle, bottom, temp;
top, middle, and bottom are function parameters, you shouldn't redefine them as local variables.

Compare:

int sort ( double top, double middle, double bottom )
{
    double temp;

    if ( top > middle ) {
        temp = middle;
        middle = top;
        top = temp;
    }
    if ( middle > bottom ) {
        temp = middle;
        middle = bottom;
        bottom = temp;
    }

    return middle;
}

Hi I've modified my code. But now I get a debug error, which says "stack around the variable x was corrupted"

Where is the mistake?

Thanks

// 

#include "stdio.h"

int sort(double top, double middle, double bottom);


int main()
{
	double x[20];			// Defining 20 random values
	x[1]=0;
	x[2]=-0.07;
	x[3]=0.08;
	x[4]=-0.01;
	x[5]=1.44;
	x[6]=0;
	x[7]=0;
	x[8]=0.07;
	x[9]=-0.01;
	x[10]=0;
	x[11]=0.97;
	x[12]=0.96;
	x[13]=1.02;
	x[14]=0.93;
	x[15]=0.9;
	x[16]=0.24;
	x[17]=1.01;
	x[18]=1.04;
	x[19]=1.01;
	x[20]=1.02;


	printf("%.1d,%.1d",x[1],x[2]);		// Printing the first two values
	
	sort(x[1],x[2],x[3]);			// Sorting out the first 3 values
	printf("%.1d,",x[2]);			// Printing the median of the 3 values
	sort(x[2],x[3],x[4]);
	printf("%.1d,",x[3]);

	return 0;

}

int sort(double top, double middle, double bottom)
{
		double temp;


		if (top>middle){
			 temp=middle;
		middle=top;
		top=temp;}
		if (middle>bottom){
		temp = middle;
		middle = bottom;
		bottom = temp;}

	return middle;
}

Your code is very short, and you won't always have someone around to debug your problems for you. Try to figure it out yourself before asking other people to show you what to fix.

Hi again. I've managed to figure out what went wrong. However I can't seem to run my sort code correctly. I've already checked on the code and can't seem to find any mistakes made. Can you guide me with the problems I faced in my IF statement?

#include <stdio.h>
  

int sort(double top,double middle,double bottom);

   int main()
   {
    double a[4]; 
    a[0]=0;
    a[1]=-0.07;
    a[2]=0.08;
    a[3]=-0.01;

	sort(a[0],a[1],a[2]);			// Sorting out the first 3 values
	printf("%.2f,",a[1]);			// Printing the median of the 3 values
	sort(a[1],a[2],a[3]);
	printf("%.2f,",a[2]);

   return 0;
  }

int sort(double top,double middle,double bottom)
{
		double temp=0;


		if (top>middle)
			 temp=middle;
		middle=top;
		top=temp;


		if (middle>bottom)
		temp = middle;
		middle = bottom;
		bottom = temp;

	return top, middle, bottom;
}

>return top, middle, bottom;
This doesn't return all three values, it evaluates (and ignores) to and middle, and returns only bottom.

What exactly do you want this program to do? If you provide an example of what the input and output should be, I can help you correct any problems.

I want it to sort from smallest to biggest, and returns only the median value. It will sort 3 values at a time. Example of sorting is


-0.07,0,0.08 and printf only 0


for the second sort, -0.07,-0.01,0.08 and printf only -0.01

Well, it doesn't actually sort currently. The array doesn't change. Compare and contrast:

#include <stdio.h>

double sort(double a[3]);

int main()
{
    double a[3];
    double median;

    a[0] = 0;
    a[1] = 0.08;
    a[2] = -0.07;
    median = sort(a);
    printf("%.2f, %.2f, %.2f, median: %.2f\n", a[0], a[1], a[2], median);

    a[0] = -0.01;
    a[1] = 0.08;
    a[2] = -0.07;
    median = sort(a);
    printf("%.2f, %.2f, %.2f, median: %.2f\n", a[0], a[1], a[2], median);

    return 0;
}

double sort(double a[3])
{
    double temp;

    if ( a[1] < a[0] ) {
        temp = a[1];
        a[1] = a[0];
        a[0] = temp;
    }

    if ( a[2] < a[1] ) {
        temp = a[2];
        a[2] = a[1];
        a[1] = temp;
    }

    if ( a[1] < a[0] ) {
        temp = a[1];
        a[1] = a[0];
        a[0] = temp;
    }

    return a[1];
}

Comparing floating point is tricky business due to natural inaccuracies in the binary representation. I'm inclined to say that you should always compare with a fudge factor, but that's a topic for another time. Just keep it on the back burner that floating point comparisons aren't as simple as integral comparisons.

Thank you so much. I know how to use the return function now.


I appreciate your help greatly. Thanks once again

This article has been dead for over six months. Start a new discussion instead.