• 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

    • WhatsApp beta users can now craft their own AI chatbots - here's why you might want one by Paul Hill Since the end of 2022, tech companies, and even non-tech companies, have been clamoring to pile AI into their services. Despite what many people say about not liking AI, plenty of people are still using it every day, making it a key offering. Not only that, but for public companies like Meta, the inclusion of AI does very well with investors, so that’s another reason it’s being added. While the most common chatbot people talk about is ChatGPT, which is pretty faceless, there is demand for AI chatbots with a face, this is why people use tools like Character.ai and Replika. One of the only big tech firms that has gone down this route is Meta, which lets you create and share AI characters. To date, some of Meta’s apps, like Messenger, allow you to chat with these AI personas but you can’t do that yet in the stable version of WhatsApp. The company is now testing it with the Android Beta and when it’s ready, it should make a more seamless experience across Meta’s applications. Many of the popular bots that people use including ChatGPT, Gemini, and DeepSeek are faceless and offer the same tone out of the box. To be fair to Gemini, it does allow all users to create Gems now, and they actually offer a bit more flexibility than just creating characters to talk to like in Messenger. The chatbots in Messenger have the benefit of being in the Messenger app, which most people use and giving them a personality and making them feel like an “AI person” fits better in Messenger. Whether we really need these AI bots in Messenger is still up for debate. It’s quite a new feature and some people may find some good uses for them, but as mentioned, they don’t seem as flexible, or provide as detailed responses as custom bots made on Poe or Gemini Gems. They are definitely for having casual conversations with. WhatsApp's new AI chatbot creator We’ve known that the chatbot feature was coming to WhatsApp for a long time already. WhatsApp beta for Android 2.25.1.26, released in January, included the feature for some beta testers. With the latest WhatsApp beta for Android 2.25.18.4, it seems like WhatsApp is trialing the feature with members of the public, suggesting its release is imminent. Screenshots of the app, obtained by WABetaInfo show that you can describe your AI, select its personality, its traits, its image and more. The process seems to be the same as the process already available in Messenger. One of the nice things that Meta provides when creating these AI bots is templates and suggestions such as the attitude of the bot or the instructions for the bot. This is the same as in Messenger and allows you to get started chatting with your custom bots faster. In terms of sharing, you have the option to make the bots private, share them with friends (at least in the case of Messenger and presumably WhatsApp), or share them publicly. If you make something specific for your needs then the private option would be best, while bots with mass appeal could be set to public. Creating bots in WhatsApp is straightforward once you have access to the AI Studio. During the creation process you’ll need to name your AI, define its personality, choose a tone, design an avatar (some will be made for you with Meta’s AI), and create a catchy tagline to attract users if you ever set it to public. Much of the information will be pre-filled based on the initial details you provide about the AI’s role and personality. Some ideas for bots that you can create include a motivational coach, a travel recommendation AI, or a daily planner. While setting up these AI bots is easy to do, users may find their actual benefits limited. Besides the nagging feeling that you’re socializing with a clever bit of code, Meta seems to truncate the answers of these bots so they don’t rattle on, but depending on what you want them to do, you may need them to give a lengthy response, but they won’t. What personalized AI chatbots could offer If you are looking for an AI that chats to you conversationally like real people do, then this could be the feature you’re looking for. The fact that you can personalize bots with specific traits is something you can’t do as easily in apps like ChatGPT and Gemini and the fact that they have an avatar makes them more connectable too. Two of the defining features of Meta’s AI implementation is the ability to create custom AIs with a unique personality and to share them publicly. If you are having difficulty thinking of what a bot could be instructed to do, you can easily find community bots and interact with those instead and may find they provide some value. While these bots could be interesting for some people, they do carry the same risks as other AIs and that is that they can hallucinate. There was also a case in the UK where a man had been encouraged by his Replika to break into Buckingham Palace with a crossbow to kill the then head of state. Similar issues to this could result from Meta’s AI chatbots in time. Potential pitfalls While the feature is pretty interesting there are some things to be aware of. Firstly, the feature is still in beta on WhatsApp so you may run into issues and things could change once it’s finally released. Meta also states that it uses your interactions to improve its AI services, for this reason it is essential not to share personal information as Meta could read it. While Meta does limit the creation of bots that go against its standards, the company also warns that bots can output harmful content, so this could be dangerous for impressionable people who end up acting on what an AI has said with negative outcomes. What to watch for next It’s not clear when these AI chatbots will be available in the stable channel but given that a wider rollout is underway among beta users perhaps we are not too far off. For most people, this is not going to be a must-have feature, just a nice to have. We’ve been using WhatsApp to chat with friends for years, so clearly the app is just fine without the inclusion of AI, but when it’s available, people may be able to get more value out of the app. When the feature launches for all users, bots should be discoverable in the same way they are on Messenger where they’re categorized by category allowing users to begin chats easily. It remains to be seen how users will interact with this feature in the long-run. Last year, we reported that Meta was looking to give bots profiles on its social networks and this was met by somebacklash in our comments section.
    • Microsoft confirms Windows Outlook breaks in many ways after major Calendar feature upgrade by Sayan Sen Microsoft has been trying to get more users onto New Outlook for Windows, and it is doing so not just by enforcing the newer app but also by making improvements along the way. In doing so, though, the company has caused the Classic Outlook app to bug out in the past. The classic app received a major Shared Calendar-related upgrade recently, with many " long-awaited improvements" as well as "small changes in form and function." As the name suggests, the Outlook Shared Calendar essentially allows multiple people to interact with and manage the calendar. With Shared Calendar improvements enabled, users will see the following changes: Instant sync and view of shared calendars Editing series end date does not reset the past Accepting meeting without having to send a response Last Modified By no longer shown in the meeting item Adding same calendar multiple times can't be done Duplicate calendars simultaneously selection Attachments addition not possible when responding to a meeting invitation Event drafts auto-save changes The "Download shared folders" setting is ignored Unfortunately, as with any major feature upgrade, there are bugs, and Microsoft has confirmed this is no different. The tech giant has shared official guidance for it so that users can work around the problems. According to the company, "Shared Calendar improvements are now enabled by default in the most recent versions of Outlook, in all update channels for Microsoft 365 Apps," and thus, the bugs are likely to affect many. Here are some of the bugs Microsoft is investigating, as well as their workarounds: Bug Workaround Meeting cancellation sent unexpectedly to some attendees in classic Outlook In a REST shared calendar, after adding or removing an attendee, or forwarding a meeting, a meeting cancellation may be sent unexpectedly to some attendees. Use the Outlook Web App or new Outlook when adding or removing an attendee or forwarding a meeting. Attendees do not get updates on attachment changes by Delegate When a delegate sends an update on a meeting that requires removing an attachment on an occurrence of a meeting series, the recipients may not get some or all of the attachment changes. In the delegate's Sync Issues folder, you'll see sync errors. Example: 17:23:26 Synchronizer Version 16.0.15313 17:23:26 Synchronizing Mailbox 'Delegate User' 17:23:26 Synchronizing local changes in folder 'Manager User' 17:23:27 Uploading to server 'https://outlook.office365.com/mapi/emsmdb/?xxxxxxxx-xx' 17:23:30 Error synchronizing folder 17:23:30 [0-320] There is no known workaround. It is recommended, whenever possible, to save attachments to SharePoint or to OneDrive and share with a link. After an attachment is deleted from an existing meeting, it may reappear after being deleted Please wait approximately one minute to give the sync time to complete. Additionally, it is advisable to save attachments to SharePoint or OneDrive whenever possible and share them using a link. A meeting created by a delegate with limited calendar access disappears and is unsent when a sensitivity label other than "Normal" is selected Three potential solutions to address this issue, each with their own implications for functionality: Manager can update delegate's permissions to allow viewing of private items. Delegate can change the sensitivity label of the meeting to "Normal". Delegate can disable Shared Calendar Improvements (not recommended). Aside from these, Microsoft has also fixed several other bugs, which you can find in the official support article here on the company's website.
    • I’ve just paid £290/$390 for a 4TB Samsung 990 Pro for my PS5 Pro so it’s not too far from the going rate. Microsoft should definitely copy Sony and let users buy their own SSD in their next consoles rather than this proprietary stuff. I paid £374/$505 for the 2TB Seagate card for my Series X a few years ago so it’s not exactly over priced. 4TB of NVMe storage ain’t cheap!
    • The EU regulations force companies to respect users privacy, choice and data. Something all tech companies have abused to the hilt and would continue to do so if it wasn’t for important legislation and laws the EU brought in, which have been adopted elsewhere around the world. The EU can be a nuisance, but they actually do more good than harm. Forcing Apple, Google, Microsoft etc to make changes hasn’t negatively impacted anyone apart from their financials as they aren’t free to pillage our data like they once were, unless they explicitly provide options to obtain consent.
    • Windows 10 Enterprise IoT LTSC will continue getting updates until January 2032. I would expect support from most programs to continue until then. Firefox still supports Windows 7 (until the end of August), which will be just over 16 years since release. Windows 10 will be of a very similar age in January 2032. I'm sure some things like games will move on earlier, but I imagine a Windows 10 machine will be safe and usable for a long time to come yet, despite the pressure and fearmongering from those who stand to gain from selling you a new PC.
  • Recent Achievements

    • Enthusiast
      Epaminombas went up a rank
      Enthusiast
    • Posting Machine
      Fiza Ali earned a badge
      Posting Machine
    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      186
    2. 2
      snowy owl
      130
    3. 3
      ATLien_0
      129
    4. 4
      Xenon
      119
    5. 5
      +FloatingFatMan
      91
  • Tell a friend

    Love Neowin? Tell a friend!