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

Started by
19 comments, last by Onemind 19 years, 9 months ago
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!!
Silence! I'm Psychoflexing...
Advertisement
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.
Firstly - size isn't important.

Secondly - your code is probably more readable and correct than your classmates. Don't worry about it.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
score :-). thank you guys, i appreciate it
Silence! I'm Psychoflexing...
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
Silence! I'm Psychoflexing...
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 CalculatorAugust 03, 2004By: 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.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Quote:Original post by ugoff
so what im asking, is if someone could re-write my program


Quote:Original post by ugoff
not have someone write the program for me


...
Half the people you know are below average.Trogdor the Burninator
You do realize that there are people from Digipen who read these forums?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement