user2969508
user2969508

Reputation: 11

How to call the selection sort function in main

So the question is to Add selection sort function to grade program above. Program should display list of grades in sorted ascending order, we are giving the selection sort function and cant change it, my question is how would I call it from the main function

Here is my code`

#include <iostream>
using namespace std;

double average(double x[], int n);
double maximum(double x[], int n);
double minimum(double x[], int n);
int nAboveAvg(double x[], int n);
void sort(double x[], int npts);


int main()
{
   double grades[50];
   int ngrades;

   cout<<"How many grades? (max = 50) ";
   cin>>ngrades;

   //create for loop to get grades from user
   for(int i = 0; i<ngrades; i++)
   {
      cout<<"Enter grade ";
      cin>> grades[i];
      while(grades[i]< 0 || grades[i] > 100)
      {
          cout<<"Invalid grade- please enter again"<<endl;
          cin>>grades[i];
      }
   }

   //call the functions
   double avg = average(grades, ngrades);
   double max = maximum(grades, ngrades);
   double min = minimum(grades, ngrades);
   int nAbove = nAboveAvg(grades, ngrades);

       //Calling the sort function
   sor = sort(grades, ngrades);
   //display results



   cout << "Average = " << avg << endl;
   cout << "# above average = " << nAbove << endl;
   cout<<"Max value is = "<<max<<endl;
   cout<<"Min value is = "<<min<<endl;
   cout<<"Array sorted "<<sor<<endl;

}

void sort(double x[], int npts)
{
  double min_value;
  int min_index;
  double temp;
  for(int i= 0; i<npts - 1; i++)
  {
      for(int j = i + 1; j<npts; j++)
      {
          if(x[j] < min_value)
          {
              min_value = x[i];
              min_index = j;
          }
      }

      temp = x[min_index];
      x[min_index] = x[i];
      x[i] = temp;
  }
  return;
}

`

Upvotes: 0

Views: 1173

Answers (1)

Fearnbuster
Fearnbuster

Reputation: 896

I think your problem is that you expect the "sort" function to return a value; it does not.

The "sort" function does not return a value, because it was defined with a "void" return value therefore trying to retrieve any data from the variable "sort" will not work (or should not, anyway).

Arrays are passed-in to functions by reference; This means that all of the changes done to the array within the sort function are are still there once the function returns; because of this, you should be outputting the "grades" array, not a non-existent return value.

EDIT: I believe that your problem is at the line:

cout<<"Array sorted "<<sor<<endl;

Trying something like this instead:

for (int i = 0; i < ngrades; ++i)
{
    cout << grades[i] << " ";
}

cout << endl;

EDIT 2: Also, change the line:

sor = sort(grades, ngrades);

to just:

sort(grades, ngrades);

EDIT 3: It turns out that there are a few problems with the "sort" function. The first, and worst, problem is that the variable "min_value" is being used without being defined.

Once I changed this, the program would run, but the "sort" function did not work properly. This brings me to the second problem: The variables "min_value" and "min_index" need to be reset for every iteration of "i".

The final problem is that, within the "j" loop, "min_value" is assigned to "x[i]", whereas it should be assigned to "x[j]":

min_value = x[i];
min_index = j;

should be:

min_value = x[j];
min_index = j;

I fixed the function and tested it to make sure that it works. Here is the code.

void sort(double x[], int npts)
{
    double min_value;
    int min_index;
    double temp;

    for (int i = 0; i < npts - 1; i++)
    {
        min_value = x[i];
        min_index = i;

        for (int j = i + 1; j < npts; j++)
        {
            if (x[j] < min_value)
            {
                min_value = x[j];
                min_index = j;
            }
        }

        temp = x[min_index];
        x[min_index] = x[i];
        x[i] = temp;
    } 

     return;
}

Upvotes: 1

Related Questions