在c ++中向数组添加索引和指定的数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在c ++中向数组添加索引和指定的数字相关的知识,希望对你有一定的参考价值。
我正在创建一个程序,从用户从数组中提供的索引中删除一个数字,显示新数组,然后要求用户在他们选择的任何索引处插入一个数字。在删除索引时,此程序的第一部分工作正常,但我在添加索引和数字时遇到问题。例如,如果用户从索引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 << "
Please 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 << "
Now, please enter the index position to add to: " << endl;
cin >> addIndex;
cout << "
Enter 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 << "
Please 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 << "
Now, please enter the index position to add to: " << endl;
cin >> addIndex;
cout << "
Enter 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
处添加,而不是在addIndex-1
。
另一答案
你的程序中有一些漏洞,你可以改进。
- 您正在声明一个常量(CAP),但数组大小正在发生变化,所以请点这样做。
- 您正在多次打印数组,最好使用一个函数,并在每次需要打印整个数组时调用它。
- 你在程序的某些部分使用像
10
这样的文字。建议使用相同的变量,如size
或CAP
而不是10
来提高可读性。 - 您甚至可以在函数中插入和删除它们,以便可以多次调用它们。
这是一个示例工作代码,您可以理解这一点。我没有实施第4步,我希望你能轻易做到,
工作守则
//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 << "
Please 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 << "
Now, please enter the index position(0 indexed based) to add to: " << endl;
cin >> addIndex;
cout << "
Enter 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;
}
以上是关于在c ++中向数组添加索引和指定的数字的主要内容,如果未能解决你的问题,请参考以下文章