My prompt is:
1. Prompt the user and input a variable named array_size.2. Dynamically allocate an array, named array_values (use a pointer variable) exactly large enough to
hold array_size number of integer values.
3. By using a loop initialize the array with integer values ranging from 1 to array_size. First array
element will be assigned the value 1, second element will be assigned the value 2, ..., the last
element will be assigned the value array_size.
4. By using a loop find the average of the values of the array and print the average value.
5. Before the program ends, make sure to delete the array_values from the memory.
I've written this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// This program will use dynamic allocation
//
#include <iostream>
using namespace std;
int main()
{
int array_size;
cout << "How many values would you like to store?";
cin >> array_size;
int* values = new int[array_size];
for (int i = 0; i < array_size; i++)
{
array_size[i] = i;
cout << values[i] << endl;
}
delete[] values;
}
|
I'm not understanding how to loop and save a new value under the next index every iteration. I thought that since "i" increases every loop, it would move to the next index location, but I'm not sure how to increase the actual number saved there. My error says that I must have a pointer-to-object type in my loop. I'm not sure what that means, my guess is that I need to use a pointer. I'm very new to this and struggling to grasp the concepts.