Sorting through a dynamically allocated array

Having some errors in the search and sorting functions

Page 1 of 1

3 Replies - 1748 Views - Last Post: 04 June 2009 - 07:25 PM Rate Topic: -----

#1 Curtain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 09

Sorting through a dynamically allocated array

Posted 04 June 2009 - 05:33 PM

Hey guys, first time posting here, but I've been trying to get this thing to work for hours, but to no avail. Here is what I need to get a program to do:

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 whenever possible.

Here's what I have so far, I need to still throw in something so that it doesn't allow you to input a negative test score.
#include <iostream>
#include <iomanip>

using namespace std;

void selectionSort(double [], double);
void showArray(double []);

int main()
{
	double *scores,
			total = 0.0,
			average;
	int numtests,
		count;

	
	cout << "How many tests scores need to be averaged? ";
	cin >> numtests;


	scores = new double[numtests];

	
	cout << "\nEnter in the test scores below.\n";
	
    for (count = 0; count < numtests; count++)
    {
		cout << "Test score " << (count + 1) << ": ";
		cin >> scores[count];
    }

	  for (count = 0; count < numtests; count++)
    {
    total += scores[count];
    }

	average = total / numtests;

	cout << "The sorted scores are\n";
   showArray(scores[count]);


	cout << fixed << showpoint << setprecision(2);
    cout << "Average Score: " << average << endl;

	delete [] scores;
    scores = 0;



	cin.ignore();
	cin.get();
	return 0;
}

void selectionSort(double array[], double size)
{
   int startScan, minIndex, minValue;

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

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


I'm getting an error on line 41 with an error code of C2664. Now, as a disclaimer, I did Frankenstein some code together from a source code library from my textbook and I'm thinking that I didn't keep track of my indexes and such as well as I should have. Any ideas on where I went wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting through a dynamically allocated array

#2 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5061
  • View blog
  • Posts: 26,434
  • Joined: 10-May 07

Re: Sorting through a dynamically allocated array

Posted 04 June 2009 - 06:34 PM

View PostCurtain, on 4 Jun, 2009 - 06:33 PM, said:

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


When you call the function showArray showArray(scores[count]); you are passing in one element of the array. However you define showArray to accept two arguments, an array (double) & size (double). Your code is not passing in an array, you are passing in a single element, however the function is expecting the entire array.
Was This Post Helpful? 1
  • +
  • -

#3 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Sorting through a dynamically allocated array

Posted 04 June 2009 - 06:54 PM

I'm pretty sure the size of an array isn't stored as a double either, rather as an unsigned integer.
Was This Post Helpful? 0
  • +
  • -

#4 Curtain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-June 09

Re: Sorting through a dynamically allocated array

Posted 04 June 2009 - 07:25 PM

View Postno2pencil, on 4 Jun, 2009 - 05:34 PM, said:

View PostCurtain, on 4 Jun, 2009 - 06:33 PM, said:

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


When you call the function showArray showArray(scores[count]); you are passing in one element of the array. However you define showArray to accept two arguments, an array (double) & size (double). Your code is not passing in an array, you are passing in a single element, however the function is expecting the entire array.

Thanks a bunch, it's working like a charm right now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1