【发布时间】:2017-04-24 01:08:55
【问题描述】:
我正在尝试解决这个问题。这是一个项目,我们的讲师需要这个标题。我让检查功能正常工作,但是在添加到数组时添加我们必须使用指针。我的理解是我们应该将这个数组复制到另一个数组并替换指针。例如 Array1 {1,2,3} 然后将其复制到 Array2 {1,2,3,4} 然后添加 4 以扩展数组。不幸的是,我发现的所有东西都在研究.. 向量和其他函数更适合这项任务,但我们只需要使用指针和大小来调整和添加元素。
// returns the index of the element in "arrayPtr" of "size"
// that corresponds to the element holding "number"
// if number is not in the array, returns -1
int check(int *arrayPtr, int number, int size);
// adds "number" to the array pointed to by "arrayPtr" of "size".
// if the number is not already there, if "number" is there - no action
// Note, the size of the array is thus increased.
void addNumber(int *& arrayPtr, int number, int &size);
// removes a "number" from the "arrayPtr" of "size".
// if "number" is not there -- no action
// note, "size" changes
void removeNumber(int *& arrayPtr, int number, int &size);
到目前为止我有这个:
// returns the index of the element in "arrayPtr" of "size"
// that corresponds to the element holding "number"
// if number is not in the array, returns -1
int check(int *arrayPtr, int number, int size) {
for (int i = 0; i < size; i++) {
if (arrayPtr[i] == number) {
return i;
}
}
return -1;
}
// adds "number" to the array pointed to by "arrayPtr" of "size".
// if the number is not already there, if "number" is there - no action
// Note, the size of the array is thus increased.
void addNumber(int *& arrayPtr, int number, int &size) {
if (check(arrayPtr, number, size)==-1) {
//add the element to the end of the array
}
//did not run if -1
}
// removes a "number" from the "arrayPtr" of "size".
// if "number" is not there -- no action
// note, "size" changes
void removeNumber(int *& arrayPtr, int number, int &size) {
}
任何关于如何进行的提示或提示或建议将不胜感激!
【问题讨论】: