Jump to content
  • Advertisement
Sign in to follow this  
Dbproguy

ACPP Exercise 4-2

This topic is 3732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I was wondering if anyone could give me a hint on Exercise 4-2 in Accelerated C++. The exercise says "Write a program to calculate the squares of integer values up to 100. The program should write two columns: The first lists the value; the second lists the square of that value. Use setw(described above) to manage the output so that the values line up in columns." The third exercise, however, says "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 was able to make the columns line up for values up to 100 or even 1000, but I'm sure that it will not work for just any squared value. My code is listed below. If you make the for loop go to 50,000 then it won't work, the squares column will shift over one after a while (I'm not exactly sure where though)
#include <iomanip>
#include <iostream>

int main()
{
	int cwidth = 0;
	for (int i = 0; i <= 100000; i++)
	{
		if (i*i < 10) {
			cwidth = 9;
		} else if (i*i >=10 && i*i < 1000) {
			cwidth = 10;
		} else if (i*i >= 1000) {
			cwidth = 11;
		}
		std::cout << i << std::setw(cwidth) << i*i << std::endl;
	}
}

I wouldn't mind a few hints but please don't spoil it and just give me the answer. I'm pretty sure that I shouldn't have that if statement to find the length of the integer. [Edited by - Dbproguy on July 2, 2008 8:50:47 AM]

Share this post


Link to post
Share on other sites
Advertisement
#include <iomanip>
#include <iostream>

int main()
{
int width = 0;
int i2 = 0;
for (int i = 0; i <= 100000; i++)
{
i2 = i*i
if (i2 < 10) {
width = 9;
} else if (i2 >=10 && i2 < 1000) {
width = 10;
} else if (i2 >= 1000) {
width = 11;
}
std::cout << i << std::setw(width) << i2 << std::endl;
}
}


But I haven't quite got what OldGuy said though, I changed the variable cwidth to width to make it more understandable though

Share this post


Link to post
Share on other sites
1) Are we agreed that the last value of 'i' in the loop will have the largest value?

2) Are we agreed that the largest value that is squared will yield the largest resulting square?

3) How can we find the number of digits needed to print a number? (There are two approaches you might consider: a "dry run" printing the number somewhere else, and a mathematical approach using logarithms. Try to figure out the details of each. E.g. where can we "print" something such that it won't show up?)

4) Don't you think you should do something about the width of the first column as well as the second? :)

Share this post


Link to post
Share on other sites
I feel like the stupidest person on earth right now.

#include <iomanip>
#include <iostream>

int main()
{
int i2 = 0;
for (int i = 0; i <= 50000; i++)
{
i2 = i*i;
std::cout << i << "\t" << i2 << std::endl;
}
}

Share this post


Link to post
Share on other sites
printf ("%5d", i2); // prints an integer with spaces to pad it to 5 characters 2 will be "    2" 300 = "  300"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!