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

1. ## 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.

3. 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. 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.

5. > 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. 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. 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. 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

9. what do they mean with square? the square root? n^n+1 or n * n?

10. Originally Posted by Mitsukai
what do they mean with square? the square root? n^n+1 or n * n?
n * n