I am stuck on a problem. I am given code and I am suppose to do the follwong sway fucntion.

The last three lines of code in the function selection_sort perform an essential operation that is commonly found in sort algorithms. Replace those three statements with a call to a function named swap that takes exactly three arguments (the array name and the two subscripts of the array whose values are to be swapped). The swap function should not return any value. Add both the prototype and the definition of swap to the program.

I am confused as to what I am being asked. Am I even close to a solution. I complied the program and I got

This is the code

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

#include <time.h>
#include <stdlib.h>

// (Use the preprocessor to replace every occurrence of NUM_COLS by 5
// and every occurrence of MAX_SIZE by 23.)
//
#define NUM_COLS 5
#define MAX_SIZE 23

void print_array (int [], int, int);
void selection_sort (int[], int);
void swap(int [], int& [], int&[]);

void main()
{
   int numbers[MAX_SIZE];
   int loc;

   srand(time(0));    // Initialize the random number generator

   for(loc = 0; loc < MAX_SIZE; loc++)
      numbers[loc] = rand() % 100 + 1;
   cout << "The initial array of random numbers:" << endl;
   print_array(numbers, MAX_SIZE, NUM_COLS);

   selection_sort (numbers, MAX_SIZE);
   cout << "\n\nThe sorted array of random numbers:" << endl;
   print_array(numbers, MAX_SIZE, NUM_COLS);
   swap(numbers,NUM_COLS, MAX_SIZE);
}

// Print an array of n integers, k columns of numbers per line.
//
void print_array(int a[], int n, int k)
{
   int i, c;
   for (i = 0; i < n; i += k) {
      for (c = 0; c < k && i + c < n; c++)
          cout << setw(5) << a[i + c];
      cout << endl;
   }
}

// Sort an array of n integers into ascending order, using
// selection sort.
//
void selection_sort(int a[], int n)
{
   int i, j, smallest;
   int temp;
   for (i = 0; i < n-1; i++) {
      smallest = i;
      for (j = i + 1; j < n; j++) {
         if (a[j] < a[smallest])
            smallest = j;
      }
  
	  int i, smallest;
      int temp = a[i];
      a[i] = a[smallest];
      a[smallest] = temp;
   }

void swap(int& [], int& [], int&[])
{
}

Here is the compile errors

1>------ Rebuild All started: Project: lab10_a, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'lab10_a', configuration 'Debug|Win32'
1>Compiling...
1>Lab10a.cpp
1>.\Lab10a.cpp(36) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(36) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(43) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>.\Lab10a.cpp(53) : error C2664: 'swap' : cannot convert parameter 2 from 'int' to 'int *[]'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>.\Lab10a.cpp(88) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(88) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(88) : error C2234: 'abstract declarator' : arrays of references are illegal
1>.\Lab10a.cpp(89) : error C2601: 'swap' : local function definitions are illegal
1>        .\Lab10a.cpp(72): this line contains a '{' which has not yet been matched
1>.\Lab10a.cpp(91) : fatal error C1075: end of file found before the left brace '{' at '.\Lab10a.cpp(72)' was matched
1>Build log was saved at "file://c:\Users\moporho\Documents\HO8\lab10_a\Debug\BuildLog.htm"
1>lab10_a - 8 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

>>function named swap that takes exactly three arguments (the array name and the two >>subscripts of the array whose values are to be swapped).
>>The swap function should not return any value.
>> Add both the prototype and the definition of swap to the program.

Your prototype's subscripts are wrong, you are trying to pass arrays of references to int,
instead the subscripts are simple integers telling the indexes whose data is to be swapped,
so

void swap(int [], int& [], int&[]);

turns into

void swap(int [], const int, const int)

The subscripts are declared as 'const', you could do also with plain non-const ints, i.e.

void swap(int [], int, int)

Then the selection_sort, the call to the swap() function is now readily in-place

void selection_sort(int a[], int n)
{
   int i, j, smallest;
   int temp;
   for (i = 0; i < n-1; i++) {
      smallest = i;
      for (j = i + 1; j < n; j++) {
         if (a[j] < a[smallest])
            smallest = j;
      }

        // swap the values at the indexes 'i' and 'smallest'
        swap(a, i, smallest);
//      int i, smallest;
//      int temp = a[i];
//      a[i] = a[smallest];
//      a[smallest] = temp;
   }
// A missing curly brace in your original code caused the
// [B]'swap' : local function definitions are illegal[/B]
// error message ... 
// You cannot define a function inside a function, ever, so
// here ends the selection_sort() function ...
[B]}[/B]

Here is the swap function to be filled in ...

void swap(int arr[], const int left, const int right)
{
    // here you need to swap the two values,
    // at the given indexes (left, right),
    // the way to do it, is actually given in the 
    // selection_sort() function, (now commented out)
}

Then a couple of things;

1) remove the swap(numbers,NUM_COLS, MAX_SIZE); line completely,
swap() is to be called from within the selection_sort() only
2) pick up the habbit of defining main() as function that returns an integer, i.e.

int main(int argc, char * argv[])
{
    // all your code here ...

    return 0;
}

I am trying to understand but I am lost. I followed your advise, but I amgetting errors. Where am I going wrong?

I see where the swap show happen, but I can not get the program to compile.

#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

#include <ctime>
#include <stdlib.h>

// (Use the preprocessor to replace every occurrence of NUM_COLS by 5
// and every occurrence of MAX_SIZE by 23.)
//
#define NUM_COLS 5
#define MAX_SIZE 23

void print_array (int [], int, int);
void selection_sort (int[], int);
void swap(int [], const int, const int);

int main(int argc, char * argv[])
{
   int numbers[MAX_SIZE];
   int loc;

   srand(time(0));    // Initialize the random number generator

   for(loc = 0; loc < MAX_SIZE; loc++)
      numbers[loc] = rand() % 100 + 1;
   cout << "The initial array of random numbers:" << endl;
   print_array(numbers, MAX_SIZE, NUM_COLS);

   selection_sort (numbers, MAX_SIZE);
   cout << "\n\nThe sorted array of random numbers:" << endl;
   print_array(numbers, MAX_SIZE, NUM_COLS);

   return 0;
  
}

// Print an array of n integers, k columns of numbers per line.
//
void print_array(int a[], int n, int k)
{
   int i, c;
   for (i = 0; i < n; i += k) {
      for (c = 0; c < k && i + c < n; c++)
          cout << setw(5) << a[i + c];
      cout << endl;
   }
}

// Sort an array of n integers into ascending order, using
// selection sort.
//
void selection_sort(int a[], int n)
{
   int i, j, smallest;

   for (i = 0; i < n-1; i++) 
   {
      smallest = i;
   }
      for (j = i + 1; j < n; j++) 
	  {

         if (a[j] < a[smallest])
            smallest = j;
      }
	  
	  // swap the values at the indexes 'i' and 'smallest'
      swap(a, i, smallest);
	  {
  
	  smallest=i;
      int temp = a[i];
      a[i] = a[smallest];
      a[smallest] = temp;

   }
}

Here are the errors:

1>.\Lab10a.cpp(43) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>Compiling manifest to resources...
1>Linking...
1>Lab10a.obj : error LNK2019: unresolved external symbol "void __cdecl swap(int * const,int,int)" (?swap@@YAXQAHHH@Z) referenced in function "void __cdecl selection_sort(int * const,int)" (?selection_sort@@YAXQAHH@Z)
1>.\Debug/lab10_a.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\moporho\Documents\HO8\lab10_a\Debug\BuildLog.htm"
1>lab10_a - 2 error(s), 1 warning(s)

Thanks,
M

>> Where am I going wrong?

You have not defined the swap() function, you only have a prototype for it.
So you really need to provide the code for the swap() i.e. fill in the missing part
which does the actual swap ...

void swap(int arr[], const int left, const int right)
{
    // here you need to swap the two values,
    // at the given indexes (left, right),
    // hint: use a temporary int variable to do it, e.g. 
    // int temp;

}

Then as to the sort

void selection_sort(int a[], int n)
{
   int i, j, smallest;
   int temp;
   for (i = 0; i < n-1; i++) {
      smallest = i;
      for (j = i + 1; j < n; j++) {
         if (a[j] < a[smallest])
            smallest = j;
      }
        // swap the values at the indexes 'i' and 'smallest'
        swap(a, i, smallest);

// Note that the above call to swap() performs the same thing
// that the below three lines would do if those were not commented out
//      int temp = a[i];       // You don't need this line
//      a[i] = a[smallest];   // You don't need this line
//      a[smallest] = temp; // You don't need this line 
   }
}

I think I got it. Let me see...

void selection_sort(int a[], int n)
{
   int i, j, smallest;
   int temp;
   for (i = 0; i < n-1; i++) {
      smallest = i;
      for (j = i + 1; j < n; j++) {
         if (a[j] < a[smallest])
            smallest = j;
      }
	  // swap the values at the indexes 'i' and 'smallest'
	  swap(a, i, smallest);
   }
}
	  
void swap(int a[], int i, int smallest)
{
	int temp = a[i];
	a[i] = a[smallest];
	a[smallest] = temp;
		
}

Or am I close...?

I think I got it.

Yes, it seems like you got it ...

You are the best! Thank you! I think I understand it better now!
M~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.