insertElement() 函数没有按预期工作
insertElement() function doesn't work as intended
我的程序中的 insertElement() 函数有问题。我打算 insertElement 做的是从原型中获取索引并将所有值向右移动,包括该索引上的值,一次。所以,如果我有我的数组 {1, 2, 3, 4} 并且我想在索引“2”处插入值“10”,则结果数组将是 {1, 2, 10, 3, 4 }.
我知道我必须调整我的 insertElement() 函数来解决这个问题,但我不确定从哪里开始,有人可以帮助我吗?这是我的代码:
#include <iostream>
using namespace std;
const int CAPACITY = 20;
void displayArray(int array[], int numElements)
{
for (int i = 0; i < numElements; i++)
cout << array[i] << " ";
cout << endl;
}
bool insertElement(int array[], int& numberElements, int insertPosition, int insertTarget)
{
int p = 0;
int j = 1;
int arrayPositionFromLast = (numberElements-1);
if (numberElements>=CAPACITY)
{
cout << "Cannot insert an element, array is full." << endl;
return false;
} else {
for (int i=arrayPositionFromLast; i>insertPosition; i--)
{
array[arrayPositionFromLast-p]=array[arrayPositionFromLast-j];
p++;
j++;
}
array[insertPosition] = insertTarget;
}
return true;
}
int main()
{
int array[6] = {1, 2, 3, 4, 5, 6};
int numArrayElements = 6;
int endOfArrayValue, insertedValue, insertedValuePosition;
cout << "Enter a value and a position to insert: ";
cin >> insertedValue >> insertedValuePosition;
insertElement(array, numArrayElements, insertedValuePosition, insertedValue);
displayArray(array, numArrayElements);
}
首先你应该用 CAPACITY
定义你的数组
int array[CAPACITY] = {1, 2, 3, 4, 5, 6};
您可以使用 memmove 移动数据。
if (numberElements>=CAPACITY)
{
cout << "Cannot insert an element, array is full." << endl;
return false;
} else {
memmove(array + insertPosition+ 1, array + insertPosition, (numberElements - insertPosition) * sizeof (int));
array[insertPosition] = insertTarget;
}
我的程序中的 insertElement() 函数有问题。我打算 insertElement 做的是从原型中获取索引并将所有值向右移动,包括该索引上的值,一次。所以,如果我有我的数组 {1, 2, 3, 4} 并且我想在索引“2”处插入值“10”,则结果数组将是 {1, 2, 10, 3, 4 }.
我知道我必须调整我的 insertElement() 函数来解决这个问题,但我不确定从哪里开始,有人可以帮助我吗?这是我的代码:
#include <iostream>
using namespace std;
const int CAPACITY = 20;
void displayArray(int array[], int numElements)
{
for (int i = 0; i < numElements; i++)
cout << array[i] << " ";
cout << endl;
}
bool insertElement(int array[], int& numberElements, int insertPosition, int insertTarget)
{
int p = 0;
int j = 1;
int arrayPositionFromLast = (numberElements-1);
if (numberElements>=CAPACITY)
{
cout << "Cannot insert an element, array is full." << endl;
return false;
} else {
for (int i=arrayPositionFromLast; i>insertPosition; i--)
{
array[arrayPositionFromLast-p]=array[arrayPositionFromLast-j];
p++;
j++;
}
array[insertPosition] = insertTarget;
}
return true;
}
int main()
{
int array[6] = {1, 2, 3, 4, 5, 6};
int numArrayElements = 6;
int endOfArrayValue, insertedValue, insertedValuePosition;
cout << "Enter a value and a position to insert: ";
cin >> insertedValue >> insertedValuePosition;
insertElement(array, numArrayElements, insertedValuePosition, insertedValue);
displayArray(array, numArrayElements);
}
首先你应该用 CAPACITY
定义你的数组int array[CAPACITY] = {1, 2, 3, 4, 5, 6};
您可以使用 memmove 移动数据。
if (numberElements>=CAPACITY)
{
cout << "Cannot insert an element, array is full." << endl;
return false;
} else {
memmove(array + insertPosition+ 1, array + insertPosition, (numberElements - insertPosition) * sizeof (int));
array[insertPosition] = insertTarget;
}