Teach Me The Ways Of The SeePlusPlus

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 3164 Views - Last Post: 24 January 2011 - 12:01 AM Rate Topic: ***-- 2 Votes

#1 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 04:54 PM

*
POPULAR

Hai guise! This is my first C++ program for class. Have I sort of got the style right? The code compiles and works perfectly, but I'm trying to get the hang of C++'s style, documentation, etc. Am I doing it right, or can you tell I miss Java? Thanks!

/*
 * Author: NeoTifa
 * Class: My Class
 * Assignment: Lab 1
 *
 * This assignment is to translate some Java code to C++ code. The goal is to find a square root using some Newton method or something.
 */

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double getValue();
double getInitialGuess(double value);
double approximateSqrt(double value);

/*
 * Main member function
 */
int main() {
	double x = getValue();
	double xSqrt = approximateSqrt(x);

	cout << fixed << setprecision(6);
	cout << endl << "sqrt(" << x << ") = " << xSqrt << endl;
	cin.get();
	return EXIT_SUCCESS;
} // End main method

/*
 * getValue member funcion
 * 
 * Pre-Conditions: The value is assumed to be a number. 
 * Post-Conditions: Returns a double value representing the user's input.
 */
double getValue() {

	double value;

	cout << "Enter value (<0) whose square root will be computed: ";
	cin >> value;

	while(value <= 0) {

		cout << "Invalid value " << value << " <= 0" << endl;
		cout << " -- try again" << endl;
		cout << "Enter value (>0): ";
		cin >> value;

	} // End while loop

	return value;

} // End getValue member function

/*
 * getInitialGuess member function
 * 
 * Pre-Conditions: Takes in the value returned by getValue, assumes a legit input.
 * Post-Conditions: Returns half of the value given.
 */
double getInitialGuess(double value) {

	return value / 2.0;

} // End getInitialGuess member function

/*
 * approximateSqrt member function
 * 
 * Pre-Conditions: Takes in value returned from getValue, assumes a legit input.
 * Post-Conditions: Returns the square root based on the equation, after displaying several guesses.
 */
double approximateSqrt(double value) {

	const double epsilon = 0.00001;
	double x = getInitialGuess(value);

	cout << fixed << setprecision (6);
	cout << "sqrt (" << value << ") = " << x << endl;

	while(abs(value - x * x) > epsilon) {

		double tx = x;
		x = 0.5 * (x + (value / x));
		cout << "sqrt (" << value << ") = 0.5  * (" << tx << " + (" << value << " / " << tx << ")) = " << x << endl;
		
	} // End while loop

	cin.get();
	return x;

} // End approximateSqrt member function


Is This A Good Question/Topic? 7
  • +

Replies To: Teach Me The Ways Of The SeePlusPlus

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 05:06 PM

I think it is fine Just keep going!!
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 05:53 PM

cout << "Enter value (<0) whose square root will be computed: ";

I think you want >0 there, don't you?
Was This Post Helpful? 1
  • +
  • -

#4 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 08:45 PM

Attached Image
Was This Post Helpful? 0
  • +
  • -

#5 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 09:03 PM

Looks mighty fine. A style thing may be not naming the variable in the function prototype: double getInitialGuess(double value); can just be double getInitialGuess(double);.

Either is perfectly legal and acceptable, I just don't think I see many people putting anything other than the type in the prototype.
Was This Post Helpful? 1
  • +
  • -

#6 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 09:49 PM

Thanks! <3
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 09:56 PM

*
POPULAR

Is no one going to say anything about the comments? Because they are horrendous. Sorry, it's true. Because they are excessive, meaningless noise. With some wrong statements in them.

> Main member function

- Main is not a member function. A member function is a function that is part of a class. This is a free function.
- I don't think you need to write a comment that you've got a main function. Because the next line is int main() { .
- Ditto for everywhere else in your code.

> } // End main method

Thanks. I was really struggling about that } over there. Seriously, stop with this. If you indent your code properly, a closing brace ends the function. Ditto everywhere else.

/*
 * getValue member funcion
 * 
 * Pre-Conditions: The value is assumed to be a number. 
 * Post-Conditions: Returns a double value representing the user's input.
 */




There's a few problems here. Aside from the member function I mentioned earlier. Your big problem is that you don't know what a precondition or a postcondition is. They specify invariants.

You can't have a precondition on value because it doesn't exist before the statements in the function. There's a reason it's called "pre". Here's an example of code that can have a precondition:

// Pre: dest and src point to valid buffers, 
// Pre: dest[begin+len-1] is valid
// Pre: src buffer size >= len - begin
int concatStr(char *dest, char *src, size_t begin, size_t len)
{ //...



Those are examples of preconditions you'll see in code I write. If someone uses that function, they need to know all those three conditions on their arguments, because violating any one of them is fatal.

The preconditions above are extremely important. Telling me you have a double that is a number is ridiculous. I assume unless you are a braindead programmer, you respect the type system. It's also not a precondition because it doesn't apply before the function is called...

Your postcondition is equivalently bad. It's not a postcondition, it's telling me what your horribly named function does. Which is an apology for your poor choice of name. It's also telling me that your function declared to return a double...returns a double. Holy smoke, an earth shattering revelation! Here is an example of a postcondition that is useful.

// Post: return pointer to valid aligned buffer of requested size or null constant
// Post: valid pointer can and must be freed with alignedFree
void* alignedMalloc(size_t sz, int align)
{ //...



If I used someone's allocator function, that's nice to know. Telling me you return a double is not. I can read the declaration.

Fix your function names and/or code design. Make your comments meaningful. You have too much code noise.

tldr; Remove all your comments in your code and it will be better.

This post has been edited by Oler1s: 09 January 2011 - 09:59 PM

Was This Post Helpful? 8
  • +
  • -

#8 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 10:57 PM

:x

I think I already said it wasn't my code, but I took the profs Java code and translated. He said I needed those comments and pre/post conditions. :( What pre/post conditions can I really write for this?! XD
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Teach Me The Ways Of The SeePlusPlus

Posted 09 January 2011 - 11:23 PM

Quote

I think I already said it wasn't my code, but I took the profs Java code and translated.
Is translation going to be the teaching style of your professor? That's horrible. If you're going to learn a language, you need to learn how to write code idiomatic in that language. Although I'm sure the "copy assignment solution from internet" crowd will be just fine with this style of teaching.

Quote

He said I needed those comments and pre/post conditions.
Did he write them? In which case, I wish you luck in surviving this equivalent of a lobotomy.

Quote

What pre/post conditions can I really write for this?!
Nothing. It's like telling a joke and then explaining the joke. The problem isn't the wording of your explanation. It's the existence of it.
Was This Post Helpful? 2
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Teach Me The Ways Of The SeePlusPlus

Posted 10 January 2011 - 06:06 AM

If those pre/post-conditions came from your teacher then you need to get these links in front of them so they can do better and not mislead the class
http://en.wikipedia....ign_by_contract
http://en.wikipedia....ki/Precondition
http://en.wikipedia....i/Postcondition

As Oler1s has said the pre/posts your are giving are not great but for a classroom exercise these might make at least some kind sense for getInitialGuess(double)
Pre: value passed in must be >0
Post: value returned is >0

Having said that then you might choose to enforce them by checking the contract has been satisfied and stopping the program if it has been violated.

Can you see that your function would result in strange outcomes if the programmer calling the function passed it a negative number?
You are expecting to always get a positive value greater than zero.
But you do nothing to ensure that is the case in the function and you provide no guidance for anyone calling your function that that is a vital requirement for your function to provide a useful output for the intended purpose.
So that's the precondition.

In that trivial function it is impossible for the pre not to lead straight to the post but just imagine a case where you do some more complex calculations and need to enforce that the outcome is valid.
Imagine your output must be positive and yet when you check just before returning it is negative.
What will you do?
Exit the program? Change the returned value to 1? or 0?
These could each be the right answer in a given case.
Enforce whatever answer you come up with and tell the programmer using the function what you will do if and when it occurs.
In the function here I would suggest that returning 1 would be the right answer in such a case (for reasons I hope are too clear to need explaining) so then the precondition you are using would be false but mine would be true.

Does all this make any sense?

This post has been edited by janotte: 10 January 2011 - 06:14 AM

Was This Post Helpful? 2
  • +
  • -

#11 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 10 January 2011 - 03:01 PM

Yes. I wrote the comments. >_> Something like this just doesn't need commented imho, but hey, I gotta do it. Anywho, thanks for the links. Our professor last quarter never explained pre/post cond. so I was just kinda winging it, and she said my documentation was beautiful. :(
Was This Post Helpful? 0
  • +
  • -

#12 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4485
  • View blog
  • Posts: 7,813
  • Joined: 08-June 10

Re: Teach Me The Ways Of The SeePlusPlus

Posted 10 January 2011 - 04:57 PM

*
POPULAR

On professor-required comments:

Professors frequently have fucking stupid comment and style requirements. Some want you to document every stupid line, some want something else equally ridiculous. Same for spacing and formatting. Some don't care; some are jerks about it.

The best advice there is to just roll with it. You've got pros here at DIC to teach you the real-world way, should they be so inclined. Professors hate to be challenged, so just suck it up, write shit code the way they want, and move on.

I knew a guy who got a B in an intro to Java class because he refused to format his code the way the instructor wanted. She wanted braces on new lines, he wanted them on the same line as the method/if/whatever. After she marked him down once, he started removing all whitespace from his code files before he turned his work in. And in the end, all it got him was a reduced grade.

Really, professors try to get you to comment stupid easy shit, because they think if you get in the habit of that now, later you'll comment the hard important shit. Which is stupid, because you'll just comment on the "what" and not the "why", because you never had to learn to comment about "why".

I document the "whys", not the "whats" (unless there's something particularly funky), because I can assume any competent programmer can look at what a line of code says and know what it does. Why it does that may be something else entirely. The "why" explains what's really going on in your program.

Whoa, sorry for the rant. My first post in the C++ forum and I go on a commenting rant.
Was This Post Helpful? 9
  • +
  • -

#13 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2670
  • View blog
  • Posts: 15,713
  • Joined: 24-September 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 10 January 2011 - 08:01 PM

Oh no, that's fine. ^__^ I'm just trying to get used to the whole C++ culture thing. Thanks.
Was This Post Helpful? 0
  • +
  • -

#14 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Teach Me The Ways Of The SeePlusPlus

Posted 11 January 2011 - 05:43 AM

While Oler1s answer was a little harsh, he's 100% correct all the way.
Also, if we're going to tell tales of formatting, one guy I lived with handed in his source code with all the variable names changed (consistently - it would compile) to names of different fruit.
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Teach Me The Ways Of The SeePlusPlus

Posted 11 January 2011 - 10:59 AM

View PostinsertAlias, on 10 January 2011 - 05:57 PM, said:

I knew a guy who got a B in an intro to Java class because he refused to format his code the way the instructor wanted. She wanted braces on new lines, he wanted them on the same line as the method/if/whatever. After she marked him down once, he started removing all whitespace from his code files before he turned his work in. And in the end, all it got him was a reduced grade.


Heh. That'll show 'em.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2