3 Replies - 2932 Views - Last Post: 03 November 2013 - 09:48 PM Rate Topic: -----

#1 tsgh07   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 30-October 13

Student grade entry, pointer notation

Posted 03 November 2013 - 01:46 PM

Hello and that you for any input you may provide in advance. I am using pointer notation to swap grade inputs from random order to descending order. I have been working on this all weekend with no success. before I changed all of the int's to doubles everything worked fine. however now I get an error. I tried to change the doubles back to int's and it still gives me the same error. The error I am getting is:

line 67: error C2665: 'swap' : none of the 3 overloads could convert all the argument types
c:\users\username\documents\cins121 cc++ programming\chapter 02 assignment\student
grade\student grade\source.cpp


#include <iostream>
#include <iomanip>

using namespace std;

void selectionSort ( double *, int );
void showArray( double *, int );
void showAverage ( double, int );
void swap ( double *, int );

int main()
{
	double *scores;
	double total = 0;
	double average = 0;
	int numScores;

	//Get the number of test scores.
	cout << "How many test scores will you enter?";
	cin >> numScores;

	//Dynamically allocate an array large enough to hold that many test scores
	scores = new double[numScores];
	
	for (int count = 0; count < numScores; count++)
	{
		cout << "Test score " << ( count + 1 ) << ": ";
		cin >> scores[count];
	}// end for

	//Calculate the total scores
	for (int count = 0; count < numScores; count++)
	{
		total += scores[count];
	}

	//sort the elements of the array 
	selectionSort ( scores, numScores );

	cout << "\nThe test scores in decending order, and their average are:\n\n"
		 << "Score\n-----\n";

		showArray ( scores, numScores );

		showAverage ( total, numScores );

	system ("pause");
	return 0;

}// end main

void selectionSort ( double *const array, const int size )
{
	int largest;

	// loop over size - 1 elements
	for ( int i = 0; i < size - 1; i++ )
	{
		largest = i;

		// loop to find the largest element
		for ( int index = i + 1; index < size; index++ )
			if ( &array[ i ], &array[ largest ] )
				largest = index;

		swap( &array[ i ], &array[ largest ] );
	}// end if
}// end function select

void swap( double * const element1Ptr, double * const element2Ptr )
{
	double hold = *element1Ptr;
	*element1Ptr = *element2Ptr;
	*element2Ptr = hold;
}// end function swap

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

void showAverage(double total, double numScores)
{
	double average;

	//Calculate the average
	average = total / numScores;

	//Display the results.
	cout << fixed << showpoint << setprecision(2);
	cout << "Average Score: " << average << endl;
}




Hello and ***thank*** you for any input you may provide in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Student grade entry, pointer notation

#2 Peter O   User is offline

  • D.I.C Regular

Reputation: 131
  • View blog
  • Posts: 309
  • Joined: 19-October 13

Re: Student grade entry, pointer notation

Posted 03 November 2013 - 02:15 PM

The parameter types in the function declaration on line 9 doesn't match the function definition on line 70.

Line 63 doesn't make much sense. Did you mean to call a function here?
Was This Post Helpful? 1
  • +
  • -

#3 tsgh07   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 30-October 13

Re: Student grade entry, pointer notation

Posted 03 November 2013 - 03:20 PM

View PostPeter O, on 03 November 2013 - 02:15 PM, said:

The parameter types in the function declaration on line 9 doesn't match the function definition on line 70.

Line 63 doesn't make much sense. Did you mean to call a function here?



I honestly have no idea what I am doing. I haven't had a problem what so ever this semester and this week has completely stumped.

This is what is should have been for line 63.

// function to sort an array
void selectionSort ( double *array,  int size )
{
	int largest;

	// loop over size - 1 elements
	for ( int i = 0; i < size - 1; i++ )
	{
		largest = i;

		// loop to find the largest element
		for ( int index = i + 1; index < size; index++ )
			if ( array[ index ] < array[ largest ] )
				largest = index;

		swap( &array[ i ], &array[ largest ] );
	}// end if
}// end function select


Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Student grade entry, pointer notation

Posted 03 November 2013 - 09:48 PM

Hi, as Peter O says the prototype and function call are different. Since you are swapping two values they should be the same, so line 9 must be incorrect.

09	void swap ( double *, int );

70	void swap( double * const element1Ptr, double * const element2Ptr )




Here you are saying if the current value is less than the largest then the new largest is the lower value.

  if ( array[ index ] < array[ largest ] )
       largest = index;


This post has been edited by #define: 03 November 2013 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1