Square Roots and Totals Table c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Square Roots and Totals Table c++

  1. #1
    Join Date
    Jan 2013
    Posts
    4

    Square Roots and Totals Table c++

    Hey I am looking to create a table somthing like this output below but I am very new to this and my code is abit everywhere.

    x sqrt(x) x ^ 2 x ^ 3
    ========================================
    1 1 1 1
    2 1.414 4 8
    3 1.732 9 27
    4 2 16 64
    5 2.236 25 125
    6 2.449 36 216
    7 2.646 49 343
    8 2.828 64 512
    9 3 81 729
    10 3.162 100 1000
    ========================================
    Totals: 55 22.47 385 3025
    ========================================


    This is the code I have I have got it to cal some of the values but numbers our everywhere. I am doing more Trial and error than anything.

    #include "stdafx.h"
    #include <iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    double x

    ;

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

    for(x = 1.0; x <= 20.0; x++) {
    cout.width();
    cout <<x*x*x << " ";
    cout.width(7);
    cout << x << " ";
    cout.width(7);
    cout << sqrt(x) << " ";
    cout.width(7);
    cout << x*x << '\n';

    }
    system("pause");



    return 0;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: Square Roots and Totals Table c++

    Without a specific question, all I can say is look at the order of your cout statements and your header. They don't match. You may want to get rid of the \n in your header too.

    Also, your for loop variable should be an int. You can cast it to a double to call the sqrt function.

  3. #3
    Join Date
    Jan 2013
    Posts
    4

    Re: Square Roots and Totals Table c++

    Here is the question I am trying to do

    Write a program that prints a table of values such as the one below for all numbers starting at 1 and up to and including a number which will be input from the console.

    Your output might look something like:

    x sqrt(x) x ^ 2 x ^ 3
    ========================================
    1 1 1 1
    2 1.414 4 8
    3 1.732 9 27
    4 2 16 64
    5 2.236 25 125
    6 2.449 36 216
    7 2.646 49 343
    8 2.828 64 512
    9 3 81 729
    10 3.162 100 1000
    ========================================
    Totals: 55 22.47 385 3025
    ========================================

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: Square Roots and Totals Table c++

    That's not a specific question addressing a problem you're having. Your question should be along the lines of "Here's my code. It's doing x when I want it to do y, and I'm not sure how to make it do z."

    Anyway, I gave you a couple of suggestions in my previous post.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,396

    Re: Square Roots and Totals Table c++

    GCDEF gave you the hints that you need to make your program right. Also, please format your code. Use 'Go Advanced' then select the code then press '#' It makes the code much easier to read.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    double x;
    
         cout.precision(4);
         cout << " x sqrt(x) x^2\n\n x^3\n";
    
         for (x = 1.0; x <= 20.0; x++) {
              cout.width();
              cout << x*x*x << " ";
              cout.width(7);
              cout << x << " ";
              cout.width(7);
              cout << sqrt(x) << " ";
              cout.width(7);
              cout << x*x << '\n';
         }
         system("pause");
         return 0;
    }
    When you have \n in a string this causes a new line to be started in the output. So the \n\n after x^2 causes 2 new lines to be output which is probably not what you want. You also want the output in the same order as your heading which is x sqrt(x) x^2 and x^3 but you are outputting in the order x^3 x sqrt(x) x^2. Cout outputs in the order that it is used.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,098

    Re: Square Roots and Totals Table c++

    I was kind of hoping he'd take the hints and figure it out himself, rather than having it spoon fed to him.

  7. #7
    Join Date
    Jan 2013
    Posts
    4

    Re: Square Roots and Totals Table c++

    This is the way it should be?... I was also wondering what way I would go about adding a total row along the bottom?

    #include "stdafx.h"
    #include <iostream>
    #include <cmath>
    using namespace std;

    int main()
    {
    double x;

    ;

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

    for(x = 1.0; x <= 10.0; x++) {

    cout.width(7);
    cout << x << " ";
    cout.width(7);
    cout << sqrt(x) << " ";
    cout.width(7);
    cout << x*x << " ";
    cout.width(7);
    cout << x*x*x << '\n';
    }




    system("pause");

    return 0;

    }

  8. #8
    Join Date
    Jan 2013
    Posts
    4

    Re: Square Roots and Totals Table c++

    cheers lads never done much of this **** before really just tryin to get my head round

  9. #9
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Square Roots and Totals Table c++

    Quote Originally Posted by Rich1e1990 View Post
    This is the way it should be?... I was also wondering what way I would go about adding a total row along the bottom?
    Go back and change your loop to use integers, not doubles.

    The reason is that floating point arithmetic is not exact. Right now, you're only getting away with it because you're adding 1 to the value. If you were adding 0.1 or some other fractional element, the loop will execute an indefinite number of times due to floating point rounding errors.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center