1

ユーザーが一連の数字を入力できるようにするプログラムを作成するように依頼されたプロジェクトがありました。「さらに処理するために数字を配列に読み込み、ユーザーは負の数を入力して終了したことを通知します(負は計算に使用されません)。すべての数値が読み込まれた後、以下を実行し、入力された # を合計し、入力された # を数え、入力された最小/最大 # を見つけ、平均を計算し、それらを画面に出力します。私はそう見えるようにしました

/* 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;
}

それはうまくいきました。私が今抱えている問題はパート2です。「配列を別の配列にコピーし、配列をN個の要素だけシフトする」ところです。これらのいずれについても、どこから始めればよいかわかりません。配列のコピーに関するいくつかのリソースを調べましたが、特にシフトに関しては、完成した現在のコードにそれらを実装する方法がわかりませんでした。誰かが正しい道で私を助けることができる考え、アイデア、またはリソースを持っているなら、それは大歓迎です. また、私は初心者なので(これは初心者クラスです)、この課題は「最適な」方法ではないかもしれませんが、それが理にかなっている場合は、学んだことを取り入れています。

4

2 に答える 2

2
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
于 2013-04-29T12:04:22.857 に答える