Lucky
Lucky

Reputation: 45

How to send a range of calculated values into an array?

In my program I am suppose to ask the user to input a range of x values, minimum and maximum x. I am suppose to have 20 numbers calculated within those two extremes. I am then suppose to display the information in a table, and then find some statistics about those numbers. I am not sure how to send the calculated values into an array so I could calculate the statistics, like mean and range. I do not have much experience with arrays, so any advice would be helpful. Here is what I have so far

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
   double xmin, xmax;
   const int POINTS = 20;
   const double PI = 3.1416;
   double increments;
   int counter = 0;
   double array[POINTS];

   cout << "Enter in a value for the minimum x value: ";
   cin >> xmin;
   cout << "Enter in a value for the maximum x value: ";
   cin >> xmax;

   increments = (abs(xmin) + xmax) / POINTS;

   double x = xmin + increments * counter;
   double min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
   double max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

   cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
   cout << setw(32) << setfill('-') << " " << endl;
   cout << setfill(' ');

   while (x <= xmax)
   {
      cout << fixed << showpos << setw(15) << setprecision(2) << x <<     setw(15)
     << setprecision(4) << 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) << endl;
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) > max)
         max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) < min)
         min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      counter++;
      x = xmin + increments * counter;

   } // End of while (x <= xmax)

   system("pause");
   return 0;
} // End of function main()

Upvotes: 1

Views: 118

Answers (3)

Jes&#250;s Reyes
Jes&#250;s Reyes

Reputation: 46

I reedited your code to add some vector<> instances, I hope you understand it

#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>

using namespace std;

int main()
{
   double xmin, xmax;
   const int POINTS = 20;
   const double PI = 3.1416;
   double increments;
   int counter = 0;
   double array[POINTS];

   vector<vector<float> > values;

   cout << "Enter in a value for the minimum x value: ";
   cin >> xmin;
   cout << "Enter in a value for the maximum x value: ";
   cin >> xmax;

   increments = (abs(xmin) + xmax) / POINTS;

   double x = xmin + increments * counter;
   double min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
   double max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

   cout << setw(15) << "x |" << setw(15) << "f(x)" << endl;
   cout << setw(32) << setfill('-') << " " << endl;
   cout << setfill(' ');
   vector<float> auxiliar;

   while (x <= xmax)
   {
      auxiliar.resize(2);
      auxiliar[0] = x;
      auxiliar[1] = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);

      values.push_back(auxiliar);
      auxiliar.clear();

      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) > max)
         max = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      if (0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x) < min)
         min = 0.0572 * cos(4.667 * x) + 0.0218 * PI * cos(12.22 * x);
      counter++;
      x = xmin + increments * counter;

   }

   for(vector<float> i:values)
     cout << fixed << showpos << setw(15) << setprecision(2) << i[0] <<     setw(15) << setprecision(4) << i[1] << endl;
 }

I used an auxiliar to add a 2-element vector to vector > object using push_back() method from vector<> template. I hope it helps you! :D

Output is same that original(xmin = 2, xmax = 3)

Enter in a value for the minimum x value: 2
Enter in a value for the maximum x value: 3
            x |           f(x)
------------------------------- 
          +2.00        -0.0043
          +2.25        -0.0759
          +2.50        +0.0800
          +2.75        +0.0155
          +3.00        +0.0425

Upvotes: 1

Tyler Lewis
Tyler Lewis

Reputation: 881

Vectors are probably a good choice, as mentioned above. First, make sure you:

#include <vector>

Vectors work like an array, except it won't let you access out of bounds and you don't have to tell it how long to make the vector, it will expand as needed. First, you instantiate the vector with the type of things you will put into it:

std::vector<double> doubleVector;

The above vector named doubleVector will hold doubles, and is empty to begin with (doubleVector.size() == 0). Now, let's say I wanted to put the numbers 1.0 through 20.0 by increments of 0.2 into the vector. We insert into the vector by using it's push_back() member function:

for (double i = 1.0; i < 20.0; i+=0.2) {
        doubleVector.push_back(i);
} //doubleVector now contains 1.0, 1.2, 1.4, 1.6, and so on

Now, to access things in the function, you can use one of two methods. You can use the array syntax:

double myDouble = doubleVector[0] //myDouble now equals 1.0

Or, you can use the .at() member function:

double myDouble = doubleVector.at(0); //myDouble still equals 1.0

To iterate through the vector to display the contents, there are two main approaches. The first (similar to an array):

for (int i = 0; i < doubleVector.size(); i++) {
    std::cout << doubleVector.at(i) << std::endl;
}

Or the more preferred way, using the Standard Template Library (STL) iterators:

for (std::vector<double>::const_iterator iter = doubleVector.begin(); iter != doubleVector.end(); iter++) {
    std::cout << *iter << std::endl;
}

If you wanted to modify the contents of the vector, you would use iterator instead of const_iterator:

for (std::vector<double>::iterator iter = doubleVector.begin(); iter != doubleVector.end(); iter++) {
    (*iter) = (*iter) * (*iter);
} //Each element in the vector is now squared

Sorry that this isn't a very specific answer to your question, but the vector is a much preferred version than using a C style array, since they are easy to mess up and cause segfaults. Vectors are also very quick to access. Now, if you could mentioned exactly which calculation you want to store in the vector, I could help out a little bit. For more about regular vectors, see http://www.cplusplus.com/reference/vector/vector/

Upvotes: 0

Keno
Keno

Reputation: 2098

Essentially you'd want to use a std::vector to handle a dynamic task such as this. Use the push_back() function on the vector each time you wish to add an element to the array.

Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element. This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.

#include <vector>

//Rest of code here

for (int i = 0; i < POINTS; ++i) {
    vectorName.push_back(VARIABLE); //To add an element at the end
    ASSIGNMENT_VARIABLE[i] = vectorName.at(i); //To assign a variable to the value of each value in the variable
}

Reference

Upvotes: 1

Related Questions