| Dimmins (3) | |||
|
Greetings This is my first time posting in any programming forum, so I hope I can pose my problem to you correctly. This is my 6th week in my first programming class, having never programmed before in my life. In order to catch up, I've been trying to write out practice problems that I believe I can solve (cash register machine and the like). This particular problem is finding whether an input number is prime, and if it is not, find the factors of the number and the number of factors. This is not a homework assignment, I assure you, but just a way of testing my abilities. We just covered how to do loops, so we do not know how to do void functions, classes, or anything of the sort. The anstolower function was created by my professor and I understand the concept but could not duplicate it without looking at it. We are using Visual Studio 2010 in class on a double booted 64-bit Windows system on a MacBook Pro. My Question: Line 75 is the problem area. The if (test == halfNum) section is never used. When inputting 100, when the loop gets to test=10, it displays "10 and 10 are factors of 100", then increasing the counted number of factors by 2 rather than 1. Since this problem arose, the downstream information display area (line 112) is also messed up. The code compiles and works perfectly (assuming a valid positive integer is input) for all numbers except for values that are perfect squares. Everything in commented and the two problem areas are set apart by upper cased comments on to the left hand side. Any suggestions on formatting, ways of commenting on the code, or general tips are also appreciated. Thank you for any help!
| |||
|
|
|||
| helios (10126) | ||
sqrt(x) = x/2 (square both sides) x = 1/4*x^2 1/4*x^2 - x = 0 x = (-b +- sqrt(b^2 - 4*a*c)) / (2*a) (+) x = (1 + sqrt(1)) / (1/2) x = 2 * (1 + sqrt(1)) x = 2 * (1 + 1) x = 4 (-) x = (1 - sqrt(1)) / (1/2) x = 2 * (1 - 1) x = 0 The only two numbers whose square roots are equal to their halves are 0 and 4. Try to think of a better way to find if n is the square root of m. Tip: don't overthink it. Use the first check you can think of. One note on style: your commenting is excessive. It makes your code harder to read because all that fluff has to be filtered out. Try to turn it down a notch, especially the "===-=====!=====-==" decorations. Things like that should appear at most once every thousand lines. | ||
|
Last edited on
|
||
| Chervil (806) | |||||||
|
It looks to me as though the square root is calculated as x/2. This is only valid when x == 4. A more appropriate way,
As for the overall style, putting plenty of comments in the code is good, but it's a good idea to keep them concise and meaningful, and not merely repeating what the code itself says. For example this:
could be replaced by this:
| |||||||
|
Last edited on
|
|||||||
| Chervil (806) | |
|
@helios Agreed :) | |
|
|
|
| Dimmins (3) | |
|
Thank you for your responses. After reading over the critiques if((test*test) == num) and while (test <= (num/test)) is what I was looking for for those two sections. Completely didn't need halfNum anymore. In the wee hours of the morning, logic seems to go all squirrelly. @helios I completely did not think to try and write it out mathematically. That helped quite a bit. Writing different parts of the code at different times caused me to comment on everything so I could put it all in the right order (hence the large obnoxious dividing areas). As I get better at coding I think I'll learn what is useful to comment and what is not necessary. @Chervil The small code you posted, along with helios reminding me of the quadratic formula, helped. Like I said above, a lot of it has to do with me remembering what each part of the code actually does. The example is great, so I'll go back and butcher the comments with readability more in mind. Thanks again! | |
|
Last edited on
|
|
| Chervil (806) | |
|
Actually, I tend to put comments in my code while I'm working on it, to remind myself of what the particular section of code is intended to achieve. Sometimes the comments can remain after the code is completed, other times, some pruning or deletion is needed. I think you'll find your style gradually changes as you progress with time and practice. | |
|
|
|
| Dimmins (3) | |
|
That was my intention, but since I was posing a question to people that already understand coding much more thoroughly, I could have removed them beforehand. After pruning the comments, the program is down to 104 lines with no comment taking more than one line and most are off to the side. I definitely believe it. In just the past few weeks the style has changed, especially after today. | |
|
|
|