# dynamic allocation

Page 1 of 1

## 11 Replies - 534 Views - Last Post: 12 February 2013 - 09:48 AMRate 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=311772&amp;s=e6a92b965bb7990116db33b31500b158&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 marshaHB

Reputation: 0
• Posts: 7
• Joined: 10-February 13

# dynamic allocation

Posted 10 February 2013 - 12:13 AM

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

//Function prototypes
void arrSelectSort(double *, int);

int main()
{
double *TestScores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold average test scores

int numTest, //To hold number of test scores
count; //Counter variable

//Get the number of test scores you wish to average and put in order
cout << "How many test scores are you entering? ";
cin >> numTest;

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

//Get the test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numTest; count++)

{
cout << "Test Score " << (count + 1) << ": ";
cin >> TestScores[count];
}

//Calculate the total test scores
for (count = 0; count < numTest; count++)
{
total += TestScores[count];
}

//Calculate the average test scores
average = total / numTest;

//Dsiplay the results
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;

//Free dynamically allocated memory
delete [] TestScores;
TestScores = 0; //make TestScores point to null

//An array of pointers to int
int *arrPtrTestScores[count];

//Sort the elements of the array of pointers
arrSelectSort(TestScores, TestScores[count]);

//Display the Test Scores in ascending order
cout << "The test scores in ascending order are: \n";
return 0;
}

//This function performs an ascending order selection sort
void arrSelectSort(double *arr, int size)
{
int startScan;
double minIndex;
double minElem;

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

arr[(int)minIndex] = arr[startScan];
arr[startScan] = minElem;
}
}
```

This post has been edited by JackOfAllTrades: 10 February 2013 - 04:54 AM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: dynamic allocation

### #2 snoopy11

• Engineering ● Software

Reputation: 1136
• Posts: 3,383
• Joined: 20-March 10

## Re: dynamic allocation

Posted 10 February 2013 - 01:31 AM

Do you have an error ?

### #3 marshaHB

Reputation: 0
• Posts: 7
• Joined: 10-February 13

## Re: dynamic allocation

Posted 10 February 2013 - 04:31 AM

snoopy11, on 10 February 2013 - 01:31 AM, said:

Do you have an error ?

marshaHB, on 10 February 2013 - 04:29 AM, said:

snoopy11, on 10 February 2013 - 01:31 AM, said:

Do you have an error ?

yea when i compile it it says there is an error on line 55 and i dont know hw to keep the user from entering a negative number

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: dynamic allocation

Posted 10 February 2013 - 04:54 AM

When you post code...USE CODE TAGS!!!

### #5 marshaHB

Reputation: 0
• Posts: 7
• Joined: 10-February 13

## Re: dynamic allocation

Posted 10 February 2013 - 05:14 AM

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

//Function prototypes
void arrSelectSort(double *, int);

int main()
{
double *TestScores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold average test scores

int numTest, //To hold number of test scores
count; //Counter variable

//Get the number of test scores you wish to average and put in order
cout << "How many test scores are you entering? ";
cin >> numTest;

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

//Get the test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numTest; count++)

{
cout << "Test Score " << (count + 1) << ": ";
cin >> TestScores[count];
}

//Calculate the total test scores
for (count = 0; count < numTest; count++)
{
total += TestScores[count];
}

//Calculate the average test scores
average = total / numTest;

//Dsiplay the results
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;

//Free dynamically allocated memory
delete [] TestScores;
TestScores = 0; //make TestScores point to null

//An array of pointers to int
int *arrPtrTestScores[count];

//Sort the elements of the array of pointers
arrSelectSort(TestScores, TestScores[count]);

//Display the Test Scores in ascending order
cout << "The test scores in ascending order are: \n";
return 0;
}

//This function performs an ascending order selection sort
void arrSelectSort(double *arr, int size)
{
int startScan;
double minIndex;
double minElem;

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

arr[(int)minIndex] = arr[startScan];
arr[startScan] = minElem;
}
}
```

### #6 sepp2k

• D.I.C Lover

Reputation: 2311
• Posts: 3,573
• Joined: 21-June 11

## Re: dynamic allocation

Posted 10 February 2013 - 05:23 AM

```int *arrPtrTestScores[count];
```

You can't define an array with a variable length like that in C++. You either have to allocate the array using new[] or use a vector.

### #7 snoopy11

• Engineering ● Software

Reputation: 1136
• Posts: 3,383
• Joined: 20-March 10

## Re: dynamic allocation

Posted 10 February 2013 - 05:33 AM

Line 55,

delete line 55 as you apparently dont need it or use it, whatever.

```int in =-1;
do{
cout << "Enter a number : ";
cin >> in;
}while (in <=-1);

```

Snoopy.

This post has been edited by snoopy11: 10 February 2013 - 05:34 AM

### #8 marshaHB

Reputation: 0
• Posts: 7
• Joined: 10-February 13

## Re: dynamic allocation

Posted 11 February 2013 - 09:02 PM

do i enter this on line 27 and since i cant leave the for loop do i nest it in the do while loop?

### #9 marshaHB

Reputation: 0
• Posts: 7
• Joined: 10-February 13

## Re: dynamic allocation

Posted 11 February 2013 - 11:55 PM

snoopy the program doesnt go beyond performing the average is my sorting function wrong

### #10 marshaHB

Reputation: 0
• Posts: 7
• Joined: 10-February 13

## Re: dynamic allocation

Posted 12 February 2013 - 09:24 AM

come on snoopy dnt give up on me

i know i am kinda slow but its only cause its been like a month or since i started this course i promise i will do better

### #11 jimblumberg

Reputation: 4912
• Posts: 15,349
• Joined: 25-December 09

## Re: dynamic allocation

Posted 12 February 2013 - 09:45 AM

Post your current properly formatted code and restate your current problem. What have you done to try to solve your problem in the last several hours?

Asking questions of a specific person will usually mean you will need to wait until that person is available and willing to help. Remember this is a volunteer service and a person may not always be available to help.

But to increase your chances of receiving help try the suggestions if they don't seem to work show what you tried and ask for more information. Also using an indent style consistently will make reading and troubleshooting your program much easier.

Jim

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: dynamic allocation

Posted 12 February 2013 - 09:48 AM

Why not print out the test scores, right after the sorting, and see?

One of the great benefits of computes is the ability quickly test your code.