この問題を解決しようとしています。これはプロジェクトのためのものであり、講師はこのヘッダーを必要とします。私はチェック関数が正しく動作しているが、配列に追加するときにポインタを使う必要があります。私の理解は、この配列を別の配列にコピーしてポインタを置き換えることです。たとえば、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) {
}
任意のヒントやヒントやアドバイスをいただければ幸いです!