我正在使用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