sisqu
asked on
Can you improve it
hi,
I make a programme but I keep the feeling that it is not good. How you will make it:
You have a massive P[10][3] contains ten triangles(its sides):
side a=P[1][1], side b=P[1][2], side c=P[1][3]. Find whitch have max surface, and whitch have min surface.
You need to try if inpud numbers can be sides of triangle.
#include <stdio.h>
#include <math.h>
int main(void) {
float trian[10][3];
float a,b,c,p;
float S_max=0,S_min=1000,S_t;
int min, max;
for(int i=0;i<10;i++)
{
a=trian[i][1];
b=trian[i][2];
c=trian[i][3];
if((a+b)>c && (b+c)>a && (c+a)>b) // can be sides of triangle
{
p=(a+b+c)/2;
S_t=sqrt(p*(p-a)*(p-b)*(p- c));
if(min>S_t) {
S_min=S_t;
min=i;
}
else if(max<S_t) {
S_max=S_t;
max=i;
}
}
}
I make a programme but I keep the feeling that it is not good. How you will make it:
You have a massive P[10][3] contains ten triangles(its sides):
side a=P[1][1], side b=P[1][2], side c=P[1][3]. Find whitch have max surface, and whitch have min surface.
You need to try if inpud numbers can be sides of triangle.
#include <stdio.h>
#include <math.h>
int main(void) {
float trian[10][3];
float a,b,c,p;
float S_max=0,S_min=1000,S_t;
int min, max;
for(int i=0;i<10;i++)
{
a=trian[i][1];
b=trian[i][2];
c=trian[i][3];
if((a+b)>c && (b+c)>a && (c+a)>b) // can be sides of triangle
{
p=(a+b+c)/2;
S_t=sqrt(p*(p-a)*(p-b)*(p-
if(min>S_t) {
S_min=S_t;
min=i;
}
else if(max<S_t) {
S_max=S_t;
max=i;
}
}
}
Hi
Few more errors. First of all initializing S_min to 1000 is an error. You could have the min surface area triangle greater than 1000. You should increase this value to the maximum possible value. Another alternative is to assume the area of first triangle i.e. trian[0] as min and then continue comparing with others as you have done.
Also following lines have errors:
if(min>S_t) { //should be S_min>S_t
S_min=S_t;
min=i;
}
else if(max<S_t) { //should be S_max<S_t
S_max=S_t;
max=i;
}
Dhyanesh
Few more errors. First of all initializing S_min to 1000 is an error. You could have the min surface area triangle greater than 1000. You should increase this value to the maximum possible value. Another alternative is to assume the area of first triangle i.e. trian[0] as min and then continue comparing with others as you have done.
Also following lines have errors:
if(min>S_t) { //should be S_min>S_t
S_min=S_t;
min=i;
}
else if(max<S_t) { //should be S_max<S_t
S_max=S_t;
max=i;
}
Dhyanesh
Since you just want to find which triangle has min/max surface, you need not calculate the sqrt of the product. However, we need be careful here if there is overflow problem.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all (dhyanesh, htang_us and sunnycoder)
for professional look, you give me good ideas. I cant deside who give the points and divide it to both.
for professional look, you give me good ideas. I cant deside who give the points and divide it to both.
One error noticed instantly is
a=trian[i][1];
b=trian[i][2];
c=trian[i][3];
should be
a=trian[i][0];
b=trian[i][1];
c=trian[i][2];
Dhyanesh