# Generating a triangle from integers

I've put together an algorithm for an assignment. I've done my best to try and keep it to a professional and readable standard. I'm posting it here so that I can get some feedback and suggestions on what it's like and whether I could improve the algorithm in some way.

  /***** Algorithm

Purpose: To create a C program which takes 3 integers and produces a result that shows which triangle(If valid) they have chosen.*****/

Variable Declaration: sideA =  integer
Variable Declaration: sideB =  integer
Variable Declaration: sideC =  integer
Character Declaration = ch

PRINT -- " Lets explore triangles! Please insert a value for side A of your triangle \n"
PRINT -- " Ranging from 1-15cm"

PRINT -- " Now insert a value for side B of your triangle ranging from 1-15cm.\n"
INPUT -- sideB

PRINT -- "And finally, insert a value for side C of your triangle ranging from 1-15cm.\n"
INPUT -- sideC

IF (sideA || sideB || sideC <=0)
PRINT "  You cannot have a triangle with any side having a value of 0.\n"
ELSE
IF(sideA || sideB || sideC >15) THEN
PRINT " Please insert a value between 1cm - 15cm only."
ELSE
IF (sideA AND sideB == sideC OR sideB AND sideC == sideA OR sideC AND sideA == side C ) THEN
PRINT "Your input creates a valid EQUILATERAL triangle.\n"
ELSE
IF (sideA==sideB OR sideB==sideC OR sideC==sideA )
PRINT " Your input creates a valid SCALENE triangle.\n"
ELSE
IF
PRINT " Your input creates a valid ISOSCELES triangle.\n"
ELSE
PRINT " You have inserted invalid range of values, as a result your triangle is Invalid.\n"
PRINT " Please restart the program by running it again and insert valid values in order to check what triangle you would get. Goodbye."
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
END PROGRAM


You may notice I did a char declaration but didn't use it in the rest of the algorithm. That is because I'm trying to expand on the algorithm and testing conditions. One of the things I want to do is insert a loop condition where unless a user insert a valid integer the user will be prompted to insert a valid integer, once it's inserted, the program continues on. This happens on 3 occasions at the beginning.

Also I have made the integer between a range of specific numbers. How would I be able to improve upon the algorithm so that a user can insert any number, and one of the conditions upon that is that one side of a triangle cannot be longer than the other two sides otherwise the triangle is invalid.

If a user inserts, for example

• sideA as 10
• sideB as 20

then sideC cannot be more than 30. Likewise the condition to be where sideA cannot be more than sideB and sideC combined, as well as sideC and sideA not been higher than sideB.

In my code I've currently got it set to make sure a user can only insert a valid integer, and thus will not allow a user to insert any character except an integer.

At the end of the algorithm and also in the code, I've got an else-if statement in case the user inserts anything invalid. But since I am trying to make it error proof from the beginning, what would I have to add so that I can remove the final else-if statement.

And finally I want to add a section to the algorithm where the result is "After user has gotten an answer, the user is then asked to either try again or simply exit."

The code is pasted below if you wish to compile. Check it out and make some suggestions based on what I'm trying to make the program do.

#include <stdio.h>
int main()
{
/*** Declaring triangle variable sides ****/

int sideA;
int sideB;
int sideC;
char ch;

printf("Lets explore triangles! Please insert a value for side 'A' of your triangle.\n");
printf(" Ranging from 1-15cm.\n");
while(scanf("%d", &sideA) != 1)
{
printf("You inserted an incorrect value. Please insert a number ranging between 1-15cm, and try again.\n");
while ( (ch=getchar()) != '\n' );
}

printf(" Now insert a value for side 'B' of your triangle ranging from 1-15cm.\n");
while(scanf("%d", &sideB) != 1 )
{
printf("You inserted an incorrect value. Please insert a number ranging from 1-15cm, and try again.\n");
while ( (ch=getchar()) != '\n' );
}

printf(" And finally, insert a value for side C of your triangle ranging from 1-15cm.\n");
while(scanf("%d", &sideC) != 1 )
{
printf("You inserted an incorrect value. Please insert a number ranging from 1-15cm, and try again.\n");
while ( (ch=getchar()) != '\n' );
}

/*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

if(sideA <=0 || sideB<=0 || sideC <=0)
{
printf(" You cannot have a triangle with any side having a value of 0.\n");
}
else
if(sideA>15 || sideB>15 || sideC >15)
{
printf("Please insert a value between 1cm-15cm only\n.");
}
else
if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
{
printf(" Your input creates a valid EQUILATERAL triangle.\n");
}
else
if( (sideA == sideB) || (sideB == sideC) || (sideC == sideA) )/*** Code to determine ISOSCELES TRIANGLE***/
{
printf("Your input creates a valid ISOSCELES triangle.\n");
}
else
if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
{
printf("Your input creates a valid SCALENE triangle.\n");
}
else
{
printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
printf("Please restart the program by closing it and opening it again to retry\n.");
printf("Goodbye.\n");
}
return(0);
}


NOTE: You may find there are some things in the code that are not in the algorithm. This is purely because I'm a little unsure on how I should write them so that it makes sense to a programmer when they try to write this program.

NOTE: Please bear in mind that I am aware the code lacks modularization and has a lot of else -if statements. I wrote the code specifically this way because I was advised to do so. It was part of my instructions.

Deep nesting and lack of modularization are the worst problems here.

## Deep nesting

You are following the arrow antipattern, instead use indipedent if clauses or a switch statement.

## Lack of modularization

You should use functions:

int get_side(side_name) {
// procedure for getting the side that you repeated 3 times
}

bool is_isoscele(sideA,sideB,sideC) {
return (sideA == sideB) || (sideB == sideC) || (sideC == sideA)
}

bool is_equilater(sideA,sideB,sideC) {
// This function may be simpler than what you wrote ...
}


ps: to return bool you must import the bool header.

• Thanks for that. I am aware it is not modular and has deep nesting. However I was advised to make it as such. This is because we are getting taught programming in C and this one part of the fundamentals they wanted to teach us. How would you make this program to make it loop back to the start after either an invalid option has been presented or after an answer has been given. I basically want to give the user the option to retry or simply exit the program. Mar 23, 2015 at 13:24
• @Shivam , learn to use do-while loops. Mar 23, 2015 at 15:34