Solved

Can you improve it

Posted on 2003-11-26
6
251 Views
Last Modified: 2010-04-15
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;
                              }
      }
}
0
Comment
Question by:sisqu
6 Comments
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9824006
Hi

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
0
 
LVL 4

Expert Comment

by:dhyanesh
ID: 9824041
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
0
 
LVL 2

Expert Comment

by:htang_us
ID: 9824829
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.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 4

Assisted Solution

by:dhyanesh
dhyanesh earned 250 total points
ID: 9825144
Hi

You need not have an else after the if

       if(min>S_t) {                                         //should be    S_min>S_t
                           
                             S_min=S_t;
                            min=i;
                       }

                       if(max<S_t) {                        //should be  S_max<S_t
                            S_max=S_t;
                            max=i;
                       }

If an 'else' statement is put then first element will be set as minimum only. However first element is max as well as min

Dhyanesh
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 250 total points
ID: 9825304
#include <stdio.h>
#include <math.h>

int main(void) {

float trian[10][3];
float a,b,c,p,i;
float S_max=0,S_min=FLOAT_MAX,S_t;       //most compilers define constants for maximum value that a data type can hold ...
                                                                  // check the header files stdlib or types.h
int min=-1, max=-1;    

    for(int i=0;i<10;i++)  //you had not declared i
     {
         
          a=trian[i][0];
         b=trian[i][1];
         c=trian[i][2];

              if((a+b)>c && (b+c)>a && (c+a)>b) // can be sides of triangle
              {
                   p=(a+b+c);                                    //you can get the result without the division
                   S_t=(p*(p-a)*(p-b)*(p-c));              // and ofcourse without the sqrt

                        if(S_min>S_t) {
                             
                             S_min=S_t;
                             min=i;
                        }

                         if( max < S_t) {
                             S_max=S_t;
                             max=i;
                        }
    }
}
0
 

Author Comment

by:sisqu
ID: 9829239
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
port mapped I/o query 10 186
C hashtable library 3 90
Intel debugger, idb, only seeing .c and .h, but no .f90 1 327
Unable to start eclipse ? 17 141
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question