2013-04-29 41 views
1

我們有一個項目,要求我們編寫一個程序,允許用戶輸入一系列數字「將數字讀入數組中進行進一步處理,用戶信號通過輸入負數來完成(在計算中不使用負數),在讀完所有數字後,執行以下操作,總結#輸入的數字,計算輸入的數字,找到輸入的最小/最大值,計算平均值,然後將它們輸出到屏幕上所以在這種工作,我做的樣子版本,所以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檔的數組元素「,我不知道從哪一個開始,我擡起頭來關於複製數組的一些資源,但我不確定如何在我已完成的當前代碼中實現它們,特別是當涉及到轉換時。如果任何人有任何想法,想法或資源,可以幫助我在正確的道路上,將不勝感激。我也應該指出,我是一個初學者(這是一個初學者課程),所以這項任務可能不是「最佳」的方式,但是如果有意義的話,我們可以將它們合併到一起。

+2

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

回答

1
for(int i = 0; i < n; ++i){ 
    int j = (i - k)%n; 
    b[i] = a[j]; 
} 

檢查它。我不知道 如果這個工程,你就能把提高

for(int i = 0; i < n; ++i) 
    b[i] = a[(i - k)%n];//here can be (i +/- k) it depends which direction u would shift 
+0

謝謝,這對我和作品來說非常有意義。這是把我扔掉的轉變,但事實證明這比我想象的要簡單得多。 – user2331891 2013-04-30 19:03:54

0

如果你只想陣列複製到另一個陣列和轉移他們

例如:輸入= 1,2,3,4 ,5;輸出= 3,4,5,1,2

的麻煩的解決方案是

//no template or unsafe void* since you are a beginner 

int* copy_to(int *begin, int *end, int *result) 
{ 
    while(begin != end){ 
    *result = *begin; 
    ++result; ++begin; 
    } 

    return result; 
} 

int main() 
{ 
    int input[] = {1, 2, 3, 4, 5}; 
    size_t const size = sizeof(input)/sizeof(int); 
    size_t const begin = 2; 
    int output[size] = {0}; //0, 0, 0, 0, 0 

    int *result = copy_to(input + begin, input + size - begin, output); //3, 4, 5, 0, 0 
    copy_to(input, input + begin, result); //3, 4, 5, 1, 2 

    return 0; 
} 

如何可以在STL算法設定幫助我們?

的read_data保持你提供

#include <algorithm> //std::minmax_element, std::rotate_copy 
#include <iostream> 
#include <iterator> //for std::begin() 
#include <numeric> //for std::accumulate() 
#include <string> 
#include <vector> 

int main(int argc, char *argv[]) //don't use _tmain, they are unportable 
{ 

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); 

auto const min_max = std::minmax_element (user_input, user_input + input_size); //only valid for c++11 

std::cout << "The maximum value entered was " << min_max.second << "\n"; 
std::cout << "The lowest value entered was " << min_max.first << "\n"; 

double sum_output = std::accumulate(user_input, user_input + input_size, 0); 
cout << "The sum of the value's entered is " << sum_output << "\n"; 

//I don't know the meaning of you count_value, why don't just output input_size? 
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"; 

int shift; 
std::cout<<"How many positions do you want to shift?"<<std::endl; 
std::cin>>shift; 
std::vector<int> shift_array(input_size); 
std::rotate_copy(user_input, user_input + shift, user_input + input_size, std::begin(shift_array)); 


//don't know what are they for? 
std::string str; 

std::getline(std::cin,str); 
std::getline(std::cin,str); 

return 0; 
} 

如果你的編譯器不支持C++ 11個功能尚未

的std :: minmax_element可以通過 的std :: min_element和std更換同一個: :max_element 的std ::開始()可以通過shift_array.begin()

我不知道你是什麼類的教學風格,在我的愚見更換,初學者應該 啓動與C++提供的更高級別的組件,如向量,字符串,算法 等等。我假設你的老師正在教你這種方式,你可以使用 算法和容器與c + +(讓我們乞求你的課是沒有教你「上課」,並說「OOP是世界上最好的東西」)。

PS:你可以使用載體,如果你喜歡

+0

隨着你的轉變(與std算法設置)我將如何去輸出移位的元素?例如,如果用戶輸入1,2,3,4,5並選擇1的移位,我想在移位後輸出新的數字。我也擺脫了count_value,並使用input_size感謝捕獲。我的代碼最後的getline是他教我們如何在調試時保持屏幕打開以查看結果。 – user2331891 2013-04-30 10:19:44