Ok i am having alittle problem with my code, I am getting 4 errors for having these things unidetitfied but they are so I dont know what is is here is the code that I have so far. The object is to have counters that will count how many comparisions and how many swaps are being made.

#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;
}

Dani AI

Generated

Immediate diagnosis and plan of attack: the compiler errors come from calling helper routines before the compiler has seen their declarations, and from the choice of swap as a helper name (it can clash with library overloads). As noted, add forward declarations (or move definitions above selectionSort) and don’t hard-code the array size inside the sort routine — that overrides the caller’s length. ’s point about posting the exact compiler diagnostics is valid: full error text speeds debugging. The original goal — counting comparisons and swaps — is best handled by passing two counters by reference (or returning a small struct).

The following shows a concise, modern-safe fix: prototypes up front, selectionSort uses the passed size (no n = 5), and counters are updated only when a comparison or a swap actually occurs. The helper swap is renamed to avoid ambiguity.

#include <iostream>

typedef int Select;

int indexOfMax(const Select arr[], int length);
void swapValues(Select &a, Select &b);
void selectionSort(Select arr[], int length, int &comparisons, int &swaps);

void selectionSort(Select arr[], int length, int &comparisons, int &swaps) {
    comparisons = swaps = 0;
    for (int last = length - 1; last > 0; --last) {
        int maxIdx = 0;
        for (int i = 1; i <= last; ++i) {
            ++comparisons;
            if (arr[i] > arr[maxIdx]) maxIdx = i;
        }
        if (maxIdx != last) { swapValues(arr[maxIdx], arr[last]); ++swaps; }
    }
}

int indexOfMax(const Select arr[], int length) { /* optional: similar logic */ }

void swapValues(Select &a, Select &b) { Select tmp = a; a = b; b = tmp; }

Practical tips: use <iostream> (not old <iostream.h>), or include <algorithm> and call std::swap. Keep functions small, test with small arrays in main, and compile with warnings enabled so any remaining name or prototype issues show clearly.

Recommended Answers

All 4 Replies

Prototypes of functions are missing and change the name of swap function as it is already defined funtion

Prototypes of functions are missing and change the name of swap function as it is already defined funtion

Argh the questions in here...
Post error reports for crist sake...
btw lol, (real reason for posting) 200+ post and still a junior poster, does not sound right ;)

200+ post and still a junior poster, does not sound right ;)

Site undergone a big design change recently....earlier many many memebrs including me had a title of posting whiz...which got changed to junior poster....earlier anyone with 100+ posts was posting whiz....now i think its much more than that....i guess site is trying to raise the standards...doesn't matter much to me....i am here to learn and help...not in chase of any titles....there was a thread regarding this in geek lounge recently...if u want u can take a look at it

Hehe, ok

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.