• 0

Mathematical Table Using Squared, Cubed, Square-root, and Cubed Root


Question

I have to create a table exactly like the one in the attachment using squared, cubed, square-root, and cube root. I am having a hard time creating the code and been struggling for hours, can anyone point me towards the right direction or have the code do this? thanks

 

post-507579-0-23039300-1383783580.png

14 answers to this question

Recommended Posts

  • 0

I am pretty new at this but this is all I got so far:

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double x = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;

    cout << "Where would you like to start? ";
    cin >> x;
    cout << endl;

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;

    cout << " x sqrt(x) x^2\n\n x^3\n";

    squared = x*x;
    cubed = x*x*x;
    squareRoot = sqrt (x);
    cubeRoot = pow(x), 1/3;


    return 0;
}

  • 0

here is my updated one, I can't get it to stop looping. I want it to stop at the number of rows when the user enters it

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double number = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;

    cout << "Where would you like to start? ";
    cin >> number;
    cout << endl;

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;
        

    squared = number*number;
    cubed = number*number*number;
    squareRoot = sqrt (number);
    cubeRoot = pow((double)(number),(double)1/3);

    do
    {
    cout << number << "  " << squared << "  " << cubed << "  " << squareRoot << "  " << cubeRoot << "  " << ceiling << "  " << floor << "  " << endl;
    number++, squared++, cubed++, squareRoot++, cubeRoot++, ceiling++, floor++;
    }
    while (number > 0);


    return 0;
}

  • 0

Well obviously it isn't going to stop since you increment 'number' inside the loop and have 'while (number < 0)' as your condition. The instructions say use a for-loop, so use a for-loop.

 

http://code.wikia.com/wiki/For_loop

  • 0

okay I stopped it from looping but is there any suggestions you would suggest for me to revise my code to make it look like the table in the attachment?

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double number = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;

    cout << "Where would you like to start? ";
    cin >> number;
    cout << endl;

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;
        

    squared = number*number;
    cubed = number*number*number;
    squareRoot = sqrt (number);
    cubeRoot = pow((double)(number),(double)1/3);

    do
    {
    cout << number << "  " << squared << "  " << cubed << "  " << squareRoot << "  " << cubeRoot << "  " << ceiling << "  " << floor << "  " << endl;
    ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;
    break;
    }
    while (number > 0);


    return 0;
}

  • 0

do
     {
     cout << number << "  " << squared << "  " << cubed << "  " << squareRoot << "  " << cubeRoot << "  " << ceiling << "  " << floor << "  " << endl;
     ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;
     [b]break;[/b]
     }
     while (number > 0);
You don't want that (the break) there. It will exit the do...while loop immediately after the first line of output.

Also, the way you've written it you should be decrementing "number" rather than incrementing it, or you could create a second count variable and increment it until it reaches the target number. As it stands it will fail because "number" your starting calculation number so you *should* be using "rows" in the do loop test instead of "number". :)

EDIT: Ugh, sorry, misread. You want to test against rows, but obviously will want to continue incrementing number.

  • 0

okay I just revised this and removed the break, so do you got any suggestions how to revise my code from looping and make my table to look exactly like the one in the attachment?

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double number = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;

    while (number <= 0)
    {

    cout << "Where would you like to start? ";
    cin >> number;
    cout << endl;
    }

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;


    squared = number*number;
    cubed = number*number*number;
    squareRoot = sqrt (number);
    cubeRoot = pow((double)(number),(double)1/3);

    do
    {
    cout << number << setw(5) << squared << setw(5) << cubed << setw(5) << squareRoot << setw(5) << cubeRoot << setw(5) << ceiling << setw(5) << floor << setw(5);
    ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;
    }
    while (number > 0);


    return 0;
}

  • 0

You could use tabs instead of spaces. I think it's "\t" in C?

You still need to fix your while statement, too. Unless you enter a negative starting number it will always be greater than zero. Plus, you're outputting your rows in the loop so you need to be testing against that variable.

for (int rowCount = 0; rowCount < rows; rowCount++)

{

...code...

}

  • 0

is that the correct place where I place the " (int rowCount = 0; rowCount < rows; rowCount++) " ? I placed it before the "do", but it still loops

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double number = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;


    {

    cout << "Where would you like to start? ";
    cin >> number;
    cout << endl;
    }

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;


    squared = number*number;
    cubed = number*number*number;
    squareRoot = sqrt (number);
    cubeRoot = pow((double)(number),(double)1/3);

    for (int rowCount = 0; rowCount < rows; rowCount++)

    do
    {
    cout << number << setw(5) << squared << setw(5) << cubed << setw(5) << squareRoot << setw(5) << cubeRoot << setw(5) << ceiling << setw(5) << floor << setw(5);
    ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;

    }
    while (number > 0);


    return 0;
}

  • 0

No, it should replace the do...while construct. The assignment you posted said to use a for... loop.

     for (int rowCount = 0; rowCount < rows; rowCount++)
     {
     cout << number << setw(5) << squared << setw(5) << cubed << setw(5) << squareRoot << setw(5) << cubeRoot << setw(5) << ceiling << setw(5) << floor << setw(5);
     ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;

     }
  

     return 0;
 }
  • 0

thank you for helping to make the program to stop the loop but my values still display diagonally and not evenly line up like a regular table should. it looks really random and values are located everywhere:

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double number = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;


    {

    cout << "Where would you like to start? ";
    cin >> number;
    cout << endl;
    }

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;


    squared = number*number;
    cubed = pow(number, 3.0);
    squareRoot = sqrt (number);
    cubeRoot = pow(number, 1/3);

    for (int rowCount = 0; rowCount < rows; rowCount++)
     {
     cout << number << setw(5) << squared << setw(5) << cubed << setw(5) << squareRoot << setw(5) << cubeRoot << setw(5) << ceiling << setw(5) << floor << setw(5);
     ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;

     }
 

     return 0;
 }
 

  • 0

I tried adding the the titles for my table but they still look very uneven looking:

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{

    double number = 0;
    double rows = 0;
    double squared = 0;
    double cubed = 0;
    double squareRoot = 0;
    double cubeRoot = 0;
    int ceiling = 0;
    int floor = 0;


    {

    cout << "Where would you like to start? ";
    cin >> number;
    cout << endl;
    }

    cout << "And how many rows? ";
    cin >> rows;
    cout << endl;


    squared = number*number;
    cubed = pow(number, 3.0);
    squareRoot = sqrt (number);
    cubeRoot = pow(number, 1/3);

    cout << "Number" << setw(6) << "Squared" << setw(6) << "Cubed" << setw(6) << "SquareRoot" << setw(6) << "CubeRoot" << setw(6) << "Ceiling" << setw(6) << "Floor" << setw(6);

    for (int rowCount = 0; rowCount < rows; rowCount++)
    {
     cout << number << setw(6) << squared << setw(6) << cubed << setw(6) << squareRoot << setw(6) << cubeRoot << setw(6) << ceiling << setw(6) << floor << setw(6);
     ++number, ++squared, ++cubed, ++squareRoot, ++cubeRoot, ++ceiling, ++floor;

     }
 

     return 0;
 }
 

  • 0

How about you worry about logic first.

 

You need a for loop, right?

You don't even need to be storing them into variables.

This whole thing could be real simple.

 

Your only inputs come from the user, and there are only 2.

So.... the pseudocode logic then becomes:

 

// Get starting point n (double)

// Get number of rows (integer)

// Print header

// For each row, print the set of values for the row of data, incrementing n by 0.1 in the process

 

Your for-loop would then look like:

    for (int intRow = 0; intRow < intRows; intRow++)
    {

         // Output formatted n

         cout << n

 

         // Output formatted squared

         cout << n*n

 

         // Output formatted cubed

         cout << n*n*n

 

         // etc...

 

         // increment n by 0.1

         n += 0.1

     }

 

The point of the exercise is to understand the LOGIC, not to worry a ton about the formatting.

Does the LOGIC make more sense to you?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • AMD Releases new GPU driver with Windows Server 2025 support and new Ryzen chips by Taras Buria AMD has released a new Pro Edition driver for customers with Radeon PRO graphics cards and certain AMD Ryzen PRO and Ryzen AI processors. These drivers get fewer updates than your standard Radeon Software drivers for mainstream gaming graphics cards, and their goal is to deliver a more stable experience with a bigger emphasis on performance and security for professional environments and use-case scenarios. AMD Software Pro Edition 25 Q2 is now available for download with the following update highlights: Support for AMD Ryzen AI PRO Series and AMD Ryzen AI Max 300 Series Support for Microsoft Windows Server 2025 And here is the list of known bugs: “Media Offline” error appears after importing certain 8K media files in DaVinci Resolve Terrain mapping images fail to load properly on Tresus MVPPro Issue with shadows when using the Subdivision Surface modifier in Blender EEVEE AMD Software Pro Edition 25 Q2 driver is compatible with 64-bit Windows 10 or 11 and Windows Server 2022 or 2025 systems with the following AMD processors and graphics cards: AMD Ryzen AI Series Radeon Pro Series Lenovo Mobile HP with AMD Ryzen Pro AMD Ryzen AI Pro Series AMD Ryzen AI Max+ 300 Series AMD Radeon PRO W7000 Series AMD Radeon PRO W6000 Series AMD Radeon PRO W5000 Series AMD Radeon PRO WX Series AMD Radeon Vega Frontier Edition Radeon PRO Duo (Polaris AMD Radeon PRO VII P16s Gen1 P16s Gen2 P14s Gen1 P14s Gen2 P14s Gen3 P14s Gen4 P14s Gen5 HP Loke (AMD Ryzen 9 Pro 7940HS) HP LoxW (AMD Ryzen 5 Pro 7640HS) HP Firefly 14 G11 A HP ZBook Power G11 You can download the AMD Software Pro Edition 25 Q2 driver from the official AMD support page. Full release notes are available on the same page as well.
    • Because of these forums, I decided to give Linux a try a few years ago. I started with Fedora, bounced around to Ubuntu, Mint, and I've landed on PopOS! and I love it. Other than my son's Macbook and his Chromebook, everything in the house runs PopOS!. When I switched my mother over to it, I showed her where the email icon was, and how to get to her Facebook, and I haven't heard a complaint. Linux is far more usable than it once was. I have a dedicated laptop that I try different distros out on all the time, but mostly I reside in Pop. What I love is if I do run into a problem, there are so many forum resources out there to find answers. Most times the distro itself will have user forums that already have a fix for the issue I'm facing. People ditching Windows 10, should they choose to, should at least try Linux. It might not be for them, but if it is, they enter a completely new world of computing. Waiting in the wings will always be Windows 11 or a Mac should they choose that route.
    • The real trick is to make sure the funds go to the right people. Anyone remember the African "famine" of the 1980s/early 1990s? A lot of that was local warlords preventing people from getting food and aid. Thousands of tons of food sat in warehouses and spoiled/got eaten by rodents and untold numbers of innocents perished. Dictatorship and corruption and rot has to be rooted out or else his money is just going to make the wrong people very rich.
  • Recent Achievements

    • Week One Done
      jrromero17 earned a badge
      Week One Done
    • One Month Later
      jrromero17 earned a badge
      One Month Later
    • Conversation Starter
      johnwin1 earned a badge
      Conversation Starter
    • One Month Later
      Marwin earned a badge
      One Month Later
    • One Year In
      fred8615 earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      228
    2. 2
      snowy owl
      156
    3. 3
      ATLien_0
      138
    4. 4
      Xenon
      128
    5. 5
      +FloatingFatMan
      127
  • Tell a friend

    Love Neowin? Tell a friend!