Reputation: 13
I'm practicing my C skills by trying to code a program for rearranging array elements in ascending order by using a created function called Bubbleswap. TO fill up the array I've used the random number generator.
When I use the following code, I will get the message that Bubblesort function is missing a return value, while it shouldn't need one. And I think therefore the request to use bubbleswap function is not working properly.
//Opgave bubblesort functie
#include "toolbox.h"
#include <ansi_c.h>
#include <stdio.h>
#include <time.h>
// functies //
double bubblesort (double arrayA[], int n)
{
int a,b,swap;
for (a =0;a<(n - 1);a++)
{
for (b=0;b<(n - a - 1);b++)
{
if(arrayA[b]>arrayA[b+1]) // for decreasing order use <
{
swap = arrayA[b];
arrayA[b]= arrayA[b+1];
arrayA[b+1]=swap;
}
}
}
}
// main script //
int main()
{
int aantal,i; //variables
double arrayA[1000],arrayB[1000] ;
float r;
srand(time(NULL)); // to start the random seeds
printf(" Typ het aantal getallen in. \n"); //request the elements
scanf("%d", &aantal);
for(i=0; i<aantal;i++) // fill the array with random numbers
{
arrayA[i]=rand();
}
printf("Getallen in volgorde \n"); //re-arrange the numbers with the help of bubblesort
for (i=0; i<aantal;i++)
{
r = bubblesort(arrayA, aantal); //request the function bubblesort //r =arrayA[i];
printf("%.0f \n", r);
}
getchar();
getchar();
}
Upvotes: 1
Views: 56
Reputation: 197
You need to sort the array using the bubblesort
function, right? Then you don't have to return anything from the function. Because by sending the array to the function, you send the reference of it and the elements of the array will be set in the sorted order. But if you want to return anything from the function (like you wrote that you'll return a double value from the array), you must return a value of that data type. But you didn't do this. That's why the program didn't compile properly.
Upvotes: 1
Reputation: 133577
The signature of the function is the following
double bubblesort(double arrayA[], int n)
^
arguments
^
function name
^
return value
So you are stating that bubblesort
returns a value of type double
, but there is no return
statement in your code.
There are two situations in which this may happen:
In this case a function like bubblesort
shouldn't return anything, given its specification, so you should change double
to void
to explicitly state this.
But in the line
r = bubblesort(arrayA, aantal); //request the function bubblesort //r =arrayA[i];
printf("%.0f \n", r);
you store the return value of your function bubblesort. So when changing the return value type, you also have to remove this two lines.
Upvotes: 1
Reputation: 12972
If I understood correctly due to your the comment //r =arrayA[i];
, to accomplish that you could change:
r = bubblesort(arrayA, aantal,i);
and:
double bubblesort (double arrayA[], int n,int i)
{
int a,b,swap;
for (a =0;a<(n - 1);a++)
{
for (b=0;b<(n - a - 1);b++)
{
if(arrayA[b]>arrayA[b+1]) // for decreasing order use <
{
swap = arrayA[b];
arrayA[b]= arrayA[b+1];
arrayA[b+1]=swap;
}
}
}
return arrayA[i];
}
Upvotes: 1
Reputation: 213872
You declared the function as
double bubblesort (double arrayA[], int n)
So the compiler expects a return value. If you don't need one, then don't declare the function that way.
Upvotes: 1