c++ - 在C++中向数组添加索引和指定数字

标签 c++ arrays indexing

我正在制作一个程序,从用户从数组中提供的索引中删除一个数字,显示新数组,然后要求用户在他们选择的任何索引处插入一个数字。该程序的第一部分在删除索引时工作正常,但我在添加索引和数字时遇到了问题。例如,如果用户从索引 5 中删除数字后的新数组是: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;
}

最佳答案

问题在于处理 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;
}

注意:因为我们讨论的是数组中的索引而不是位置,所以它被视为0基于和元素添加到 index addIndex , 不在 addIndex-1 .

关于c++ - 在C++中向数组添加索引和指定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49043020/

相关文章:

c++ - 代码已经进入curl_easy_perform()时如何中止网络请求

javascript - 如何在给定键数组的情况下创建嵌套对象

assembly - NASM - 如何将 8 位寄存器移动到完整的 32 位寄存器?

c++ - 名为 type 的全局变量隐藏了 Lua 中的内置 type() 函数

c++ - Irrlicht - 创建 3D 平面/立方体网格

c - 段错误错误,将元素分配给字符数组

python - Pytorch 张量索引

mysql - 如何正确预热 MySQL FULLTEXT 索引?

c++ - 是否有包含目录的 pragma 指令?

ios - 如何遍历 Swift 数组并在 iOS 图表中使用数据?