2
\$\begingroup\$

So i got an task from school. Which ask's me to write a program tad creates an 2d array with 11 rows and 5 columns. In which the first column starts with 2.0 and increments by 0.1 all the way up to 3.0. Then in next column it should be devided by 1 so if x is 2.0 then 1 / x and goes trough all 11 rows. then in next column it has to multiply by 2 so x^2 and repeat in every column up to x^4. So so far i got it to do what i need , but i know there should be a better way to achieve this. as if there would be a task to go up to x^50 for example i imagine it would be pain to do with my way. as its hardcoded.

So here is my code:

#include<iostream>
#include<iomanip>
#include<array>

using namespace std;

int main()
{
 const int rows = 11;
 const int columns = 5;
 double x = 2.0;


 array<array<double, columns>,rows> A{};
 cout << setprecision(2);
 cout << fixed;
 cout << setw(2) <<"x" << setw(10) << "1/x" << setw(7) << "x^2" << setw(7) << "x^3" << setw(8)<< "x^4\n";
 cout << "------ ------ ------ ------ ------\n";
 A[0][0] = x;
 for(int i = 0; i < A.size(); i++)
 {
     A[i+1][0] = A[i][0] + 0.1;
     A[i][1] = 1 / A[i][0];
     A[i][2] = A[i][0] * A[i][0] * A[i][0];
     A[i][3] = A[i][0] * A[i][0] * A[i][0]* A[i][0];
     A[i][4] = A[i][0] * A[i][0] * A[i][0]* A[i][0] * A[i][0] ;
     for(int j = 0; j < A[0].size(); j++)
     {
         cout << A[i][j] << setw(4)  << " ";
     }
     cout << endl;
 }

 return 0;
 }

and here is how it's supposed to look: enter image description here

Looking for suggestions or tips. I am strugling here. And sorry for my english it's not my native.

\$\endgroup\$
1
  • \$\begingroup\$ You say "devided by 1" and "multiply by 2" but you do something different. And it's not x^2, either. Most of your table is obviously wrong. \$\endgroup\$ – superb rain Aug 28 '20 at 18:08
2
\$\begingroup\$

You're almost there, you just need to use std::pow() to raise a value to an arbitrary power, and add another for-loop to handle the repetetive work of filling in all the remaining columns:

for(int i = 0; i < A.size(); i++)
{
     A[i][0] = 2.0 + 0.1 * i;
     A[i][1] = 1 / A[i][0];

     for(int j = 2; j < A[i].size(); j++)
     {
         A[i][j] = std::pow(A[i][0], j);
     }
     ...
}
\$\endgroup\$
2
  • \$\begingroup\$ Thanks man. I didn't even know about std::pow :D Learned something new today. Thanks man. \$\endgroup\$ – DreamHacker Aug 29 '20 at 8:08
  • 1
    \$\begingroup\$ @DreamHacker It's a pretty basic function provided by the standard library. I suggest you browse through en.cppreference.com/w and see what other helpful functions there are. You don't have to remember everything, but just get a feel of what is out there, so next time you need something you know where to look. \$\endgroup\$ – G. Sliepen Aug 29 '20 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.