【问题标题】:C++ Array loop for average平均的 C++ 数组循环
【发布时间】:2013-08-30 09:36:52
【问题描述】:

我目前正在尝试使用 C++。到目前为止,我已经学会了制作一个程序,该程序允许我输入 10 个变量以及 5 个已经赋值的变量,然后找到这些数字的平均值。但是我无法弄清楚如何做到这一点,我为数组创建了一个 for 循环,但似乎没有找到答案平均值。以下代码有什么问题?这是我目前所拥有的:

#include <iostream>

using namespace std;

int cole[10];
int sum = 0;
int main()
{
    int a = 10;
    int b = 10;
    int c = 10;
    int d = 10;
    int e = 35;
    cout << "Please input ten numbers, one at a time" << endl;
    cin >> cole[0];
    cin >> cole[1];
    cin >> cole[2];
    cin >> cole[3];
    cin >> cole[4];
    cin >> cole[5];
    cin >> cole[6];
    cin >> cole[7];
    cin >> cole[8];
    cin >> cole[9];
    cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;

    for(int x = 0; x < 10; x++ )
    {
        int sum = 0;
        cout << cole[x] << " ";
        sum += cole[x];
        cole[x]++;
    }
    sum += cole[0];
    cole[0]++;
    cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
    cout << "The average of these numbers is: ";
    sum = sum + a + b + c + d + e;
    cout << sum / 15;

}

提前致谢

【问题讨论】:

  • int sum = 0;移出循环。
  • 这段代码的作用:sum += cole[0]; cole[0]++;?
  • 数组项求和后为什么要增加?
  • 另外你包括cole[0]两次。
  • 请开始使用调试器:)

标签: c++ arrays loops average


【解决方案1】:

请试试这个代码:IDEONE

#include <iostream>

using namespace std;

int cole[10];
int sum = 0;

int main()
{
    int a = 10;
    int b = 10;
    int c = 10;
    int d = 10;
    int e = 35;
    double avg = 0;
    cout << "Please input ten numbers, one at a time" << endl;
    for (int i=0;i<10;i++) {
        cin >> cole[i];
    }
    cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;

    for(int x = 0; x < 10; x++ )
    {
        cout << cole[x] << " ";
        sum += cole[x];
    }
    cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
    cout << "The average of these numbers is: ";
    sum = sum + a + b + c + d + e;
    avg = sum / 15.0;
    cout << avg;

}

请记住,平均值不应是整数值 - 它会将数字四舍五入。请记住,您也可以在循环中将每个值输入到数组中,不要一个一个地输入。您还在循环中一次又一次地声明“int sum = 0”,因此全局总和在循环内不可见。我也删除了一些不需要的代码。你可以看看,干杯

【讨论】:

    【解决方案2】:
        #include <iostream>
    
    using namespace std;
    
    int coleCount = 10;
    int cole[coleCount];
    int sum = 0;
    int main()
    {
        int a = 10;
        int b = 10;
        int c = 10;
        int d = 10;
        int e = 35;
    
        cout << "Please input ten numbers, one at a time" << endl;
    
        for(int i = 0; i < coleCount; i++)
        {
          cin >> cole[i];  
        }
    
        cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
    
        for(int i = 0; i < coleCount; i++ )
        {
            cout << cole[i] << " ";
            sum += cole[i];
            //cole[x]++; why are you incrementing this?
        }
        //sum += cole[0]; why?
        //cole[0]++; why?
        cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
        cout << "The average of these numbers is: ";
        sum += (a + b + c + d + e);
        cout << sum / 15;
    
    }
    

    【讨论】:

      【解决方案3】:

      摆脱循环内的int sum = 0cole[x]++;。也这么输:

      sum += cole[0];
      cole[0]++;
      

      在循环之后。

      【讨论】:

        【解决方案4】:
        int sum = 0;
        

        这需要在循环之外,你不断地重置总和。

        【讨论】:

          【解决方案5】:

          您声明了两次sum。从 for 循环中删除声明!

          for(int x = 0; x < 10; x++ )
          {        
              cout << cole[x] << " ";
              sum += cole[x];
              cole[x]++;
          }
          

          【讨论】:

            【解决方案6】:

            固定代码:

            #include <iostream>
            
            using namespace std;
            
            int cole[10];
            int sum = 0;
            int main()
            {
                int a = 10;
                int b = 10;
                int c = 10;
                int d = 10;
                int e = 35;
                cout << "Please input ten numbers, one at a time" << endl;
                cin >> cole[0];
                cin >> cole[1];
                cin >> cole[2];
                cin >> cole[3];
                cin >> cole[4];
                cin >> cole[5];
                cin >> cole[6];
                cin >> cole[7];
                cin >> cole[8];
                cin >> cole[9];
                cout << "There will now be 5 assigned variables, so that we have 15 variables" << endl;
            
            
                int sum = 0;  //initialize sum outside the for loop
                for(int x = 0; x < 10; x++ )
                {
                    cout << cole[x] << " ";
                    sum += cole[x];
                    cole[x]++;
                }
                //sum += cole[0];  //this seems unnecessary
                //cole[0]++;
                cout << "and " << a << " " << b << " " << c << " " << d << " " << e << endl;
                cout << "The average of these numbers is: ";
                sum = sum + a + b + c + d + e;
                cout << sum / 15;
            
            }
            

            【讨论】:

            • 还有sum的双重声明
            猜你喜欢
            • 2011-03-07
            • 2010-09-07
            • 2023-01-09
            • 1970-01-01
            • 2019-04-19
            • 1970-01-01
            • 1970-01-01
            • 2021-02-12
            • 1970-01-01
            相关资源
            最近更新 更多