0

Hey,

so I'm trying to make a program that sorts user inputted "pancake sizes" and then outputs the result after a selection sort.

My problem is that with an input of 12.3, 14.2, 15.2 etc(doubles) the program won't work, and I have no idea what to do for the parameters to make it work.

I included a working copy that works for integer values, but I've been trying all the int and double combinations and the compiler won't work

Thanks

Attachments
//Roopak Mitra
//Program sorts user input from lowest to highest

#include <iostream>

using namespace std;

void fillArray(int a[], int size, int& numberUsed);
//Precondition: size is the declared size of the array a.
//Postcondition: numberUsed is the number of values stored in a.
//a[0] through a[numberUsed - 1] have been filled with
//nonnegative integers read from the keyboard.
void sort(int a[], int numberUsed);
//Precondition: numberUsed <= declared size of the array a.
//The array elements a[0] through a[numberUsed - 1] have values.
//Postcondition: The values of a[0] through a[numberUsed - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[numberUsed - 1].
void swapValues(int& v1, int& v2);
//Interchanges the values of v1 and v2.
int indexOfSmallest(const int a[], int startIndex, int numberUsed);
//Precondition: 0 <= startIndex < numberUsed. Reference array elements
//have values. Returns the index i such that a[i] is the smallest of the
//values a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].

int main( )
{
	int acog = 0;
	const int Max_Value = 50;
	int sampleArray[Max_Value];
	int numberUsed;
	fillArray(sampleArray, Max_Value, numberUsed);
	sort(sampleArray, numberUsed);
	cout << "After sorting the pancake sizes are";
	for (int index = 0; index < numberUsed; index++)
	cout << sampleArray[index] << " ";
	cout << endl;
	cin >> acog;
	return 0;	
}

void fillArray(int a[], int size, int& numberUsed)
{
	cout << "Enter pancake diameters (-1 to end entering) ";
	int next, index = 0;
	cin >> next;
	while ((next >= 0) && (index < size))
{
	a[index] = next;
	index++;
	cin >> next;
}
	numberUsed = index;
}

void sort(int a[], int numberUsed)
{
	int indexOfNextSmallest;
	for (int index = 0; index < numberUsed - 1; index++)
	{
		indexOfNextSmallest =
		indexOfSmallest(a, index, numberUsed);
		swapValues(a[index], a[indexOfNextSmallest]);
	}
}

void swapValues(int& v1, int& v2)
{
	int temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}

int indexOfSmallest(const int a[], int startIndex, int numberUsed)
{
	int min = a[startIndex];
	int indexOfMin = startIndex;
	for (int index = startIndex + 1; index < numberUsed; index++)
	if (a[index] < min)
	{
		min = a[index];
		indexOfMin = index;
		//min is the smallest of a[startIndex] through a[index]
	}
	return indexOfMin;
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.