//Maxwell Brewer CS1A Chapter 9, p. 537, #1
//
/*******************************************************************************
* ARRAY ALLOCATOR
* _____________________________________________________________________________
* This program will
* _____________________________________________________________________________
* INPUT
*
*
* OUTPUT
*
*
******************************************************************************/
#include <iostream>
using namespace std;
int *arrayAllocator(int numElements){
//first check if argument is 0 or negative
if(numElements <= 0){
//in this case return a null pointer
return nullptr;
}
//otherwise, dynamically allocate memory
//for an array of specified size
int *ptr = new int[numElements];
//return the created pointer
return ptr;
}
int main() {
int numElements = 70;
int* myArray = arrayAllocator(numElements);
if (myArray != nullptr) {
// Use the array (example)
for (int i = 0; i < numElements; i++) {
myArray[i] = i * 2;
}
// Print the array
for (int i = 0; i < numElements; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
// Deallocate memory
delete[] myArray;
} else {
cerr << "Array allocation failed or invalid size." << endl;
}
return 0;
}
Ly9NYXh3ZWxsIEJyZXdlciAgICAgICAgICAgICAgICAgQ1MxQSAgICAgICAgICAgICAgICAgICAgIENoYXB0ZXIgOSwgcC4gNTM3LCAgIzEKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogQVJSQVkgQUxMT0NBVE9SCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIFRoaXMgcHJvZ3JhbSB3aWxsIAogKiBfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXwogKiBJTlBVVAogKiAgIAogKiAKICogT1VUUFVUCiAqICAgCiAqCiAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgKmFycmF5QWxsb2NhdG9yKGludCBudW1FbGVtZW50cyl7CiAgICAvL2ZpcnN0IGNoZWNrIGlmIGFyZ3VtZW50IGlzIDAgb3IgbmVnYXRpdmUKICAgIGlmKG51bUVsZW1lbnRzIDw9IDApewogICAgICAgIC8vaW4gdGhpcyBjYXNlIHJldHVybiBhIG51bGwgcG9pbnRlcgogICAgICAgIHJldHVybiBudWxscHRyOwogICAgfQogICAgCiAgICAvL290aGVyd2lzZSwgZHluYW1pY2FsbHkgYWxsb2NhdGUgbWVtb3J5CiAgICAvL2ZvciBhbiBhcnJheSBvZiBzcGVjaWZpZWQgc2l6ZQogICAgaW50ICpwdHIgPSBuZXcgaW50W251bUVsZW1lbnRzXTsKICAgIAogICAgLy9yZXR1cm4gdGhlIGNyZWF0ZWQgcG9pbnRlcgogICAgcmV0dXJuIHB0cjsKIH0KIAogaW50IG1haW4oKSB7CiAgICBpbnQgbnVtRWxlbWVudHMgPSA3MDsKICAgIGludCogbXlBcnJheSA9IGFycmF5QWxsb2NhdG9yKG51bUVsZW1lbnRzKTsKCiAgICBpZiAobXlBcnJheSAhPSBudWxscHRyKSB7CiAgICAgICAgLy8gVXNlIHRoZSBhcnJheSAoZXhhbXBsZSkKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG51bUVsZW1lbnRzOyBpKyspIHsKICAgICAgICAgICAgbXlBcnJheVtpXSA9IGkgKiAyOwogICAgICAgIH0KCiAgICAgICAgLy8gUHJpbnQgdGhlIGFycmF5CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBudW1FbGVtZW50czsgaSsrKSB7CiAgICAgICAgICAgIHN0ZDo6Y291dCA8PCBteUFycmF5W2ldIDw8ICIgIjsKICAgICAgICB9CiAgICAgICAgc3RkOjpjb3V0IDw8IHN0ZDo6ZW5kbDsKCiAgICAgICAgLy8gRGVhbGxvY2F0ZSBtZW1vcnkKICAgICAgICBkZWxldGVbXSBteUFycmF5OwogICAgfSBlbHNlIHsKICAgICAgICBjZXJyIDw8ICJBcnJheSBhbGxvY2F0aW9uIGZhaWxlZCBvciBpbnZhbGlkIHNpemUuIiA8PCBlbmRsOwogICAgfQoKICAgIHJldHVybiAwOwp9