• 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

    • Sony unveil Marvel fighting game 'Tōkon: Fighting Souls' for PlayStation 5 and PC by Pulasthi Ariyasinghe Sony held a massive surprise in store for those who made to the end of its State of Play presentation today. Coming in as a collaboration between PlayStation Studios, Arc System Works, and Marvel Games, Tōkon: Fighting Souls is slated to be a tag team fighter featuring everyone's favorite heroes and villains from the Marvel comics universe. Watch the debut trailer above. Developer Arc System Works, which has previously worked on series like Guilty Gear and Blazblue, has revealed that the focus on this project was making sure it was easy to pick up and play by anyone. To put this into perspective, the studio says that while other fighting games require learning each character's moves to play them effectively, Tōkon will only have one way of playing for all available fighters. "From the beginning, we aimed to make MARVEL Tōkon: Fighting Souls a title that could be enjoyed by a wide variety of players," explains Game Director and Lead Battle Designer, Kazuto Sekine. "We have designed the mechanics in such a way that you can perform a variety of actions with either traditional fighting game inputs or simple inputs. With the press of a few buttons, multiple characters can appear on screen to provide backup or attack together – creating a new and exciting team VS battle experience." So far, Captain America, Iron Man, Spider-Man, Doctor Doom, Storm, Ms. Marvel, Star-Lord, and Ghost Rider (Robbie Reyes) have been revealed as playable characters, neatly filling the trailer's four versus four battle. Each character has a Japanese flair to them too, such as Iron Man's unique Gundam style for his armor. As for why the studio went with the 4v4 Tag Team battle solution, the team said that the team-up nature of superheroes was the inspiration for that, making sure that players can fill the screen with all sorts of powers they are familiar with. Tōkon: Fighting Souls is coming to both PlayStation 5 consoles as well as PC sometime in 2026. More announcements with reveals for characters, stages, gameplay modes, and online mechanics will be coming up until launch as well.
    • Nothing surprising there. Anyone expecting privacy on ANY social media platform is delusional at best. Let alone one controlled by the same creep that owns Tesla (a.k.a. facecrook on wheels), which blatantly violates user privacy with Tesla vehicles as reported by Louis Rossman. This action of his is just another prime example of why ANYTHING from him is trust-worthy as the same from Zuckerberg...i.e. not at all.
    • 007 First Light's first trailer shows off an action-packed James Bond game by Pulasthi Ariyasinghe IO Interactive has been teasing its next project outside of Hitman for some time now, and even before its first-ever showcase that's happening soon, the James Bond game showed up during the PlayStation State of Play event today. Dubbed 007 First Light, the third-person action-adventure title received a trailer showing off the new face of Bond, some of his gadgets, and plenty of action. Catch it above. While inspired by novels of Ian Fleming as well as the movie franchise, First Light will follow Bond to reveal just how he earned his MI6 007 role in an all-original story by IO Interactive. James Bond will be just 26 years old in this iteration. While not the smooth and tactical agent with a martini that we've seen in the movies just yet, this Bond is described as a man with "sharp instincts, sometimes reckless, who is still learning when to fight, when to bluff, and when to disappear into the shadows." As expected from the Hitman developer, missions in 007 First Light will offer both stealth and loud options, depending on the player's actions. A variety of futuristic gadgets will be available for use as well, while driving portions are also confirmed for the title. "In 007 First Light, Bond starts as a NAVY air crewman, when against all odds, an audacious act of bravery propels him on MI6’s most challenging training program," says the studio, regarding Bond's beginnings. "This training coupled with his natural instinct, wits, and heart will see him grow into a fully-fledged spy. It’s a completely original standalone story, developed in collaboration with Amazon MGM Studios." The studio says that the game will take players across the world, from snow-drenched mountains to sun-soaked beaches, while interacting with some of the most iconic characters in the franchise, including M, Q, and Moneypenny. There are original faces in the story too, such as Bond's mentor Greenway. 007 First Light is coming to PC (Steam and Epic Games Store, Xbox Series X|S, PlayStation 5, and the Nintendo Switch 2 sometime in 2026.
    • That sharp cold toothache you dread? Its origins trace back to ancient, unexpected purpose by Sayan Sen Image by Pavel Danilyuk via Pexels Scientists at the University of Chicago have discovered that the sensitive tissue inside our teeth first evolved as part of the armored skin of ancient fish. Their new study, published in Nature, confirms that dentine—a key part of teeth—was originally used by early vertebrates to sense their surroundings. This research supports the idea that dentine wasn’t always used for chewing. Instead, millions of years ago, it helped fish detect changes in the water. The study also clears up confusion about Anatolepis heintzi, a fossil once thought to be the earliest known vertebrate because of its dentine-like structures. For years, scientists debated whether Anatolepis was really an early vertebrate. The fossil had tiny tubules that some researchers believed were odontodes—small structures considered to be the ancestors of teeth. However, there wasn’t enough evidence to be sure. To settle the debate, scientists used synchrotron scanning, a powerful imaging technique, to study different fossils and modern creatures. The scans revealed that Anatolepis didn’t have dentine. Instead, its tubules were sensory structures similar to those found in arthropods like crabs and shrimp. These structures, called sensilla, help animals detect their surroundings. “This shows us that ‘teeth’ can also be sensory even when they’re not in the mouth,” said lead researcher Yara Haridy, PhD. “There’s sensitive armor in these fish. There’s sensitive armor in these arthropods. This explains the confusion with these early Cambrian animals.” Although Anatolepis turned out to be an arthropod, researchers did find real dentine in another ancient fish. The Ordovician vertebrate Eriptychius, which lived about 465 million years ago, had large dentine-filled tubules in its armor. This confirms that dentine first evolved in vertebrates as a sensory tissue. Further tests showed that modern fish, such as sharks and teleosts, still have nerve connections in their external dentine structures. This means early vertebrates may have used dentine to sense their environment before it became part of teeth. Scientists have two main ideas about how teeth came to be. The “inside-out” theory suggests teeth evolved first and were later adapted for exoskeletons. The new research supports the “outside-in” theory, which argues that sensory structures appeared first in exoskeletons and later evolved into teeth. While the team didn’t find the oldest vertebrate fish, study co-author Neil Shubin, PhD, believes the discovery is still important. “We didn’t find the earliest one, but in some ways, we found something way cooler,” he said. Source: University of Chicago, Nature This article was generated with some help from AI and reviewed by an editor. Under Section 107 of the Copyright Act 1976, this material is used for the purpose of news reporting. Fair use is a use permitted by copyright statute that might otherwise be infringing.
  • Recent Achievements

    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      347
    2. 2
      snowy owl
      167
    3. 3
      +FloatingFatMan
      164
    4. 4
      ATLien_0
      161
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!