Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accelerated C++ Problem setw

1 view
Skip to first unread message

Pavel R.

unread,
Nov 16, 2009, 11:35:43 PM11/16/09
to
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.)

Richard Heathfield

unread,
Nov 17, 2009, 2:07:44 AM11/17/09
to
In
<097444c8-7b0b-4f4e...@o31g2000vbi.googlegroups.com>,
Pavel R. wrote:

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

Antoon

unread,
Nov 17, 2009, 2:49:31 AM11/17/09
to

"Pavel R." <razo...@gmail.com> schreef in bericht
news:097444c8-7b0b-4f4e...@o31g2000vbi.googlegroups.com...

Hint: log( max_value )

Antoon


Francis Glassborow

unread,
Nov 17, 2009, 3:12:48 AM11/17/09
to

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.

Barry Schwarz

unread,
Nov 18, 2009, 5:09:20 PM11/18/09
to
On Mon, 16 Nov 2009 20:35:43 -0800 (PST), "Pavel R."
<razo...@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

0 new messages