使用float的动态数组

Dynamic arrays using float

本文关键字:数组 动态 float 使用      更新时间:2023-10-16

我有一个小任务需要完成,我很困惑。这项任务有三个部分,分别是:

  1. 编写一个程序,动态分配一个用户指定大小的浮点数组(目前正在处理),如果有人能检查我的代码,那将不胜感激

  2. 然后,它应该允许用户输入浮点数,这些浮点数应该存储在数组中。(我不知道这意味着什么,所以如果有人能解释的话,如果我愿意的话)

  3. 程序应该打印保存到数组中的内容、总和和数组中的平均值,然后退出。

正如你所知,我是C++和一般编码的新手,所以请尽可能地为我拼写出来。我必须使用指针,所以恐怕我无法改变这一点。

    #include <iostream>
using namespace std;
int main()
{
int length;
cout << “Please enter the length of the array: “;
cin >> length;
float * dArray = new float [length];
for (int i = 0; i < length; i++)
{
cin >> dArray[i] = i;
for (int i = 0; i < length; i++)
{
cout << dArray[i] << “ “;
}
cout << ‘/n’;
int sum = 0;
for (int i=0; i < length; i++)
{
sum +=dArray[i];
avg =sum/length;
cout << “Sum is “ << sum << “/nAverage is “ << average;
delete [] dArray;
}
return 0;
}

请解释第二部分。

提前谢谢。

关于

然后,它应该允许用户输入浮点数,这些浮点数应该存储在数组中。(我不知道这意味着什么,所以如果有人能解释的话,如果我愿意的话)

这意味着您必须让用户向该数组输入值。你所做的就是给他们价值观。

你需要做的是改变

    for (int i = 0; i < length; i++)
    {
        dArray[i] = i;
    }

    for (int i = 0; i < length; i++)
    {
        cin>>dArray[i];
    }

还要注意,length应该是int,而不是float。

完成后,这可能是你需要的代码(尽管我建议你自己找到总和和平均值的部分,并使用我发布的这个代码作为参考来检查是否有任何错误,因为找到总数和平均值真的很容易)

#include <iostream> // include library
using namespace std;
   int main() // main function
      {
        int length;           // changed length to int
        float sum = 0 , avg;  // variables to store sum and average
        cout << "Please enter the length of the array: "; // ask user for array
        cin >> length;
        float *dArray = new float[length];
        cout << "nEnter " << length << " values to be added to the arrayn";
        for (int i = 0; i < length; i++)
          {
            cin >> dArray[i];  //accepting values
            sum += dArray[i];   // finding sum
          }
        avg = sum / length;  //the average
        cout << "nThe array now containsn";    // Displaying the array
        for (   int i = 0;  i < length; i++)     // with the loop
          {
            cout << dArray[i] << " ";
          }
        cout << "nThe sum of all values in the array is " << sum;  // the sum
        cout << "nnThe average value is " << avg;                 // the average
        delete[] dArray;
        return 0;
      } 

编辑

收到你的评论后,我决定发布这个新代码。(我假设你的意思是,只要用户愿意,程序就应该重复)我使用了一个do-white循环来完成它。

#include <iostream> // include library
using namespace std;
   int main() // main function
      {
        int length;           // changed length to int
        char a;               // a variable to store the user choice 
        do
          {
            float sum = 0 , avg;  // variables to store sum and average        
            cout << "nPlease enter the length of the array: "; // ask user for array
            cin >> length;
            float *dArray = new float[length];
            cout << "nEnter " << length << " values to be added to the arrayn";
            for ( int i = 0; i < length; i++ ) 
              {
                cin >> dArray[i];  //accepting values
                sum += dArray[i];   // finding sum
              }
            avg = sum / length;  //the average
            cout << "nThe array now containsn";    // Displaying the array
            for (   int i = 0;  i < length; i++ )    // with the loop
              {
                cout << dArray[i] << " ";
              }
            cout << "nThe sum of all values in the array is " << sum;  // the sum
            cout << "nnThe average value is " << avg;                 // the average
            
            cout << "nnDo you want to try again ( y/n ) ?n";
            cin >> a;
          
            delete[] dArray;
          }while( a =='Y' || a == 'y'  );  // The do while loop repeats as long as the character entered is Y or y
        
        return 0;
      }    

好吧,希望这是你想要的,如果不是,请通知我并发表评论…:)


只是为了让你知道,你发布的新代码甚至没有编译。以下是一些问题。

cin >> dArray[i] = i;

您不需要在此处使用 = i。仅仅cin >> dArray[i] ;就足够了。

下一个问题是

cout << ‘/n’;

首先,它是n而不是/n。您还需要将它括在双引号中,而不是单引号中。那是cout << "n";

接下来,您还没有定义变量avg。还要注意,您还使用了一个未定义的变量average,我想您的意思是avg

现在有一个主要问题,你没有关闭你打开的花括号。你打开了for循环的括号,但忘记了关闭它。我把这部分留给你,因为你需要通过尝试自己学习这部分。

现在有一个问题我不明白,您使用了“ “,它在某种程度上与" "不同。我不知道是我的电脑出了问题,还是一个完全不同的符号。我的编译器无法识别它。如果它没有给你端带来任何麻烦,那就不要介意。

好吧,这总结了我在你的代码中注意到的问题(我注意到的那些问题)。

您的问题太简单了,我们不能只给您答案,但我已经对您的代码发表了关于如何解决问题的建议:

#include <iostream>
using namespace std;
int main()
{
  float length; //it doesn't make sense for something to be of a float length
                //should be size_t instead
  cout << "Please enter the length of the array: ";
  cin >> length;
  float *dArray = new float[length];
  for (int i = 0; i < length; i++)
  {
    dArray[i] = i; //this line is incorrect
    //how should we read the data into this array?
    //we've used cin before
  }
  for (int i = 0;  i < length; i++)
  {
    cout << dArray[i] << " ";
  }
  cout << 'n';
  //now we've output the array, just need to output the sum and average value
  int sum = 0;
  for (int i=0; i < length; i++)
  {
     sum += //what should go here?
  }
  int average = //how should we calculate the average?
  cout << "Sum is " << sum << "nAverage is " << average;
  delete[] dArray;
  return 0;  
}