【问题标题】:Adding an index and specified number to an array in c++在c ++中向数组添加索引和指定数字
【发布时间】:2018-03-01 05:13:39
【问题描述】:

我正在制作一个程序,从用户提供的数组的索引中删除一个数字,显示新数组,然后要求用户在他们选择的任何索引处插入一个数字。该程序的第一部分在删除索引时运行良好,但我在添加索引和数字时遇到了麻烦。例如,如果用户从索引 5 中删除数字后的 NEW 数组是:12 34 45 2 8 16 180 182 22,如果您记得数组从 0 开始,这是正确的,那么他们请求例如添加索引 5再次使用数字 78,它变得一团糟。它显示 12 34 45 2 78 8 16 180 182 22 (然后由于某种原因它还输出数字 -858993460?)所以问题基本上是它在它应该之前添加了新索引和第一索引。如果这听起来很令人困惑,我很抱歉,但我已经坚持了几个小时。谢谢!

//This program demos basic arrays

#include <iostream>
using namespace std;

const int CAP = 10;

int main()
{
    int size;
    int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
    size = 10;
    int i, delIndex, addIndex, newInt = 0;

    cout << "Your list is: " << endl;
    for (i = 0; i < CAP; i++)
    {
        cout << list[i] << endl;
    } 

//Deleting an index

cout << "\nPlease enter index to delete from: ";
cin >> delIndex;

for (i = delIndex; i <= 10; i++)
{
    list[i] = list[i + 1];

}

cout << "The index position you specified has been deleted." << endl;
cout << "The new array is: " << endl;
for (i = 0; i < (size - 1); i++)
{
    cout << list[i] << endl;
}

//Adding an index

cout << "\nNow, please enter the index position to add to: " << endl;
cin >> addIndex;
cout << "\nEnter the number to add to the index: " << endl;
cin >> newInt;

for (i = size - 1; i >= addIndex - 1; i--)
{
    list[i + 1] = list[i];
}
        list[addIndex - 1] = newInt;
        size++;

    cout << "The number has been added at the specified index position." << 
endl;
    cout << "The new array is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    }



    return 0;
}

【问题讨论】:

  • for (i = delIndex; i &lt;= 10; i++) {list[i] = list[i + 1];} -- 此循环写入超出数组范围的项目。其次,数组无法调整大小,因此“删除元素”或“添加元素”不是您想要做的。
  • 那我不知道怎么说了。如果您测试我的程序,用户可以请求索引删除一个数字。这些是我的指示,不是我编造的。
  • 它不是“工作正常”。你很幸运它有效。该数组有 10 个项目,但您访问的项目远远超出 list[9]。您正在访问索引 10 和 11,因此会造成缓冲区溢出并发生 未定义的行为
  • for (i = delIndex; i &lt;= 10; i++) 更改为for (i = delIndex; i &lt; 9; i++)
  • @Ishpreet -- for (i = delIndex; i &lt; 10; i++) -- 在最后一次迭代中仍然超出范围。

标签: c++ arrays indexing


【解决方案1】:

问题在于在您的代码中处理 size 变量。

以下是更正的代码。看到它工作here

#include <iostream>
using namespace std;

const int CAP = 10;

int main()
{
    int size;
    int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
    size = CAP;
    int i, delIndex, addIndex, newInt = 0;

    cout << "Your list is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    } 

    //Deleting an index

    cout << "\nPlease enter index to delete from: ";
    cin >> delIndex;

    for (i = delIndex; i < size; i++)
    {
        list[i] = list[i + 1];
    }
    size--;
    cout << "The index position you specified has been deleted." << endl;
    cout << "The new array is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    }

    //Adding an index

    cout << "\nNow, please enter the index position to add to: " << endl;
    cin >> addIndex;
    cout << "\nEnter the number to add to the index: " << endl;
    cin >> newInt;

    for (i = size - 1; i >= addIndex; i--)
    {
        list[i + 1] = list[i];
    }
    list[addIndex] = newInt;
    size++;

    cout << "The number has been added at the specified index position." <<endl;
    cout << "The new array is: " << endl;
    for (i = 0; i < size; i++)
    {
        cout << list[i] << endl;
    }
    return 0;
}

注意:由于我们说的是数组中的index而不是position,所以它被视为0 based and element被添加到 index addIndex,而不是 addIndex-1

【讨论】:

  • 这似乎已经修复了我的程序,但现在出现了一个非常微小的新错误,也许你可以帮助我。例如,当我将数字 675 添加到索引点 3 中时,它确实将其输入到正确的位置,但是输入数字 (675) 之后的数字“10”会重复出现并出现两次。你知道这是为什么吗?
  • @user8930130 现已更正。问题出在循环条件部分。它应该是 i &gt;= addIndex; 而不是 i &gt; addIndex;。现在更正答案。
  • 这非常有效。希望这里的每个人都和你一样乐于助人,并且和你一样重要。谢谢!
【解决方案2】:

你的程序有一些漏洞,你可以改进。

  1. 您正在声明一个常量 (CAP),但数组大小正在发生变化,因此请务必这样做。

  2. 您正在多次打印数组,最好使用一个函数,并在每次需要打印整个数组时调用它。

  3. 您在程序的某些部分使用了像 10 这样的文字。建议使用 sizeCAP 等相同的变量而不是 10 以提高可读性。

  4. 您甚至可以将插入和删除放在函数中,以便可以多次调用它们。

这是一个示例工作代码,您可以理解它。我还没有实施第 4 步,希望你能轻松完成,

LIVE CODE

工作代码

//This program demos basic arrays
#include <iostream>
using namespace std;
int CAP = 10;

void printArr(int list[]){
    for (int i = 0; i < CAP; i++)
        cout << list[i] << " ";
    cout<<endl;
}

int main()
{
    int list[CAP] = { 12, 34, 45, 2, 8, 10, 16, 180, 182, 22 };
    int i, delIndex, addIndex, newInt = 0;
    cout << "Your list is: " << endl;
    printArr(list);

    //Deleting an index
    cout << "\nPlease enter index(0 indexed based) to delete from: ";
    cin >> delIndex;
    for (i = delIndex; i < CAP - 1; i++)
        list[i] = list[i + 1];
    CAP--;
    cout << "The index position you specified has been deleted." << endl;
    cout << "The new array is: " << endl;
    printArr(list);

    //Adding an index
    cout << "\nNow, please enter the index position(0 indexed based) to add to: " << endl;
    cin >> addIndex;
    cout << "\nEnter the number to add to the index: " << endl;
    cin >> newInt;
    for (i = CAP; i > addIndex; i--)
        list[i] = list[i-1];        
    list[addIndex] = newInt;
    CAP++;
    cout << "The number has been added at the specified index position." << endl;
    cout << "The new array is: " << endl;
    printArr(list);
    return 0;
}

【讨论】:

  • 当我将此程序放入 Visual Studio 时,我收到错误消息“const int CAP = 10,表达式必须是可修改的值”。你知道这是为什么吗?
  • @Ishpreet 这只是替换索引addIndex - 1 处的元素。不添加。密切关注输出。您还可以在输出末尾看到两次22
  • @cse 是的,我错过了,更新了代码和工作链接。好收获
  • @user8930130 您不能更改 constant 的值,而是使用变量。参考this
猜你喜欢
  • 1970-01-01
  • 2010-12-29
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多