当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Dynamic arrays using float

问题描述:

I've got a small task I need to complete and I'm rather confused. This task has 3 parts to it which are:

  1. Write a program that dynamically allocates a float array of a size specified by a user (currently working on - if anyone could check my code for this it would be appreciated.

  2. It should then allow the user to input that number of floats, which should be stored in the array. (I have no clue what this means so if I'd appreciate someone explaining it if they could)

  3. Program should print what was saved into the array, the sum, and the average value in the array, and exit.

As you could tell I'm new to C++ and coding in general so please spell it out for me wherever possible. It is mandatory that I am using pointers so I'm afraid I can't change that.

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

}

Please explain the 2nd part.

Thanks in advance.

网友答案:

Regarding

It should then allow the user to input that number of floats, which should be stored in the array. (I have no clue what this means so if I'd appreciate someone explaining it if they could)

It means that you have to let the user input the values to that array. What you are doing is giving them values yourself.

What you need to do is change

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

to

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

Also Note that length should be an int and not a float.

After completion, this would probably be the code you need ( although I would advice you to do the part of finding the sum and average by yourself and use this code I have posted as reference to check for any mistake, as finding the sum and average for this is really easy )

#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 array\n";
        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 contains\n";    // 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 << "\n\nThe average value is " << avg;                 // the average

        delete[] dArray;
        return 0;
      } 

EDIT

After getting your comment, I decided to post this new code. ( I am assuming what you meant is that the program should repeat as long as the user wants ) I have done it by using a do while loop.

#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 array\n";
            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 contains\n";    // 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 << "\n\nThe average value is " << avg;                 // the average

            cout << "\n\nDo 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;
      }    

Well, hope this is what you were looking for, if not, please do notify me with a comment... :)


Just so you know, the new code you have posted doesn't even compile. Here are some of the problems.

cin >> dArray[i] = i;

You don't need to use = i here. Just cin >> dArray[i] ; is enough.

The next problem is

cout << ‘/n’;

First of all, its \n and not /n. You also need to enclose it in double quotes and not single quotes. That is cout << "\n";

Next one, you have not defined the variable avg . Also note that you have also used an undefined variable average, which I assume you meant avg.

Now here's one of the main problems , You have not closed the curly brackets you opened. You open the brackets for for loops, but forget to close it. I'm leaving that part to you as you need to learn that part yourself by trying.

Now Here's one problem I don't understand, you have used “ “, which is somehow not the same as " ". I don't know if it's something wrong with my computer, or if it's a totally different symbol. My compiler couldn't recognize it. If its not causing any trouble on your end, then don't mind it.

Well, this sums up the problems I noticed in your code ( the problems that I noticed ).

网友答案:

Your issues are too simple for us to just give you the answers, but I've commented your code with suggestions on how to solve your problem:

#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;  
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: