Sign in to follow this  
ugoff

Why do I have so much... code?!

Recommended Posts

hey guys, could you help me out with something please. im in class right now, and they just had us write a basic calculator. oh, by the way, C++ ;-). so anyway, i get done with my calculator (and am very proud, dont know why). so i start looking at some of my peers' monitor's, and notice that they have SO much less code than me. so what im asking, is if someone could re-write my program to use less lines and trim it down, but still have the same functionality. also, just use the syntax thats in the program, nothing fancy like arrays, pointers, if/else, when... etc. you get what i mean. so anyway, here's my code, please fix it!!
/***************************************************************************************************************
Simple Calculator
August 03, 2004
By: Chris Egner
***************************************************************************************************************/

#include <iostream.h>

float multiply(float multi1, float multi2)
{
	float multi_answer;
	multi_answer = multi1 * multi2;
	return multi_answer;
}

float square(float squ1)
{
	float num_squared;
	num_squared = squ1 * squ1;
	return num_squared;
}

float subtract(float val1, float val2)
{
	float sub_answer;
	
	sub_answer = val1 - val2;
	return sub_answer;
}

float add(float val3, float val4)
{
	float add_answer;
	
	add_answer = val3 + val4;
	return add_answer;
}

int main()
{
	float num1, num2, num3, num4, multi1, multi2, square1;
	float sub_solved;
	float add_solved;
	float multi_answer;
	float square_answer;

	cout << "Ok, first, we will subtract two numbers" << endl;
	cout << "First Number? ";
	cin >> num1;
	cout << "Second number that is smaller than the first? ";
	cin >> num2;

	sub_solved = subtract(num1, num2);
	cout << num1 << " - " << num2 << " is equal to: " << sub_solved << endl;
	cout << endl;
	cout << "Ok, on to addition. First Number to add? ";
	cin >> num3;
	cout << "Alright, second number? ";
	cin >> num4;
	
	add_solved = add(num3, num4);
	cout << num3 << " + " << num4 << " = " << add_solved << endl;
	cout << endl;
	cout << "Ready to multiply!? First number please :-): ";
	cin >> multi1;
	cout << "Second number for multiplying? ";
	cin >> multi2;
	multi_answer = multi1 * multi2;
	cout << multi1 << " * " << multi2 << " = " << multi_answer << endl;
	cout << endl;
	cout << "Isnt multiplication great!! Ok, lets try squaring a number now: ";
	cin >> square1;
	square_answer = square(square1);
	cout << square1 << " squared = " << square_answer << endl;
	return 0;
}

THANK YOU!!

Share this post


Link to post
Share on other sites
There are always multiple answers to a problem, as long as your way works, then there is no real reason to worry about code length; especially since it's for learning.

But my guess is your friends are just directly returning the answer instead of using temporary variables and a seperate expression.

ie. return (multi1 * multi2);

Good luck.

Share this post


Link to post
Share on other sites
Maybe you could ask your instructor for hints as to what you could do better. I don't think that our doing your homework for you would teach you anything. And we're certainly not going to help you cheat on an assignment, sorry.

Share this post


Link to post
Share on other sites
Also, since all of the operations here are equivalent to C++ operators (except squaring, but it is just as simple), there's literally no need to define functions for every operation. Oh yeah, and there's nothing wrong with reusing variables to perform the same operation.


/***************************************************************************************************************
Simple Calculator
August 03, 2004
By: Chris Egner
***************************************************************************************************************/


#include <iostream>

using namepace std;

int main()
{
float num1, num2;

cout << "Ok, first, we will subtract two numbers" << endl;
cout << "First Number? ";
cin >> num1;
cout << "Second number that is smaller than the first? ";
cin >> num2;
cout << num1 << " - " << num2 << " is equal to: " << (num1 - num2) << endl;
cout << endl;
cout << "Ok, on to addition. First Number to add? ";
cin >> num1;
cout << "Alright, second number? ";
cin >> num2;
cout << num1 << " + " << num2 << " = " << (num1 + num2) << endl;
cout << endl;
cout << "Ready to multiply!? First number please :-): ";
cin >> num1;
cout << "Second number for multiplying? ";
cin >> num2;
cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl;
cout << endl;
cout << "Isnt multiplication great!! Ok, lets try squaring a number now: ";
cin >> num1;
cout << num1 << " squared = " << (num1 * num1) << endl;
return 0;
}




Of course, the objective might have been to use functions for each operation so your instructor might have a problem with this. However, it should be obvious how redundant these functions really are, and functions are meant to reduce redundant code...

@Everyone else: I realize the homework policy, but in this situation (given the simplicity of the problem) I thought that a concrete example would be more informative for the OP.

Share this post


Link to post
Share on other sites
Quote:
Original post by ugoff
lol, dont worry fruny. its not homework, and im not cheating. im just trying to fix my code, not have someone write the program for me


I'm not worried - it's your tuition money, not mine.

Share this post


Link to post
Share on other sites
lol, and how did you know im from Digipen??

and whoever quoted me... yes, i know what i was saying when i wrote that. what i meant/mean is that i have WRITTEN the program. i just need someone to help me trim it up and shorten it, i just didnt know how to do that.

and thunderhawk, thank you very much. i already knew there was that way to do it as well, it's just that, like you said, the point of the lesson was to do it using functions

Share this post


Link to post
Share on other sites
Here's step one to shrinking your code. We can help you through it, but we won't write it all. Do this and then show what you have to see if there are further possibilities.

Here is one of your functions now:

float multiply(float multi1, float multi2)
{
float multi_answer;
multi_answer = multi1 * multi2;
return multi_answer;
}


It is 3 lines that do this:

1. Create a variable to hold the answer.
2. Assign the answer to the variable.
4. Return the answer from the function.


Well, you don't really need a temp variable in this case. All this function has to do is take the arguments passed to it and return the result. You could do this:


float multiply(float multi1, float multi2)
{
return (multi1 * multi2);
}



So you can shrink all your functions that way. You see (multi1 * multi2) produces a float, which is then returned :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
You do realize that there are people from Digipen who read these forums?


Is DigiPen hated or something? I was actually considering that school but, if it's a joke, then I'll look elsewhere!

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
You do realize that there are people from Digipen who read these forums?


He's not from Digipen, he's 14-15 according to his profile.

Anyhow, Your code looks just fine for a beginner and very clear. Anything homework related is frowned upon(and against the rules) around here, unless its general help IE-don't ask for a solution. Just so you know.

Share this post


Link to post
Share on other sites
oh i get it, so when somebody posts here, the mods and admins can see their IP addresses as well?? and rhaal, that is freaking awesome. thank you so much for showing that to me. i never thought of it, and it makes things so much easier

EDIT- oh, i AM coming from Digipen, but im not a full time student. im just here for a couple weeks doing a workshop

Share this post


Link to post
Share on other sites
Quote:
Original post by ugoff
lol, and how did you know im from Digipen??
...


Washu Moderator Member since: 3/24/2001 From: Kanemitsu

The bold part makes me see things in a new light!!!

While the point of the exercise was to demonstrate functions, I think the choice of material for the exercise was poor. One of the primary goals of learning to code should be learning to code correctly, and in having a lesson where you define such simple operations as functions will tend to ingrain in students...shall we say bad practices? Now, that's not saying that you will fall into that trap, but keep in mind that most classroom examples are "wrong" in industry. See the shapes example for instance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by ugoff
lol, and how did you know im from Digipen??


"Your computer may be broadcasting an IP address." [rolleyes]


Ah, so he must be a summer-session kid... my bad.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Now, that's not saying that you will fall into that trap, but keep in mind that most classroom examples are "wrong" in industry. See the shapes example for instance.


Also, at least according to what I've heard (I don't tend to read books on programming, but just occasionally Google things) most textbook examples are "wrong" according to the compiler. ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Now, that's not saying that you will fall into that trap, but keep in mind that most classroom examples are "wrong" in industry. See the shapes example for instance.


Which shapes example? The one in every book about inheritance? If so please explain why, because I've accepted the example so far!

Share this post


Link to post
Share on other sites
Just examine your friend's code and see what he/she did differently. BTW, how can we be helping him with homework when he already has a working (and readable) calculator program.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this