I'm new to c++ so please take things slow with me. I heard in class that dynamic arrays like in java aren't supported so I thought as an exercise I would make a program that sort of acts like a dynamic array. I'm using a mac btw. For some reason it repeats the last number:

here is the code:

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;


void intake(int a);
int n;
int* num;

int main()
{
    int sum, average, i, a;
    cout << "this program takes in an indeterminate amount of numbers\n";
    cout << "enter a letter to stop\n";
    i = 0;
    sum = 0;
    average = 0;
    num = new int[1];
    while (!cin.fail())
    {
        i++;
        cout << "Enter number " << i << ": ";
        if (!cin.fail())
        {
            cin >> a;
            intake(a);
            sum = sum + a;
        }
    }
    average = sum / i;
    for(int i = 0; i<n;i++)
    {
        cout << num[i] << " ";
        if ((i%10) == 0)
            cout << endl;
    }
    cout << "\nAverage: " << average << endl;
    return 0;
}
void intake(int a)
{
    int* numa;
    numa = new int[n];
    for(int i = 0; i<n; i++)
        numa[i] = num[i];
    num = new int[n+1];
    for(int i = 0; i<n; i++)
        num[i] = numa[i];
    num[n] = a;
    n++;
}

and here's what the output was:

this program takes in an indeterminate amount of numbers
enter a letter to stop
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Enter number 8: 8
Enter number 9: 9
Enter number 10: 10
Enter number 11: 11
Enter number 12: 12
Enter number 13: 13
Enter number 14: 14
Enter number 15: 15
Enter number 16: 16
Enter number 17: a
1
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 16
Average: 8

Recommended Answers

All 3 Replies

"I heard in class that dynamic arrays like in java aren't supported"

Hmmmm, yes they are. Try a vector.

What is the purpose? Are you just trying to dynamically allocate a specific amount of memory for your input buffer, enter numbers one by one, and then compute the average?

Do you know what memory leaks are? This piece of code is full of them and is extremely dangerous.

After you read the wiki article, I suggest you read about proper dynamic memory usage:
Dynamic Memory Tutorial.

Using those statements, it is possible to dynamically allocate arrays, but you must do it properly using the new[] and delete[] operators and appropriate copying procedures. In C++, a dynamically-sized array does not actually exist. As the programmer, responsibility for an array's usage and sizing falls completely on you.

That being said, the STL has a vector class that exhibits some dynamic sizing behavior. However, this behavior only manifests itself under certain conditions. For example, this is still not allowed, dynamic sizing will not occur and it will cause a crash:

#include <vector>
int main() {
  std::vector<int> myVec(10, 0);  //declare a vector with 10 elements
  myVec[12] = 12;                 //store a value to the 12th element of the vector, ERROR
  return 0;
}

But you can do this:

#include <vector>
int main() {
  std::vector<int> myVec(10, 0);  //declare a vector with 10 elements
  myVec.resize(20,0);             //notice the additional statement explicitly asking for a resize
  myVec[12] = 12;                 //store a value to the 12th element of the vector, NOT AN ERROR NOW
  return 0;
}

or something like this:

#include <vector>
int main() {
  std::vector<int> myVec(10, 0);  //declare a vector with 10 elements
  for (int i = 12; i < 20; ++i) {
    myVec.push_back(i);           //push new values onto vector, will resize if required
  }
  return 0;
}

but this way is inefficient and rather stupid.

Other actions will cause a re-size as well, but these are the main ones to be concerned about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.