Selection Sort Program with Comparison and Swap Counter | Code Help

  • Thread starter Thread starter heavyc
  • Start date Start date
  • Tags Tags
    Sort
AI Thread Summary
The user is attempting to implement a selection sort program that counts comparisons and swaps but encounters four error messages. Key issues identified include the absence of a main() function, which is essential for any C program, and the need for clarification on the specific error messages to provide better assistance. The code also contains potential problems, such as the use of "iostream.h" instead of "iostream" and the incorrect handling of the array in the indexoflargest function. Suggestions emphasize the importance of sharing error messages for effective troubleshooting. Addressing these issues will help in successfully implementing the selection sort with the desired counters.
heavyc
Messages
16
Reaction score
0
Okay here's the deal I am writing a program so that i could put a counter into a selection sort and count the comparsions and the swaps but when i wrote thw code I get 4 error messages and I don't know why so here is the code if anyone can help it would be nice thank you
Code:
 #include <iostream.h>
#include <stdlib.h>
#include <cstddef>
typedef int Select;
void selectionsort(Select theArray[], int n)
{
n = 5;
for (int last = n-1; last >= 1; --last)
{
int largest = indexoflargest(theArray, last + 1);
swap(theArray[largest], theArray[last]);
}
}
int indexoflargest(const Select theArray[], int size)
{
int indexsofar = 0;
for (int currentindex = 1; currentindex < size; ++currentindex)
{
if(theArray[currentindex] > theArray[indexsofar])
indexsofar = currentindex;
}
return indexsofar;
}
void swap(Select& x, Select& y)
{
Select temp = x;
x = y;
y = temp;
}
 
Physics news on Phys.org
Perhaps sharing the error messages with us might help.
 
The error messages would help. And also, if that is your program in its whole, you don't have a main() function which every C program must have. There is nothing calling your predefined functions.
 
Back
Top