Wow, I am having huge problems passing values between functions and wrapping my head around pointers.

For some reason when I tried to return values from functions they would not pass and when I tried putting pointers in the place, passing them kept giving me compiler errors. Now I have no errors but my values are not passing correctly.

Any help I could get with this code would be great!

#include<iostream>

#include<iomanip>

using namespace std;



//function prototypes

void displayScores(int, int[]);

void sortString(int, int[]);

double averageScore(int, int, int[]);

int dropLowest(int, int, int[]);



int main()

{

	int count = 0,

		arraySize = 0,

		*array,

		lowest = 0,

		*lowPtr = &lowest;

	double average = 0.0;

		

	

	cout << "How many test scores would you like to enter? ";

	cin >> arraySize;

	

	//alocate memory

	array = new int[arraySize];

	

	//collect data

	for (count = 0; count < arraySize; count++)

	{

		cout << "Test " << (count + 1) << " score : ";

		cin >> array[count];

		

		//verify input

		if (array[count] < 0)

		{

			cout << "Please enter a score greater or equal to zero!\n";

			cout << "Enter the " << (count + 1) << " test score : ";

			cin >> array[count];

		}

	}

	//sort the array

	sortString(arraySize, array);

	

	//display scores

	displayScores(arraySize, array);

	

	//droplowest score

	dropLowest(arraySize, *lowPtr, array);

	

	// display lowest

	cout << lowPtr << "Is the lowest score\n" ; 

	

	//calculate average

	averageScore(arraySize, *lowPtr, array);

	

	//display scores

	displayScores(arraySize, array);

	cout << "The average is " << average << endl;

	

	//delete memory

	delete [] array;

	array = 0;

	

	return 0;

}



void displayScores(int arraySize, int array[])

{

	for (int i = 0; i < arraySize; i++)

	{

		cout << array[i] << endl;

	}

}



void sortString(int arraySize, int array[])

{

	int swapScore = 0,

		highest = 0;

	

	for (int startIndex = 0; startIndex < (arraySize - 1); startIndex++)

	{

		highest = array[startIndex];

		swapScore = array[startIndex];

		

		for (int count = startIndex + 1; count < arraySize; count++)

		{			



			if (array[count] > highest)

			{

				highest = array[count];

				swapScore = array[startIndex];

				array[count] = swapScore;

				array[startIndex] = highest;

			}

		}

	}

}



double averageScore(int arraySize, int lowPtr, int array[])

{

	double total = 0;

	double average = 0.0;

	

	for (int i = 0; i < arraySize; i++)

	{

		total += array[i]; 

	}

	total -= lowPtr;



	average = (total / (arraySize - 1));

	cout << fixed << showpoint << setprecision(2);

	cout << "the total is " << total << endl;

	cout << "The average is " << average << endl;

	return average;

}

int dropLowest(int arraySize, int lowPtr, int array[])

{

	lowPtr = array[0];

	

	for (int i = 1; i < arraySize; i++)

	{

		if (array[i] < lowPtr)

			lowPtr = array[i];

	}

	return lowPtr;

}

Thanks guys!
Jay

In main( ), you have a value lowest and a pointer to it, lowptr. Your function dropLowest( ) takes a parameter by value, to which you store the lowest found value, and return that.

Back in main( ), you do not capture that returned value, so it's lost (the dropped bits are what make the dust bunnies you find in your computer.)

Why not simply pass the variable lowest by reference, and then it will be modified by the function.

void dropLowest(int arraySize, int & lowPtr, int array[])
{
	lowPtr = array[0];

	for (int i = 1; i < arraySize; i++)
	{
		if (array[i] < lowPtr)
			lowPtr = array[i];
	}
}

//usage
int lowvalue;
int arr[10];
dropLowest( 10, lowvalue, arr );

Alternatively, you could set the function up to return the value, and use that

int dropLowest(int arraySize,  int array[])
{
	int lowPtr = array[0];

	for (int i = 1; i < arraySize; i++)
	{
		if (array[i] < lowPtr)
			lowPtr = array[i];
	}
        return lowPtr;
}

//usage
int lowvalue;
int arr[10];
lowvalue = dropLowest( 10, arr );

The second version is my recommendation. The reasoning being, think of it in terms of a mathematical function - something goes in, something comes out - by means of the return value. When a function is meant to give back a single value, the return mechanism should be first choice.

Thanks!

Sometimes it is the easiest things...

Cheers!

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.