【发布时间】:2018-09-19 11:57:01
【问题描述】:
我有一个班级作业,我需要在其中创建 3 个函数来使用常量参数进行测试。我还应该创建和删除动态内存。我已经附上了作业的确切说明,以防万一,以及我的代码。
如果代码混乱,我深表歉意。我是一名初级程序员,也是该网站的新手,所以我不确定如何完美地格式化所有内容。
任务路线:
编写一个 C++ 程序来测试下面描述的三个使用指针和动态内存分配的函数。
Expand:将一个 int 数组和数组的大小作为参数。它应该创建一个两倍于参数数组大小的新数组。该函数应该将参数数组的内容复制到新数组中,并用 -1 初始化新数组中未使用的元素。该函数应该返回一个指向新数组的指针。
concatenate:将两个 int 数组和数组的大小作为参数(即 4 个参数)。它应该创建一个足够大的新数组来存储两个数组。然后它应该将第一个数组的内容复制到新数组中,然后将第二个数组的内容复制到新数组的剩余元素中,并返回一个指向新数组的指针。
subArray:它接受一个 int 数组、一个起始索引和一个长度作为参数。它创建一个新数组,该数组是原始数组中从起始索引开始的元素的副本,并且长度等于长度参数。例如,subArray(aa,5,4) 将返回一个仅包含元素 aa[5]、aa[6]、aa[7] 和 aa[8] 的新数组。
我的代码:
#include <iostream>
using namespace std;
int* Expand(int [], int);
int* concatenate(int[], int, int[], int);
int* subArray(int[], int, int);
int main()
{
//Declare variables
const int SIZEAA = 10;
const int SIZEBB = 5;
int aa[SIZEAA] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int bb[SIZEBB] = { 11, 22, 33, 44, 55 };
//Output both original arrays
cout << "aa[10]: ";
for (int i = 0; i < SIZEAA; i++)
cout << aa[i] << " ";
cout << endl;
cout << "bb[5]: ";
for (int i = 0; i < SIZEBB; i++)
cout << bb[i] << " ";
cout << endl;
//Call the Expand function
int* aaExpand = Expand(aa, SIZEAA);
//Output expanded array
cout << "Testing Expand: ";
for (int i = 0; i < 20; i++)
cout << aaExpand[i] << " ";
//Release dynamic memory
delete[] aaExpand;
aaExpand = nullptr;
cout << endl;
//Call the concatenate function
int* concatenateArray = concatenate(aa, SIZEAA, bb, SIZEBB);
//Output concatenated array
cout << "Testing concatenate: ";
for (int i = 0; i < (SIZEAA + SIZEBB); i++)
cout << concatenateArray[i] << " ";
//Release dynamic memory
delete[] concatenateArray;
concatenateArray = nullptr;
cout << endl;
//Call subArray function
int* arraySub = subArray(aa, 5, 4);
//Output the sub array
cout << "Testing subArray: ";
for (int i = 0; i < 4; i++)
cout << arraySub[i] << " ";
//Release dynamic memory
delete[] arraySub;
arraySub = nullptr;
cout << endl;
}
int* Expand(int aa[], int size) /*This function takes in an array and
the size as parameters, creates a new array of double the size, and copies
the old array into it.It then adds -1 into all new spaces created.
It returns a pointer to the new array*/
{
//Declare new array
int* aaNew;
int newSize = size * 2;
aaNew = new int[newSize];
//Copy old array into new array
for (int i = 0; i < newSize; i++)
{
if (i >= 0 && i < size) //Check to see if it needs to copy an old value in or put -1 into the array
aaNew[i] = aa[i];
else
aaNew[i] = -1;
}
return aaNew;
}
int * concatenate(int aa[], int sizeAA, int bb[], int sizeBB) /*This
function takes in two different arrays, creates a new array, then copies
both arrays into the new array.It returns a pointer to the new array*/
{
//Create new array size
int newSize = (sizeAA + sizeBB);
//Create new array
int* concatArray;
concatArray = new int[newSize];
//Add elements of first and second array into new array
for (int i = 0; i < newSize; i++)
{
if (i >= 0 && i < sizeAA) //Check to see if a value from the first or second array is supposed to be added
concatArray[i] = aa[i];
else
concatArray[i] = bb[i - sizeAA];
}
return concatArray;
}
int * subArray(int a[], int start, int length) /* This function takes in
an array, a start value, and a length value. It creates a new array and
copies the values of the original array starting at the passed start value
and continues until the new array is the length of the passed length value.
It returns a pointer to the new array*/
{
//Create new array size
int subSize = length;
//Create a new array
int* sub;
sub = new int[subSize];
//Add elements of original array starting at the passed start value into new
array until the new array is the length specified by the argument
for (int i = 0; i < subSize; i++)
{
sub[i] = a[start];
start += 1;
}
return sub;
}
【问题讨论】:
-
codereview.stackexchange.com 可能是发布此类问题的正确网站
-
知道了,谢谢你的建议!
标签: c++ dynamic-memory-allocation