1

Hi I'm new to programming and struggling with these application. Here is the error code:

Error C3078 you cannot 'new' an array of unknown bounds line 17

I am struggling to understand and grasp the concept of pointers. Any help is great thanks for looking at my code.

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

//function prototypes
int getArray(int num);
void selectionsortArray(int *[], int);
double findAverage(int *scores, int nums);
void showSortedArray(int *[], int);

int main()
{
int *scores = new int[]; // To dynamically allocate an array

double total = 0, //accumulator
        average; //to hold average test scores

int testScores, // To hold the amount of test scores the user will enter
    count; //Counter variable

// Request the amount of test scores the user would like to enter
cout << "How many test scores do you wish to process: ";
cin >> testScores;

getArray(testScores);

selectionsortArray(&scores, testScores);

cout << "Test scores sorted:\n\n";
showSortedArray(&scores, testScores);

average = findAverage(scores, testScores);

//set precision
cout << setprecision(2) << fixed;
cout << "\tAverage Score\n";
cout << average;






}

int getArray(int num)
{
int *array, ptr; //set array pointer equal to 0
int count;

cout << "\tPlease enter Test Scores by percent:\n";
for (count = 0; count < num; count++)
{
    cout << "Test score #" << (count+1)<< ": ";
    cin >> array[count];
}

ptr = *array; //Set the ptr to be returned with the array

return ptr; // return ptr
}

void selectionsortArray(int *arr[], int testScores)
{
int startscan, minIndex;
int *minElem;

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

void showSortedArray(int *arr[], int testScores)
{
for ( int count = 0; count < testScores; count ++)
    cout << *(arr[count]) << " \n";
}

double findAverage(int *scores, int testScores)
{
double average = 0;

for (int count = 0; count < testScores; count++)
    average += scores[count];

average /= testScores;

return average;
}
  • This code has multiple , serious issues. `getArray` writes to a wild pointer, and returns a single int. The comments in `getArray` are wrong (they say something, but the code actually does something else). `selectionsortArray` takes `arr` incorrectly and this won't even compile. You seem to be missing a basic understanding of what a pointer is -- go back and review your notes or whatever on how pointers work. – M.M Jul 04 '15 at 07:03
  • Thank you for your help. I have been struggling with pointers for some reason and don't quite understand in what circumstance, why and with what do you dereference and reference.0 I keep going back and reading just doesn't seem to stick. With the comment in get array that was just bad practice. I had the code doing one thing and decided to change it and never changed the comments..... Maybe finding a video or a read online will help. Again thank you for your time! – PARKER GRAY Jul 05 '15 at 14:50

2 Answers2

1
int *scores = new int[]; // To dynamically allocate an array

Above line will fail with the error message you specified in question, since you didnt specify the array length for initialization. In C++ array declaration using dynamic allocation need a constant integer as array size.

If you change your code as follow, or a number suitable for your need in place of 10 , will solve your issue.

int *scores = new int[10]; // To dynamically allocate an array

Since you allocate memory using new operator, you should deallocate after usage as follows:

delete[] scores;
M.M
  • 138,810
  • 21
  • 208
  • 365
Steephen
  • 14,645
  • 7
  • 40
  • 47
1

Your error is here:

int *scores = new int[]; // To dynamically allocate an array

You can't do this in C++. If you don't know the size of array you can create the pointer:

int *scores;

When you will know the size, you will be able to allocate memory on heap:

scores = new int[5]; // To dynamically allocate an array

About the difference between static and dynamic allocation you can read here:

Declaring array of int

And delete scores after you are done with it or you will get memory leak.

Community
  • 1
  • 1
demonplus
  • 5,613
  • 12
  • 49
  • 68