Jump to content

Array Allocator... Need a lil C++ help

Jerochy

Really can't quite figure out what is wrong here. The problem wants me to dynamically allocate an array of doubles, populate it, and return the point to a new array. Also, the function should take in an integer parameter that shows that indicates of the new array. 

Here's my code: 
 

 
 
#include <iostream>
#include <array>
#include <iomanip>
#include <string>
 
 
using namespace std;
 
int main()
{
int size = 1000;
int*number;
int getNumber();
int *prt = nullptr;
 
void getNumber(int*ptr);
{
cout << "Enter a number";
cin >> ptr;
 
}
 
for (int i = 0; i < size; i++)
{
intPtr = 0;
cin >> intPtr;
delete[]intPtr;
}
int*buildArray();
{
int*array = new int;
for (int i = 0; i < size; i++);
{
array = i;
return array;
}
}
 
 
 
}


Any help at all would help thank you!
Link to comment
Share on other sites

Link to post
Share on other sites

i don't think I understood what you need? You want to create an array of doubles, populate it and return the pointer to that array?

 

If I understood correctly, this is what you want:

#include <iostream>using namespace std;double * buildArray(int S);double * buildArray2(double *, int, int);int main() {	int n;	double * arr;	do {		cout << "Input size of the array: ";		cin >> n;	} while (n < 0);	cout << "Building array..."<<endl;	arr = buildArray(n);	cout << "The created array : ";	for (int i = 0; i < n; i++)		cout << arr[i] << " ";	cout << endl;	//I think you also want to create an array starting from a pos the user enters?	int pos;	double * arr2;	do {		cout << "Input pos: ";		cin >> pos;	} while (pos < 0 || pos > n);	arr2 = buildArray2(arr, pos, n);	cout << "The second array: ";	for (int i = 0; i < n - pos; i++)		cout << arr2[i] << " ";	cout << endl;	system("pause");	return 0;}double * buildArray(int n) {	double * temp = new double[n];	for (int i = 0; i < n; i++)		temp[i] = i;	return temp;}double * buildArray2(double* arr, int pos, int n) {	double * temp = new double[n - pos];	int k = 0;	for (int i = pos; i < n; i++) {		temp[k] = arr[i];		k++;	}	return temp;}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×