# Sorting through a dynamically allocated array

Page 1 of 1

## 3 Replies - 3124 Views - Last Post: 04 June 2009 - 07:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=108540&amp;s=78695ebc18cdadf948269bd3c3842bd7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Curtain

Reputation: 0
• 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

• Professor Snuggly Pants

Reputation: 6627
• Posts: 30,868
• Joined: 10-May 07

## Re: Sorting through a dynamically allocated array

Posted 04 June 2009 - 06:34 PM

Curtain, 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.

### #3 Hiram

Reputation: 69
• 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.

### #4 Curtain

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

## Re: Sorting through a dynamically allocated array

Posted 04 June 2009 - 07:25 PM

no2pencil, on 4 Jun, 2009 - 05:34 PM, said:

Curtain, 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.