My teacher wantes me to Write a function that dynamically allocates an array of doubles and returns a pointer to the new array. Also The function should accept an integer parameter that indicates the size of the new array. can you guys pls give me some tips on how Write a main program to test.
#include <iostream>
double* make_double_array(int size);
int main() {
intconst length = 20;
double *ptr = make_double_array(length);
int sum = 0;
for(int i = 0; i < length; i++) {
ptr[i] = i+1;
sum += ptr[i];
}
std::cout << "The sum of 1 to " << length << " is " << sum << std::endl;
//free the memory of ptr, unless you want to have a memory leak
return 0;
}
double* make_double_array(int size) {
double *temporary;
//you should put your function definition here
return temporary;
}
I am going to assume that you actually wanted help creating a dynamic array. Here is a link to a tutorial on this site showing how to create one: http://www.cplusplus.com/doc/tutorial/dynamic/