Question

For this assignment I have to write a selection-sort function in another file that sorts arrays in ascending order to use in my driver. I've already searched this question for my error and I still haven't found anything that I've seen can help me out. Can someone please help me out?

Here's what I have so far.

#include <iostream>
using namespace std;


void *selectionsort(int values[], int size){


    for(int i=0; i<size-1; i++){
        for(int j=0; j<size; j++){
            if(values[i] < values[j]){
                int temp = values[i];
                values[i] = values[j];
                values[j] = temp;


            }

        }

    }
}

Here's my driver if needed.

#include <stdlib.h>
#include <iostream>
#include "selection.cpp"

using namespace std;

#define ArraySize 10 //size of the array
#define  Seed  1 //seed used to generate random number

int values[ArraySize];


int main(){


    int i;

    //seed random number generator
    srand(Seed);

    //Fill array with random intergers
    for(i=0;i<ArraySize;i++)
        values[i] = rand();

    cout << "\n Array before sort" << endl;

    for(i=0;i<ArraySize; i++)
        cout << &values[]<< "\n";

    //int* array_p = values;

    cout << "\n Array after selection sort." << endl;

    //Function call for selection sort in ascending order.
    void *selectionsort(int values[], int size);

    for (i=0;i<ArraySize; i++)
        cout << &values[] << "\n";


    //system("pause");

}
Was it helpful?

Solution

Your function is not void change it to

void selectionsort(int values[], int size){

Extra: Your function returns a void pointer, if you define it like that. And a void pointer is a pointer that can point to... well anything.

And of course see @brokenfoot's answer to learn how to call a function.

OTHER TIPS

  1. Since you have defined your main() as int main() means it returns an int. SO, before your end bracket in main, add a return 0;
  2. since you aren't returning from selectionsort() function, make it void:
    void selectionsort(int values[], int size)
  3. And when you are calling this function in main(), call it like:
    selectionsort(values,size);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top