Jump to content

Triangle Enumeration

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hello everyone,

I'm really confused on some homework of mine. The code is working; however, its supposed to determine what type of triangle three variable sides create.

Here is the problem out of the book:

(A) Define an enumeration type, triangleType, that has the values scalene, isosceles, equilateral, and noTriangle.

(B) Write a function, triangleShape, that takes as parameters three numbers, each of which represents the length of a side of the triangle. The function should return the shape of the triangle. [Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side]

© Write a program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle.



#include <iostream>

using namespace std;


typedef int side;

enum triangleType {scalene, isosceles, equilateral, noTriangle} shape;

triangleType triangleShape(side&, side&, side&, triangleType&);


int main() {

	side a;

	side b;

	side c;

	int type;


	cout << "Enter side a: ";

	cin >> a;


	cout << "Enter side b: ";

	cin >> b;


	cout << "Enter side c: ";

	cin >> c;



	type = triangleShape(a, b, c, shape);



	switch(type) {

	case 0:

		cout << "This is a scalene triangle." << endl;

		break;

	case 1:

		cout << "This is an isosceles triangle." << endl;

		break;

	case 2:

		cout << "This is an equilateral triangle." << endl;

		break;

	case 3:

		cout << "This is not a triangle." << endl;

		break;

	}


	return 0;

}



triangleType triangleShape(side& a, side& b, side& c, triangleType& shape) {


	if (a == b && b == c)			        return equilateral;


	else if (a == b || b == c || c == a)		return isosceles;


	else if (a != b && b != c && c != a)		return scalene;


	else						return noTriangle;

}

The code works fine and returns the correct values. However, the "noTriangle" value... Is there something I'm missing on this? How would I determine if it's "not a triangle"?

Edited by Jrb, 03 April 2011 - 12:36 AM.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Input -1, -1, and -1. This will result in your method returning "equilateral", even though a triangle with negative-length sides does not exist. Your conditions did not consider negative or 0 values being sent to side.

Also, I wouldn't typedef int as "side", that's just confusing and sending a reference to your function is not, in my opinion, a good idea either, just send copies. It's the same amount of processing power (both 32 bits to copy), and it assures users of the function that it won't modify their int. You don't need the triangleShape function to take a triangleType value, as it does nothing with the argument in the method and simply returns a new triangleType value. Your case statement is also not taking advantage of the enumeration you have set, which would look like this:
	switch(type) {

	case scalene:

		cout << "This is a scalene triangle." << endl;

		break;

	case isosceles:

		cout << "This is an isosceles triangle." << endl;

		break;

	case equilateral:

		cout << "This is an equilateral triangle." << endl;

		break;

	case noTriangle:

		cout << "This is not a triangle." << endl;

		break;

	}

Wow I changed my sig!

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
additionally, if the user enters lengths 1, 2, 3, you won't have a triangle (the points are colinear), and 1, 2, 4 is simply impossible.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
ZekeDragon,

I agree with what you said; however, I have to have the triangleShape function as part of the assignment. My professor likes to see stuff that we've talked about in our assignments (such as typedef and refrencing). I know it's not great looking, but that's just the way she wants them. I do get what you and WingedPanther are saying though, and I will fix this. Thanks :P

#5
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks for your advice guys. I added in some new code:

if (a <= 0 || b <= 0 || c <= 0) {

		cout << "Cannot have a negative or null side." << endl;

		exit(1);

	}

And

switch(type) {

	case noTriangle:

		cout << "This does not represent a triangle." << endl;

		break;

	case equilateral:

		cout << "This is an equilateral triangle." << endl;

		break;

	case isosceles:

		cout << "This is an isosceles triangle." << endl;

		break;

	case scalene:

		cout << "This is a scalene triangle." << endl;

		break;

	}

	return 0;

}

And finally

if  (a + b < c || b + c < a || a + c < b)  return noTriangle;

Works great now.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users