This program should accept array of integers and integers that indicate the number of elements in the array. the function should then determine the median of the array. Using pointer notation. I did research to see if someone had similar problem like me, but their was only other one, it was from a person that was way more advanced and was doing a lot more with her median function and other calculations, to confusing for my knowledge.

This is what i have created.

#include <iostream>
using namespace std;

//Function Prototypes.
void showArrayPtr(double*, int);
void showMedian(double, int);
void arrSelectSort(double*, int);


int main()
{
	double *NUMBERS; //To dynamically allocate an array.
		int numValues;

	//To find out how many spaces to set aside.
	cout << "How many numbers would you like to enter? ";
	cin >> numValues;

	//To dynamically allocate an array large enough to hold that many numbers.
	NUMBERS = new double[numValues];
	if( NUMBERS == NULL)
	return 0;

	//To get users input for numbers.
	cout << "Enter the numbers below.\n";
	for (int count = 0; count < numValues; count++)
	{
		cout << "Enter value #" << (count + 1) << ": ";
		cin >> NUMBERS[count];


		//Validate the input.
		while (NUMBERS[count] <= 0)
		{
			cout << "zero or negative numbers not allowed.\n";
			cout << "Enter value #" << ( count + 1 ) << ": ";
			cin >> NUMBERS[count];
		}
	}


	//Sort the numbers in the array pointer
	arrSelectSort (NUMBERS, numValues);

	//Will display them in sorted order.
	cout << "The numbers in ascending order are: \n";
	showArrayPtr(NUMBERS, numValues);
	
	return 0;
}

void arrSelectSort(double *array, int size)
{
	int startScan, minIndex;
	double minElem;

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minElem = array[startScan];
		for( int index = startScan + 1; index < size; index++)
		{
			if (array[index] < minElem)
			{
				minElem = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minElem;
	}
}

void showArrayPtr(double *array, int size)
{
	for( int count=0; count < size; count++)
		cout << array[count] << " ";
	cout << endl;
}

void showMedian(double *array, int size)
{
		
	arrSelectSort(double *NUMBERS, int numValues);

	int medIndex = 0;
	double median = 0;

	//If median is half way between.
	if ((array % 2) !=0)
	{
		medIndex = ((numValues - 1) / 2);
		median = array[medIndex];
	}
	else
	{
		medIndex = numValues / 2;
		median = ((array[medIndex] + array[medIndex +1]) /2);
	}
	//Will display the median of the numbers.
	cout << "The median of those numbers is: \n" << median << endl;

}

I think everything is ok, up until the void showMedian part or the last part that is where i get errors like double should be preseded by ), Function thoes not take 0 arguments, illegal left operator for %, syntax error ), and numValues undeclared identifier. Some honestly don't make sense to me because if i fix those syntax errors it will be for no reason, and the others, i thought i identified what every value is, apparently not. Could you guys assist as to pointing out what im doing wrong. Much appreciation.

Recommended Answers

All 4 Replies

At line 6, you declared showMedian to take a double parameter, but
you implemented it to take a double *.

At line 84, are you trying to call arrSelectSort again? If so, it doesn't need the types in its arguments.

At line 90, you are checking a pointer to see if it's odd or even. Probably you meant to check size, not array.

At line 92, it looks like you have become confused between the size parameter and the numValues variable used elsewhere.

In the case for even number of elements, remember that array will be indexed from 0 to size-1, not 1 to size.

commented: sounds like good advice to me! +22

ok let me see if i'm understanding properly

for line 6 i should of wrote it like :

void showMedian(double*, int);

[

for line84 i dont have to input the arguments so i can live it like so

arrSelectSort();

for line 90 i should of taken the size i see that mistake now.same with line 92 i should of used the size
so the entire last part should look like so:

void showMedian(double *array, int size)
{
		
	arrSelectSort();

	int medIndex = 0;
	double median = 0;

	//If median is half way between.
	if ((size % 2)!= 0)
	{
		medIndex = ((size - 1) / 2);
		median = array[medIndex];
	}
	else
	{
		medIndex = size / 2;
		median = ((array[medIndex] + array[medIndex +1]) /2);
	}
	//Will display the median of the numbers.
	cout << "The median of those numbers is: \n" << median << endl;

}

This did help get rid off some of those errors that i now see i where so wrong. But i still get one that tells me arrSelectSort function does not take 0 arguments. What does that mean and how can i fix it.

Function call must match the function declaration. Your function takes two parameters:

void arrSelectSort(double*, int);

so you need to give it a double* and an integer when you call it on line 84 (line 4 in most recent post):

arrSelectSort(array, size);

Leave off the double* and int , which are the TYPES for the arguments. Types are already declared here in line 1, so you don't put them again.

void showMedian(double *array, int size)

I fixed the whole thing, i modified my code and everything works now how its supposed to work. thank you all for your help. those pointers helped out a lot.

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.