From: Pavel R. on
Hello, I was wondering how to approach the second part of this
problem.

Part 1 : Write a program to calculate the squares of int values up to
100. The program should write two columns : 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 columns.

Here is the code for that part :
for ( int i = 0; i <= 100; i++)
{
cout << i << setw(6) << i*i << endl;
}

Part 2 : What happens if we rewrite the previous program to allow
values up to but not including 1000 but neglect to change the
arguments to setw? Rewrite the program to be more robust in the face
of changes that allow i to grow without adjusting the setw arguments.

I don't see how I can pass the length of the integers ( int a = 5;
a.length or size) into the setw to predict all the possible widths of
the output.


Can anyone help me out? (this is a not a homework problem.)
From: Richard Heathfield on
In
<097444c8-7b0b-4f4e-aaf7-ec4e6988e0e3(a)o31g2000vbi.googlegroups.com>,
Pavel R. wrote:

> Hello, I was wondering how to approach the second part of this
> problem.
>
> Part 1 : Write a program to calculate the squares of int values up
> to 100. The program should write two columns : 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 columns.
>
> Here is the code for that part :
> for ( int i = 0; i <= 100; i++)
> {
> cout << i << setw(6) << i*i << endl;
> }
>
> Part 2 : What happens if we rewrite the previous program to allow
> values up to but not including 1000 but neglect to change the
> arguments to setw? Rewrite the program to be more robust in the face
> of changes that allow i to grow without adjusting the setw
> arguments.
>
> I don't see how I can pass the length of the integers ( int a = 5;
> a.length or size) into the setw to predict all the possible widths
> of the output.
>
>
> Can anyone help me out? (this is a not a homework problem.)

Here's a solution that betrays my C origins.

#include <limits.h>
int GetWidth(int n)
{
int w = 1; /* first digit - all numbers have them */
if(n < 0)
{
++w; /* sign */
if(n == INT_MIN)
{
++n; /* this will not affect the digit count */
}
n = -n;
}
while( n > 9) /* while we have at least two digits... */
{
++w; /* ...count the digit... */
n /= 10; /* ...and drop it... */
} /* ...and go round again */
return w;
}

Now you can do:

int max = 100;
int fieldwidth = GetWidth(max * max) + 1; /* add one for spacing */

for ( int i = 0; i <= max; i++)
{
cout << i << setw(fieldwidth) << i*i << endl;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Antoon on

"Pavel R." <razoredx(a)gmail.com> schreef in bericht
news:097444c8-7b0b-4f4e-aaf7-ec4e6988e0e3(a)o31g2000vbi.googlegroups.com...
> Hello, I was wondering how to approach the second part of this
> problem.
>
> Part 1 : Write a program to calculate the squares of int values up to
> 100. The program should write two columns : 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 columns.
>
> Here is the code for that part :
> for ( int i = 0; i <= 100; i++)
> {
> cout << i << setw(6) << i*i << endl;
> }
>
> Part 2 : What happens if we rewrite the previous program to allow
> values up to but not including 1000 but neglect to change the
> arguments to setw? Rewrite the program to be more robust in the face
> of changes that allow i to grow without adjusting the setw arguments.
>
> I don't see how I can pass the length of the integers ( int a = 5;
> a.length or size) into the setw to predict all the possible widths of
> the output.
>
>
> Can anyone help me out? (this is a not a homework problem.)

Hint: log( max_value )

Antoon


From: Francis Glassborow on
Antoon wrote:
> "Pavel R." <razoredx(a)gmail.com> schreef in bericht
> news:097444c8-7b0b-4f4e-aaf7-ec4e6988e0e3(a)o31g2000vbi.googlegroups.com...
>> Hello, I was wondering how to approach the second part of this
>> problem.
>>
>> Part 1 : Write a program to calculate the squares of int values up to
>> 100. The program should write two columns : 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 columns.
>>
>> Here is the code for that part :
>> for ( int i = 0; i <= 100; i++)
>> {
>> cout << i << setw(6) << i*i << endl;
>> }
>>
>> Part 2 : What happens if we rewrite the previous program to allow
>> values up to but not including 1000 but neglect to change the
>> arguments to setw? Rewrite the program to be more robust in the face
>> of changes that allow i to grow without adjusting the setw arguments.
>>
>> I don't see how I can pass the length of the integers ( int a = 5;
>> a.length or size) into the setw to predict all the possible widths of
>> the output.
>>
>>
>> Can anyone help me out? (this is a not a homework problem.)
>
> Hint: log( max_value )
>
> Antoon
>
>

Actually there is something worse lurking in this program. i cannot
increase without limit. To avoid undefined behaviour i must remain less
than the square root of INT_MAX (or a similar value if you elect to use
some other integer type). Given that you might as well write a program
which outputs columns that are sufficiently spaced for the maximum
possible value. Even using C's long long int the colum width will be
relatively small.
From: Barry Schwarz on
On Mon, 16 Nov 2009 20:35:43 -0800 (PST), "Pavel R."
<razoredx(a)gmail.com> wrote:

>Hello, I was wondering how to approach the second part of this
>problem.
>
>Part 1 : Write a program to calculate the squares of int values up to
>100. The program should write two columns : 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 columns.
>
>Here is the code for that part :
> for ( int i = 0; i <= 100; i++)
> {
> cout << i << setw(6) << i*i << endl;
> }

Is the first column right aligned?

>Part 2 : What happens if we rewrite the previous program to allow
>values up to but not including 1000 but neglect to change the
>arguments to setw? Rewrite the program to be more robust in the face
>of changes that allow i to grow without adjusting the setw arguments.

Does "without adjusting" mean
1 - Not changing the source code and recompiling but allowing the
arguments (yes, there should be more than one call to setw) to change?
If so, you can compute the longest length needed for a given maximum
value of i.
2 - Always using the same arguments regardless of the maximum value
of i? If so, you can compute the longest length needed for the
maximum possible value of i.

--
Remove del for email