Hello...I'm trying to write this program answering this question and I keep getting C2440 errors and C2664 errors and I did some research but couldn't find anything on how to correct it.

The question says : Write a program that dynamically allocates an array large enough to hold a user defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation wherever possible.

This is what I have ,any help would be greatly appreciated .thanks

#include <iostream>
#include <iomanip>
using namespace std;

void arrAscendingOrder(double *[], int);
void showArrPtr(double *[], int);
void showAverage(double , int);

int main()
{
	double *scores,				//To dynamically allocate an array  
		   total=0.0,			//Accumulator
		   average;				//To hold the averge scores
	int numScores;				//To hold the number of test scores
	
	
	//Get the number of test scores.
	cout<<"How many test scores would you like to process?";
	cin>>numScores;

	//Dynamically allocate an array large enough to hold that many
	//test scores
	scores = new double[numScores];

	//Get the test score for each test
	cout<<"Enter the test scores below.\n";
	for (int count=0; count<numScores; count++)
	{
		cout<<"Test "<<(count+1)<<": ";
		cin>> scores[count];
	}
	
	//Calculate the total scores
	for (int count=0; count<numScores; count++)
	{
		total += scores[count];
	}
	
	arrAscendingOrder (scores, numScores);

	showArrPtr (scores, numScores);
	
	showAverage(total, numScores);

	//Free dynamically allocated memory.
	delete [] scores;
	scores =0;		//make sales point to null.

	return 0;

}

void arrAscendingOrder(double *array[], int size)
{
	int startScan, minIndex;
	int *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 showArrPtr(double *array[], int size)
{
	for (int count=0; count< size; count++)
		cout<< *(array[count])<<" ";
	cout<<endl;
}

void showAverage(double total, int numScores)
{
	double average;
	//Calculate the average  
	average = total / numScores; 
	//Display the results.
	cout<<fixed<<showpoint<<setprecision(2);
	cout<<"Average Score: "<<average<<endl;
}

Recommended Answers

All 7 Replies

You declare the first arrAscendingOrder and showArrPtr paremeter as an array of pointers to double ( Why?! ). However the only array (accessed via score pointer) in your program is an ordinar array of doubles. You are trying to pass it in these functions.

See your showArray correct parameter declaration as array of doubles.

That's all.

Yet another defect: you don't check user input. It's extremely bad practice. Avoid it.

if (!(cin >> numscores) || numscores <= 0) {
    // not a number or incorrect value...
}

Use code tag with the language specifier for your snippets:
[code=cplusplus] source

[/code]
Mark wrong statements in your snippets. As usually nobody want to copy/paste all texts in IDE to reproduce your syntax errors.

You declare the first arrAscendingOrder and showArrPtr paremeter as an array of pointers to double ( Why?! )

I also don't understand exactly why he's doing it in that way ...

To the OP: Array's names are actually constant pointers to the first element of the array, so if you intended to pass a pointer to the array, you were already doing it in the wrong way ...
If you pass an array to a function, a constant pointer to the first element of the array is passed to the function ...

I'm new to c++ and I don't really understand how to use pointers correctly to answer this question or any question for that matter. Can you give me some hints? or a little more detailed explanation of why what I did was wrong..I'm a little lost here. Thanks (and for the input validation ..I always add it in when I'm done..it just a habit but thanks for pointing it out)

I think the way you are declaring your parameters is incorrect
You have arrAscendingOrder(double *[], int).

I think you jsut need (double * , int) for a parameter list because your scores array is a pointer to the memory location to the first elements.

Correct me if I'm wrong,

I hope this helped

It's so simple: if you want to process a Type array argument then declare the correspondent parameter as Type a[] or Type* a . Now access this array elements as a[i] .

An array argument always passed as a pointer to the first array element (an array index sequence started from 0, so it's a[0] . That's why you can declare array parameter as a pointer.

If you don't want to change this array elements then better declare the parameter as const Type a[] or const Type* . It's exactly your case in show... functions.

Yet another good advice: never start coding until you understand so basic language concepts, otherwise it looks like you young pilot try to become familiar with landing procedures after take off only (via Internet link aboard ;)).

Thanks. That was very helpful. I just needed to take off the brackets so it's array* not array*[ ]

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.