Program 3 - Realizing a Misanthropic Dream

From Maryville College CS Wiki
Jump to: navigation, search

Introductory Rant

Another long day at the Analytical Society in Cambridge has passed. The foolish men I must surround myself with have been particularly tiring of late, and far less than helpful in the design of complex machines. I seldom think that many men are up to the task of reasoning as I can. "Mr. Babbage" this, and "Mr. Babbage" that, Why do they call after me all the time? And now, I must walk home amid the mob. How I detest the mob.

Charles Babbage

It's not that we could do without them. Oh no, we need their labour, and we need their taxes to fund our higher purposes. What we do not need is their tendency to break my plate glass windows. Oh yes, I know it is they that do it. I have outlined a table of the ways they break and destroy, and yet I have to walk home among them. I have to pass through their parks, with their infernal offspring grabbing at my coat tails. I have to see their "musicians", if one uses the term loosely, playing on the street corners. I know the organ grinder will be there. I hate organ grinder music. It distracts and infuriates me! The suffering and pecuniary effects of these half rated vagabonds astounds the mind. At least I am almost at my door. I turn back and realize that the one thing I cannot stomach is the mob's noise. All the noise, noise, NOISE!

At least my chamber is quiet. I think I will turn to my computations, and find solace in my math. Ah, computation, how wonderfully exact, obedient, and ready to serve. Wait. What's this? My table tells me the log of 1231 is 3.0901?! Outrageous, even a fool knows that log(1231) is 3.0903 if rounded at 4 places. They will not get away with this! These tables affect every aspect of computations, and they have the gall to make an error. No, this will not do. This cannot be trusted to the minds of men, for the computers[1] will make error upon error and their error will grow to catastrophe. I shall build a machine, for it is to the machine we should entrust all that is important. I shall build a difference engine!

Problem

The above rant is my attempt to think like Charles Babbage, a truly embittered man (though he had a few friends with whom he was tender.) He set out to build a table of logarithms via machine, and to this end he designed the difference engine. The difference engine would have been the world's first general purpose mechanical computer. However, his designed proved to be far more involved and expensive than originally anticipated. Also, he had started the design of the analytic engine which would have been more powerful. His parliamentary backers decided to fund the analytic engine instead. The analytic engine also proved too expensive and so it was ultimately abandoned. He did succeed in building parts of both engines, and it has been shown that his machines would have worked. Modern engineers have built his designs, showing that Babbage really did know his stuff!

As another side note, the analytic engine was programmable. A woman by the name of Ada Lovelace
Ada Lovelace
, a long time friend of Babbage, wrote programs for the analytic engine. This has lead to the wonderful bit of trivia that the world's first computer programmer was a woman, even though it is now a male dominated field.

The task which the difference engine was originally designed for was the computation of logarithms. Logarithms were much more important to day to day computation during Babbage's time as they were used to multiply numbers efficiently. This follows from the definition of the log:


  b^x = y


  \log y = x

Suppose we multiply two exponentials of a common base. By the laws of exponents:


  b^x \cdot b^y = b^{x+y}

So if you want to multiply two numbers, you find the log of both numbers, then add them, and then take the antilog using your log table. For instance


  123 * 412

becomes


  e^{\log 123 + \log 412}


  e^{4.8121 + 6.0210}


  e^{10.8331}


   50670

Which is close to the answer. A skilled log table user would be able to extract the last digit, which is 6. 50676 is the exact answer. Note that everything except the addition step is just a table lookup in a book. Thus the more accurate and precise your log tables, the more accurate and precise your multiplications. Also note that here, when I write "log" I mean natural log. Up until the advent of the pocket calculator, log always referred to natural log, and in most engineering circles, it still does.

So now, here's your problem. You are going to realize Charles Babbage's dream, and you are going to construct a program which generates a nice and neat log table. The table will be in four columns, and it will allow for the user to select the number of lines per page, and the number of pages to print. For example, here is a sample run of my program:

Sample Run

Welcome to the Babbage Log Engine

Lines Per Page: 5
Pages: 3
0.1                                                                2.0
----------------------------------------------------------------------
  0.1 -2.3026        0.6 -0.5108        1.1  0.0953        1.6  0.4700
  0.2 -1.6094        0.7 -0.3567        1.2  0.1823        1.7  0.5306
  0.3 -1.2040        0.8 -0.2231        1.3  0.2624        1.8  0.5878
  0.4 -0.9163        0.9 -0.1054        1.4  0.3365        1.9  0.6419
  0.5 -0.6931        1.0  0.0000        1.5  0.4055        2.0  0.6931
----------------------------------------------------------------------


2.1                                                                4.0
----------------------------------------------------------------------
  2.1  0.7419        2.6  0.9555        3.1  1.1314        3.6  1.2809
  2.2  0.7885        2.7  0.9933        3.2  1.1632        3.7  1.3083
  2.3  0.8329        2.8  1.0296        3.3  1.1939        3.8  1.3350
  2.4  0.8755        2.9  1.0647        3.4  1.2238        3.9  1.3610
  2.5  0.9163        3.0  1.0986        3.5  1.2528        4.0  1.3863
----------------------------------------------------------------------


4.1                                                                6.0
----------------------------------------------------------------------
  4.1  1.4110        4.6  1.5261        5.1  1.6292        5.6  1.7228
  4.2  1.4351        4.7  1.5476        5.2  1.6487        5.7  1.7405
  4.3  1.4586        4.8  1.5686        5.3  1.6677        5.8  1.7579
  4.4  1.4816        4.9  1.5892        5.4  1.6864        5.9  1.7750
  4.5  1.5041        5.0  1.6094        5.5  1.7047        6.0  1.7918
----------------------------------------------------------------------


Note how each "page" begins with a header showing the first and last logarithms on the page. Also note that everything is neatly aligned. There is a lot of iomanip action going on in this program. I want you to reproduce this output exactly as it appears here. The exact specification of what you see here is as follows:

  • The pages are exactly 70 characters wide. The "-" separator lines are 70 characters across.
  • There are four columns. The columns are specified as:
    • 5 characters are used for the x value
    • 8 characters are used for the log(x) value
    • Internal columns are preceded by 6 spaces.
    • The last column has no spaces after it.
  • 2 blank lines follow the bottom separator of the page.

Clearly, hand computing test cases here will be highly impractical. Instead, what we should do is just spot check a few of the logarithms.

Hints & Observations

1. You've probably realized that cout lets you do line output, but you can't come back up to a line. This means that each line has to be printed all at once. You'll be doing 4 logarithms per loop iteration. The key here is knowing the starting point of each column.

2. Look at the cmath documentation on http://www.cplusplus.com to see how to compute logarithms.

3. Try to identify patterns in how to determine the first and last log on a page as well as the start of each column. A little bit of algebra on a piece of paper will go a long way here. That goes for the columns too. There is a definite function which determines the start and end of the page, and the value on each column given the number of lines per page, what page you are on, and what row you are on.

4. Use setw and setfill to do your formatting. You shouldn't have to write a loop to do the "---" separator lines! Nor should you write loops which count out spaces.

5. You may want to discuss the design with your peers. Bouncing ideas off of each other can be a big help with problems like this. Do not look at each other's code, but definitely work together on the math bits.

Challenge My solution is just 60 lines of code long, including comments and blank lines. See if you can do this in fewer lines. If you can, I'll add some extra points. That is, of course, provided that you don't skimp on comments to pull it off. You have to write a well-formed stylistically correct program in fewer than 60 lines to get the points. Points will be awarded based on how clever your solution is.

Grading

Category Points
Proper Indentation 5
Proper use of Blank Lines 5
Comments are adequate 15
Variable names 5
Functionality (it works) 70

Footnotes

  1. Prior to 1947, the term "Computer" referred exclusively to a professional person who's sole task was to carry out mathematical computation. This is why ENIAC was called an "Electronic Computer." By the 1960's, the term came to be used almost exclusively for electrical and mechanical devices. The computer profession is dead. No one you will meet will claim to be a computer. (At least not the sane ones.)