我們有一個項目,要求我們編寫一個程序,允許用戶輸入一系列數字「將數字讀入數組中進行進一步處理,用戶信號通過輸入負數來完成(在計算中不使用負數),在讀完所有數字後,執行以下操作,總結#輸入的數字,計算輸入的數字,找到輸入的最小/最大值,計算平均值,然後將它們輸出到屏幕上所以在這種工作,我做的樣子版本,所以C++數組複製/移位
/* Reads data into array.
paramater a = the array to fill
paramater a_capacity = maximum size
paramater a_size = filled with size of a after reading input. */
void read_data(double a[], int a_capacity, int& a_size)
{
a_size = 0;
bool computation = true;
while (computation)
{
double x;
cin >> x;
if (x < 0)
computation = false;
else if (a_size == a_capacity)
{
cout << "Extra data ignored\n";
computation = false;
}
else
{
a[a_size] = x;
a_size++;
}
}
}
/* computes the maximum value in array
paramater a = the array
Paramater a_size = the number of values in a */
double largest_value(const double a[], int a_size)
{
if(a_size < 0)
return 0;
double maximum = a[0];
for(int i = 1; i < a_size; i++)
if (a[i] > maximum)
maximum = a[i];
return maximum;
}
/* computes the minimum value in array */
double smallest_value(const double a[], int a_size)
{
if(a_size < 0)
return 0;
double minimum = a[0];
for(int i = 1; i < a_size; i++)
if (a[i] < minimum)
minimum = a[i];
return minimum;
}
//computes the sum of the numbers entered
double sum_value(const double a [], int a_size)
{
if (a_size < 0)
return 0;
double sum = 0;
for(int i = 0; i < a_size; i++)
sum = sum + a[i];
return sum;
}
//keeps running count of numbers entered
double count_value(const double a[], int a_size)
{
if (a_size < 0)
return 0;
int count = 0;
for(int i = 1; i <= a_size; i++)
count = i;
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
const int INPUT_CAPACITY = 100;
double user_input[INPUT_CAPACITY];
int input_size = 0;
double average = 0;
cout << "Enter numbers. Input negative to quit.:\n";
read_data(user_input, INPUT_CAPACITY, input_size);
double max_output = largest_value(user_input, input_size);
cout << "The maximum value entered was " << max_output << "\n";
double min_output = smallest_value(user_input, input_size);
cout << "The lowest value entered was " << min_output << "\n";
double sum_output = sum_value(user_input, input_size);
cout << "The sum of the value's entered is " << sum_output << "\n";
double count_output = count_value(user_input, input_size);
cout << "You entered " << count_output << " numbers." << "\n";
cout << "The average of your numbers is " << sum_output/count_output << "\n";
string str;
getline(cin,str);
getline(cin,str);
return 0;
}
這都很好,我有現在的問題是部分二,我們要「數組複製到另一個和N檔的數組元素「,我不知道從哪一個開始,我擡起頭來關於複製數組的一些資源,但我不確定如何在我已完成的當前代碼中實現它們,特別是當涉及到轉換時。如果任何人有任何想法,想法或資源,可以幫助我在正確的道路上,將不勝感激。我也應該指出,我是一個初學者(這是一個初學者課程),所以這項任務可能不是「最佳」的方式,但是如果有意義的話,我們可以將它們合併到一起。
C++有一個[組漂亮的algorightms的]替換原始陣列(http://en.cppreference.com/w/cpp/algorithm/rotate_copy),其你可以使用,例如['std :: rotate_copy'](http://en.cppreference.com/w/cpp/algorithm/rotate_copy),這似乎是你想要的。 – 2013-04-29 12:02:04