Thread: Square ints, setw problem(not too hard ;))

  1. #1
    Registered User Daniel Primed's Avatar
    Join Date
    Jun 2005
    Posts
    13

    Square ints, setw problem(not too hard ;))

    Still working thru Accelerated C++, I've got yet another problem. To solve more problems it would be benifital if I just had the answer as it would help me much more than explaining it and then letting me figure it out.

    What I'm trying to do is fairly easy but a few simple niggles are getting in the way. So if someone could complete the following exercise it would be greatly apreciated. This is what I'm trying to do:

    Write a program to calculate the squares of int values up to 100. The program should write two colmns: The first lists the value; the second contains the square of that value. Use setw to manage the output so that the values line up in column.

    Thanks for the help. I hate asking for solutions to my problems. Someday I want to be someone that helps people like me on this forum.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You post an attempt, we'll help you fix it.

  3. #3
    Registered User Daniel Primed's Avatar
    Join Date
    Jun 2005
    Posts
    13
    I'd rather somebody do it themself.

    Main problem is that I don't know how to use setw and setting up the vectors,how to identify each one.

    Sorry if I'm being a bother.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    I would write the program but I don't have the time. Try looking at this tutorial and if you still can't figure it out, I will post the source for the program you want.

    http://cprogramming.com/tutorial/iomanip.html

    For finding the square of every number less than or equal to 100 you will need a loop. Probably something like

    Code:
    int main()
    {
    int x;
    x = 0;
    while (x =< 100)
    {
    square root algorithms
    x++;
    }
    return 0;
    }
    or you could use a for loop, either one will work

    I hope this helps.
    Last edited by natey14; 01-18-2006 at 12:07 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Main problem is that I don't know how to use setw and setting up the vectors
    1. You don't need vectors to answer this queston.
    2. You can print the result with less formatting without using setw.

    In short, show us what you can do then we'll help.

    > I'd rather somebody do it themself.
    I'd rather you'd ........ off you time wasting sponger

  6. #6
    Registered User Daniel Primed's Avatar
    Join Date
    Jun 2005
    Posts
    13
    Gave it a go and here's what I got:

    Code:
    #include<iostream>
    #include<vector>
    
    using namespace std;
    
    int main();
    {
        vector<int> list;
        int number=0;
        vector<int>::iterator beg=list.begin();
        list.end()=100;//not sure if this will work or if I say that the end if when when size=100, 100 elements
        while(iter!=list.end()){
                                number++;
                                list.push_back(number);
                                cout<<number<<endl;
                                //setw stuff goes here
                                cout<<number*number;
        }else
            ++iter;
        }
    Problems are, not sure how to use setw,or should I use something else. If so what? Other than that am I writing to the vector elements properly?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    for ( int i = 0 ; i < 100 ; i++ ) list.push_back(i*i);
    for ( int i = 0 ; i < 100 ; i++ ) cout << i << " " << list[i] << endl;

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Salem is right(not like I even needed to say that as he is always right, lol), you really don't need vectors for a problem like this. In fact if you don't fully understand vectors than I wouldn't try using them....Just a simple loop will do it...

    Code:
    int main()
    {
        int x;
        int square;
        x = 0;
        while (x <= 100) //Keeps looping until it hits 100
        {
              square = x * x;  //Squares whatever value x is
              cout << "The square of " << x << " is " << square << endl; // Outputs the numbers
              x++;
        } 
    cin.get(); // Waits for you to press something
    }
    You don't even really need setw for the program I just wrote but if you want to I still suggest figuring out setw and setprecision.
    "SETW" tutorial >>>>>>>> http://cprogramming.com/tutorial/iomanip.html
    Last edited by natey14; 01-19-2006 at 10:12 AM.

  9. #9
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    what do they mean with square? the square root? n^n+1 or n * n?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Mitsukai
    what do they mean with square? the square root? n^n+1 or n * n?
    n * n

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  2. Help with my draughts game
    By Zishaan in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 07:33 AM
  3. C++ Checkers Game
    By SnS CEO in forum C++ Programming
    Replies: 9
    Last Post: 09-07-2005, 01:21 AM
  4. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  5. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM