0
我试图找出这个问题。这是一个项目,我们的教练需要这个标题。我得到了检查函数正常工作,但添加到数组时,我们必须使用一个指针。我的理解是我们应该将这个数组复制到另一个数组并替换指针。例如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) {
}
如何进行将不胜感激任何提示或提示或建议!