1

For my assignment, we have to build .h file that links the sort.cpp file and the testsort.cpp file. However when I compile and run the testsort.cpp program it doesnt provide the sorted array. Rather it just outputs the original array. I tired to add a return function to the sort.cpp file but i get the following error:

error: return-statement with a value, in function returning 'void' [-fpermissive] return A;

testsort.cpp

#include <iostream>
#include <cstdlib>     

#include "sort.h"


int main() 
    {
      const int n = 10;
      int i, isort;
      float A[n];

      for (i=0;i<n;i++) 
          {
            A[i] = float(rand())/RAND_MAX; 
          }
  
      for (i=0;i<n;i++) 
          {
            std::cout << A[i] << " ";
          }
  
      std::cout << " unsorted\n";

      std::cout << "Enter 1 for insertion sort, 2 for partition test, 3 for quick sort\n";
      std::cin >> isort;

      switch (isort) 
          {
  
              case 1:
              InsertionSort( A, n );
              break;
  
              case 2:
              //  std::cout << "Count for small sub-array " << Partition( A, n ) << "\n";
              break;
  
              case 3:
              //  QuickSort(A,n);
              break;
  
              default:
              std::cout << isort << " is not an allowed choice\n";
          }

      for (i=0;i<n;i++) 
          {
            std::cout << A[i] << " ";
          }
      std::cout << " sorted\n";
    }

Sort.h

#ifndef SORT_H
#define SORT_H

void InsertionSort (float A[], int n){}

#endif

Sort.cpp

#include <iostream>
#include <cmath>
#include "sort.h"

      void InsertionSort (float A[], float n)
      {
        int value;
        int j;
        
        for (int i = 1; i < n; i++)
          {
            value = A[i];
            j = i;
              
              while(j > 0 && A[j-1] > value) 
                {
                   A[j] = A[j-1];
                   j--;
                }
            
            A[j] = value;
          }
         //return A; 
        //std::cout<<"Running Insertion Sort\n";
      }
  • Why is `InsertionSort`'s `n` parameter typed as `float` instead of `size_t`? – Dai Nov 21 '20 at 03:40
  • 1
    You should be using `size_t` instead of `int` for all array indexers: https://stackoverflow.com/questions/3340880/c-best-practices-int-or-size-t – Dai Nov 21 '20 at 03:41
  • 1
    Your sort algorithm is wrong. Go back and study the Insertion Sort algorithm. – JoelFan Nov 21 '20 at 03:43

2 Answers2

0
  1. A void function cannot return values.
  2. Inside InsertionSort method, var value is of type int. But array elements are of type float with value b/t 0 and 1. During execution of "value = A[i];" , value becomes 0(implicit type conversion), so sorting fails. Change type of value to float.
Rana Vivek
  • 126
  • 2
0

The way you are using sort.h and sort.cpp is wrong. Your insertion sort function in sort.cpp is never being called. What I tried was that I replaced your blank insertion sort function in sort.h with the function written in sort.cpp. And deleted sort.cpp file. Also you need to change the datatype of value in your insertion sort function to float as you are using a float array. These two changes and your sorting function is working fine.

Sort.H

#ifndef SORT_H
#define SORT_H

void InsertionSort (float A[], float n)
  {
    float value;
    int j;
    
    for (int i = 1; i < n; i++)
      {
        value = A[i];
        j = i;
          
          while(j > 0 && A[j-1] > value) 
            {
               A[j] = A[j-1];
               j--;
            }
        
        A[j] = value;
      }
     //return A; 
    //std::cout<<"Running Insertion Sort\n";
  }

#endif
aman kapoor
  • 122
  • 6