HW Help for a beginner!

Write your question here.
I'm having trouble producing this output. His solution is only 60 lines of code and he challenged us to complete it in fewer lines for extra points suffice to say we do not skimp on sufficient comment. CS111, basically an intro to programming. I've got some code down, but I want to produce the code and am not sure how to approach it in terms of saving lines. If someone could perhaps nudge me without giving source code, other than say, an if loop would do, or, if functions could help there. The output should look like this
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
----------------------------------------------------------------------

For the specific assignment, check this link out https://cs.maryvillecollege.edu/wiki/Intro_CS_I/fall2019/prog3

I have recently rekindled my love for computers for this class and recently had to drop my CS minor but I really need insight.
Thanks so much!

I am unsure if this is the right approach. The full source code is as follows.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
        /*col is number of columns, row is number of rows, pages is num of pages lines is num of lines */
        int n, col, row, pages, lines;
        double value, x, entries, lentry; //value of log(x), x for use of log(x)

        //Display welcome message
        cout << "Welcome to the Babbage Log Engine" << endl
             <<  endl                               << endl;
        //get lines and page numbers
        cout << "Lines Per Page: "                  << endl;
        cin  >> lines;
        cout << "Pages: "                           << endl;
        cin  >> pages;

        //number of columns and rows
        col = 4;
        x = 0.1;
        //calculate n
        n = lines * col;

        //calculate entries
        entries = log(x);

        //display results of user input
        do
        {
                cout << entries << setw(70) << lentry           << endl;
                cout << setw(70) << setfill('-') << entries     << endl;

                //calculate entries
                entries = log(x)
                //increase log to be computed
                x++;
        }  while(entries <= lines);
}
I'd start with code that just prints out x and log(x) values. Then take this hint to heart:

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.
Hello MrBaggins,

You say The full source code is as follows. Not really. If it does not compile it is not the full code.

The only thing I can see to save on lines of code would be things like:
cout << "Lines Per Page: "; cin >> lines;. Also by loosing the "endl" at the end of the first part you will have the input follow the prompt and not be on the second line.

The compiler ignores comments along with white space and blank lines, so I do not consider them lines of code.

Other than adding code for the output to display what you have shown I think you should be good.

Someone who knows more about log tables will have to comment on that part of the code. Its not that I can not figure if the program is working. Its not an area of math I know much about.

Hope that helps,

Andy
Here's where I'm at so far.
I can calculate logs just fine. I'm not sure how to handle the formatting to be honest in my do .. while loop. I'm beginning to think I need to nest this or switch to a for loop.
cin >> lines;
cout << "Pages: " << endl;
cin >> pages;

//number of columns and rows
col = 4;
x = 0.1;
//calculate n
row = lines;

//calculate max entries
max = col * lines;

//calculate entries
entries = log(x);

//set row to 1
row = 1;

//display results of user input
do
{
cout << fixed << setprecision(5) << x << fixed << setw(70) << lentry << endl
//print the page seperator
<< setw(70) << setfill('-') << endl;
cout << setw(70) << x << fixed << setprecision(8) << entries << endl;

//cout << fixed;
//calculate entries
//entries = log(x);

//increase rowcount
row++;

//increase x
lentry = x++;
cout << setw(70) << setfill('-') << endl;
} while(row <= lines);
} cin >> lines;
cout << "Pages: " << endl;
cin >> pages;

//number of columns and rows
col = 4;
x = 0.1;
//calculate n
row = lines;

//calculate max entries
max = col * lines;

//calculate entries
entries = log(x);

//set row to 1
row = 1;

//display results of user input
do
{
cout << fixed << setprecision(5) << x << fixed << setw(70) << lentry << endl
//print the page seperator
<< setw(70) << setfill('-') << endl;
cout << setw(70) << x << fixed << setprecision(8) << entries << endl;

//cout << fixed;
//calculate entries
//entries = log(x);

//increase rowcount
row++;

//increase x
lentry = x++;
cout << setw(70) << setfill('-') << endl;
} while(row <= lines);
}
In the following line, I have trouble when I try to setfill(entires)
cout << setw(70) << x << fixed << setprecision(8) << entries << endl;
Thank you for the clarification about full source code Handy Andy!
Hello MrBaggins,
I worked this up quickly to give you an idea of how it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
std::cout << std::fixed << std::showpoint; // <--- Only needs done once.
	
do
{

	std::cout << std::string(77, '-') << std::endl;

	std::cout << std::setprecision(5) << x << std::string(5,' ') << std::setw(10) << lentry << '\n';
		//print the page seperator
	//std::cout << std::setfill('-') <<std::setw(70) << ' ' << std::endl;
	std::cout << std::string(77, '-') << '\n';

	std::cout << std::setw(70) << x << std::setprecision(8) << entries << std::endl;

Line 1 only needs to be done once and will affect all "cout" statements until it is changed.

The rest is spread out to make it easier to understand.

Line 6 used the "std::string" to do the same thing as you are trying to do with "setfill", just easier.

To use "setfill" you would need something like this:
std::cout << std::setfill('-') <<std::setw(70) << '-' << std::setfill(' ');
First you change the fill character. Then the "setw" acts upon what follows. In this case the minus sign, i.e., print one character and the "setfill" does the other 69. Then when you are done you need to put the fill character back to a space.

The same is true for "setw". It only acts on what follows.

The other thing you should notice is that I ended most lines with "\n" and not "endl". You do not need an "endl" at the end of every "cout" statement. Usually the new line ("\n") will work fine and I use the "endl" on the last line of output. This works the same whether it is to standard out (the screen) or to a file.

I think the do/while loop could work, but the while condition would need to be based on "pages" not the "row" and "lines" that you are using.

I need to call it a night and take a fresh look at this in the morning.

Hope that helps,

Andy
Give my regards to Melilot the Younger when you get back to the Shire, to her and her new husband.
I shall <3 LOL
Thanks handy andy for your cohesive response. I'm going to definitely be working on this today after I study for my prob stats exam, the problem is, I have CS111 directly after that class. I think I am going to create a for loop and nest a while loop or 2 inside to govern the behavior of each page.

Will keep everyone posted!!

I can't thank you guys enough. One condition of the course is I seek help on a help site, which this what I assume this is, is to cite it within the program. And to NOT use source code provided.

What you provided is a good example of how to use those specific manip functions which I needed help with.

Thanks again!!!
less lines of code is a conceit as much as anything. Usually the code isn't better-- its harder to read, harder to modify/fix/debug/etc and rarely runs faster (it runs faster if you cut out WORK DONE not if you cut out LINES OF CODE). Those 2 are rarely related if you are doing the same algorithm/approach etc.

that said, you can lump stuff.
x++; //wasted. somewhere nearby this you almost always USE x for something, even if its just a print statement or whatever.
so write
cout << x++ <<...
same as cout << x; x++; 2 lines of code
or
foo(x ++); //etc same as foo(x); x++ 2 lines of code.

cout <<blah;
cout << blah2; //combine these into 1 statement.

initialize variables to a default.
instead of int rows; rows= 1; for 2 statements, say int rows = 1. You can chain all the same type on one line of code with initialized values.

a quick application of what I just said:
1
2
3
4
5
6
7
8

 entries = log(x);  //reorder to remove this line. 
      do
        {
                entries = log(x++)
                cout << entries << setw(70) << lentry<< endl  << setfill('-') << entries << endl;
        }  while(entries <= lines);


again, the more you condense it, the uglier it will be. But these are some ways to shave off LOC. Post your latest when you get a running, working program, and we can cut a few lines off somewhere, most likely.
Last edited on
again, the more you condense it, the uglier it will be.

Not necessarily, sometimes "condense" means removing redundant/duplicate code which tends to improve code maintainability/readability.

I do agree though that placing multiple statements on one line just for the sake of reducing lines is usually a poor practice and IMO often tends to obscure the purpose of the code.
Hello MrBaggins,

I will start with the variables:
1
2
int /*n{},*/ col{ 4 }, /*row{},*/ pages{ 2 }, lines{ 5 };
double /*value{},*/ x{ 0.1 }, max{}/*, entries{}, lentry{}*/; //value of log(x), x for use of log(x) 

Those that have a comment on them are either never used or not needed. "row" and "lines" are basically the same. You only need one not both.

The way I did the program "entries" was not needed and "lentry" is never used.

By cutting out these unused variables you can cut the lines of code that set their value.

Also the variables "sol", "pages", "lines" and "x" are given a value to start with, so the lines that set "col" and "x" are not needed.

The variables "pages" and "lines" have these values and when you comment out the lines that input these values from the user you can concentrate on the rest of the program with out having to enter values for these variables every time the program is run.

When the program is working the way you want change "pages" and "lines" to empty {}s and uncomment the input.

You can use the do/while loop. Inside the loop I used a for loop to print the number of lines needed and the "cout" statement printed all the columns at one time.

After that the only thing needed is x += 2.0;.

In the while condition I used "pages" for this loop. By writing the condition as while (--pages > 0);. You subtract one from "pages" before you check it against (0) zero. This is also a way of saving a line of code.

With a little bit of work the program produces:

              Welcome to the Babbage Log Engine
------------------------------------------------------------

 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.6 - 4.5
------------------------------------------------------------
2.6   0.9555    3.1   1.1314    3.6   1.2809    4.1   1.4110
2.7   0.9933    3.2   1.1632    3.7   1.3083    4.2   1.4351
2.8   1.0296    3.3   1.1939    3.8   1.3350    4.3   1.4586
2.9   1.0647    3.4   1.2238    3.9   1.3610    4.4   1.4816
3.0   1.0986    3.5   1.2528    4.0   1.3863    4.5   1.5041
------------------------------------------------------------



 Press Enter to continue:


Of course between the line of dashes and the 0.1 - 2.0 would be where the user enters information.

FWIW my IDE says that there are 68 lines of code, but that is before the unused lines of comments and code are removed. If I calculated right it dropped down to 52 or maybe it could be 50.

Hope that helps,

Andy
Topic archived. No new replies allowed.