使用数组的问题

时间:2018-10-23 23:21:30

标签: c++ arrays

我正在学习如何做数组,目前一直被困在这个项目中。我已经附上了我所拥有的,但是由于程序继续重复而没有结束我需要的答案,因此无法弄清楚我要去哪里。

应该允许用户输入20个数字,然后显示每个数字及其与输入数字的平均值的差。作业的第2部分是对其进行修改,以便用户可以输入最多20个数字,直到输入前哨值为止。

#include <iostream>
#include <string>

using namespace std;

int main()

{
    const int SIZE = 12;
    int numbers[12] = { 0 };
    int value = 0;
    int counter = 0;
    int total = 0;
    int average = 0;
    int diffFromAvg = 0;
    int SENTINEL = -1;

    cout << "Please enter a positive number: " << endl;
    cin >> value;

    while ((counter < SIZE) && (value != SENTINEL))
    {
        total = total + value;
        numbers[counter] = value;
        counter = counter + 1;

        if (counter != SIZE) {
            cout << "Please enter a positive number: " << endl;
            cin >> value;
        }
        if (counter > 0) {
            average = total / counter;
            for (int i = 0; i <= SIZE; i--)
            {
                diffFromAvg = numbers[i] - average;
                cout << "Number[i]: " << numbers[i] << " Difference from Average is " <<
                    diffFromAvg << endl;
            }
        }
        else {
            cout << "Processing incomplete. No values in the array." << endl;
        }
    }

    system("Pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

我建议您遵循IPO model

#include <cstdlib>
#include <iostream>
#include <iomanip>

int main()
{
    constexpr std::size_t max_numbers{ 20 };
    constexpr int sentinel{ -1 };

    int numbers[max_numbers];
    std::size_t num_count{};

    int sum{};

    // input:
    for (int current_value;
         num_count < max_numbers && std::cout << "Please enter a positive number: " &&
         (std::cin >> current_value) && current_value != sentinel;
         ++num_count)
    {
        numbers[num_count] = current_value;
        sum += current_value;
    }

    // proccess:
    if (!num_count) {
        std::cerr << "No input.\n\n";
        return EXIT_FAILURE;
    }

    auto mean{ sum / static_cast<double>(num_count) };

    // output:
    std::cout.put('\n');
    for (std::size_t i{}; i < num_count; ++i)
    {
        auto distance_to_mean{ numbers[i] - mean };
        std::cout << "Number " << std::setw(2) << i + 1 << ": " << std::setw(5) << numbers[i]
                  << '\t' << std::setw(5) << std::fixed << distance_to_mean << '\n';
    }
    std::cout.put('\n');
}

答案 1 :(得分:0)

另一个答案也同样好。我保留了while()循环,但进行了一些更改。将您的处理过程分开一些是明智的。可以将其分解为较小的功能

   #include <iostream>
#include <string>

using namespace std;

int main() {
    const int SIZE = 12;
    int numbers[12];
    int value = 0;
    int index;
    int SENTINEL = -1;

    index = 0;  // initialize here so you keep track of it
    while ((index < SIZE) && (value != SENTINEL)) {

        cout << "Please enter a positive number: " << endl;
        cin >> value;

        numbers[index] = value;

        index++; // increment index after assigning the array value
    }

    // Calculate the average
    int total = 0;
    for (int i = 0; i < SIZE; i++) {
        total += numbers[i];
    };
    int average = total / SIZE;

    cout << "Average is: " << average << endl;
    for (int i = 0; i < SIZE; i++) {
        int diffFromAvg = numbers[i] - average;
        cout << "Number[i]: " << numbers[i]
             << " Difference from Average is "
             << diffFromAvg << endl;
    }

    system("Pause");
    return 0;
}