0

I am having a problem with returning the sorted value in the function sorted. I know it has something too do with fun pointers.

On a side note, how is a function suppost return something if it's declared as a void?

thanks a bunch!

 /**
 * helpers.c
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>
#include <math.h>
#include "helpers.h"
#include <stdio.h>


//Returns true if value is in array of n values, else false.

bool search(int value, int values[], int n)
{
...
}


// Sorts array of n values.


void sort(int values[], int n)
{
    int bubbled = true;
    int index = 0;
    int count = 1;

    while(bubbled)
    {
        count += 1;  //if values[index] is less then values[index+1], up the count
        if(values[index] > values[index+1])
        {
            values[index+1] = values[index];
            count = 0;  //and if not, restart

        }
        if (count == n)
            {
                bubbled = false;
            }

    }

    printf("%i", values); //error: format specifies type 'int' but the argument has type 'int *' [-Werror,-Wformat]

}

1 Answer 1

1

The array name "values" is a pointer, it does not serve us %i, to show the complete array it is necessary to use a for loop:

for( int j = 0; j < n; j++)
{
    printf("%i", values[j]);
}

A void function returns nothing, but it has an effect on the program, in this case it changes the values array, when we pass a complete array to a function the changes occur in the original array, contrary to what normally happens. We do not want the function to return anything but yes we are interested in its effect.

2
  • thank you! By effect, do you mean if its true or false?
    – Nova
    Commented Jul 5, 2017 at 19:54
  • Nay, a function like bool search (int value, int values [], int n) returns true or false, really an integer, false zero, a nonzero number, true. A void function returns nothing to the calling function, not even true or false. Just do a task in the program, in this case the ordering of the array, and that is the only thing we want, there is a return value that we want to use, only the array change
    – MARS
    Commented Jul 5, 2017 at 20:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .