我正在使用Visual Studio 2013,C++,控制台应用程序。我现在一直在努力解决一个问题。我想知道是否有方法来初始化一个数组,例如用户输入,例如:用用户输入初始化一个数组
我有一个数组:int arr[] = { 3, 7, 5, 9, 1};
。因此,我想要初始化的值是一个用户输入。
有没有办法做到这一点?所有的帮助和意见将不胜感激。
这是我的代码: cout < <“输入数组元素的数量:”; cin >>元素;
cout << "Enter the difference value: ";
cin >> difference;
cout << "Enter the sequence of elements: ";
vector<int> arr(elements);
for (int i = 0; i < elements; i++)
{
cin >> arr[i];
}
//the following needs to have an array
//in their respective functions.
sorter(arr[], elements);
elementsDifference(arr[], elements, difference);
该程序必须遍历一个数组,并找到具有给定差异的对。
但是,在我的问题,数组的大小由用户依赖于输入例如,输入元素的大小:然后用户输入元素的数量,那么这将如何工作? @Arun A.S – 2015-04-03 08:12:50
@PrathamPatel,为此添加了代码。 – 2015-04-03 08:18:38