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

时间:2018-03-01 05:13:39

标签: c++ arrays indexing

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

2 个答案:

答案 0 :(得分:1)

您的计划存在一些漏洞,您可以改进。

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

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

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

  4. 你甚至可以插入&amp;删除函数,以便可以多次调用它们。

  5. 这是一个示例工作代码,您可以理解这一点。我没有实施第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;
    }
    

答案 1 :(得分:1)

问题在于在代码中处理bar.Things.Item(1)变量。

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

size

注意:由于我们在讨论数组中的 index 而非位置,因此它被视为基于#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,而不是addIndex