Jump to content
xisto Community
Sign in to follow this  
Jeune

Need Debugging Help For A Simple Program Just to make sure.

Recommended Posts

Hi guys.

I just finished my programming homework.
It's a program that gets the coefficients
A,B, and C of two lines in general form and
computes for the

(1) Slope of each line
(2) y-intercept of each line
(3) and their intersections (if there are any)

I know it's quite trivial but it's a homework and

I need help in making sure the algorithm works for all
cases.

For me, the hardest part is the third one where you have to get the intersections.

Here are some cases I came up with:

No lines at all
Parallel Lines
Perpendicular Lines
First line is non-existent
Second line is non-existent
Normal case(there is an intersection)

Can somebody, well if you have time to spare of course, be kind enough to help me try to test the program to see if there are any bugs?

It's pretty simple. Just type in all the possible cases you know and see
if the output of the program is correct.

I have tried all the cases I know and as far as they are concerned, the program is ok.

Thanks and best wishes this holiday season.

This is my code btw:

/*  Author: Jose Luis Roberto Asuncion  Section: G-4L  Date: 21/12/05 10:13am   Description: Given the coefficients of two lines in general form,   this program computes for their slope, y-intercept and intersection.    */#include "stdio.h"main() {              float A,B,C;     /* coefficents of 1st line  */       float D,E,F;     /* coefficients of 2nd line */              printf("\nEnter the coefficents");       printf(" A,B and C of the first line:");       printf("\n(press enter after each coefficient)\n\n");       scanf("%f %f %f",&A,&B,&C);              printf("\nEnter the coefficents");       printf(" A,B and C of the second line:");       printf("\n(press enter after each coefficient)\n\n");       scanf("%f %f %f",&D,&E,&F);               printf("\n");                          /* Compute for the slope of the first line */              if ((A==0) && (B!=0))     /*  Instance of a horizontal line where A=0 and B!=0 */       {            printf("The slope of the first line is 0");       }       else if (B==0)       {            if (A!=0) /* instance of a vertical line where A!=0 and B==0 */               printf("The slope of the first line is undefined");             else  /* instance where the user entered A==0 and B==0 */               printf("\nThe first line does not exist. There is no slope");        }       else       {    /* instance where the user entered some number other than 0 for each coefficient */            printf("The slope of the first line is %1.1f",(-1*A)/B);       }                     /* Compute for the Y-intercept of the first line */                     if ((A==0) && (B!=0)) /* instance when the line is a horizontal line */          printf(" and the y-intercept is %1.1f",(-1*C)/B);        else if (B==0) /* instance when B=0, triggering a vertical line... */       {          if ((C==0) && (A!=0)) /* that either coincides with the y-axis */          {             printf(" and is conincidental to the y-axis.");             printf(" and thus, has infinitely many y-intercepts");          }          else if (A!=0) /* or is a vertical line somewhere on the plane */             printf(", is a vertical line and does not have a y-intercept.");          else  /* instance where there is no line because A and B is 0*/             printf(" and no y-intercept to compute");       }       else  /* instance where the user entered some number other than 0 for each coefficient */            printf(" and has a y-intercept of %1.1f",(-1*C)/B);               printf("\n");              /* Compute for the slope of the second line */              if ((D==0) && (E!=0)) /* instance of a horizontal line */       {            printf("The slope of the second line is 0");       }       else if (E==0) /* instance of a vertical line */       {            if (D!=0)   /* instance of a vertical line somewhere on the plane */               printf("The slope of the second line is undefined");            else /* instance where there is no line at all bec. D & E is 0 */               printf("The second line does not exist. There is no slope");       }       else /* instance where the user entered some number other than 0 for each coefficient */       {            printf("The slope of the second line is %1.1f",(-1*D)/E);       }              /* compute for the y-intercept of the second line */              if ((D==0) && (E!=0)) /* instance of a horizontal line */          printf(" the y-intercept is %1.1f",(-1*F)/E);       else if (E==0)       {          if ((F==0) && (D!=0)) /* instance when the line coincides with the y-axis*/           {             printf(" and is coincidental to the y-axis.");             printf(" and thus, has infinitely many y-intercepts");          }          else if (D!=0) /* instance of a vertical line somewhere on the plane */             printf(", is a vertical line and does not have a y-intercept.");          else /* instance when there is no line because A is 0 */             printf(" and no y-intercept to compute");       }       else /* instance when the user entered some number other than 0 for each coefficient */            printf(" and has a y-intercept of %1.1f",(-1*F)/E);              printf("\n");              float Det, DetX,DetY,X,Y; /* declare variables for use in Cramer's rule*/       Det = (A*E)-(B*D); /* Equation to get the Determinant */       DetX = -1*C*E - (-1*F)*B; /* Equation to get the Determinant of X*/       DetY = -1*A*F - (-1*C)*D; /* Equation to get the Determinant of Y*/       X = DetX/Det; /* Equation to get X */       Y = DetY/Det; /* Equation to get Y */                     /*        In some cases, the determinant may be zero and Cramer's rule cannot be employed to        solve for the intersection. In this case the lines may be parallel, coincidental,       either or both non existent.        */       if (Det==0)        {          /*            assuming that C and F are both not zero, this  is a             special instance of two parallel lines that are either             horizontal or vertical but not both of course          */                   if (((A==0)&&(D==0) || (B==0)&&(E==0)) && !((A==0)&&(D==0) && (B==0)&&(E==0)) && ((C!=0)&&(F!=0)) && (C!=F))          {                if ((A==0)&&(D==0)) /* instance of a horizontal line */                {                                       printf("There is no intersection because the two lines are parallel");                   printf("\nThey are both horizontal lines.");                }                else /* instance of a vertical line */                {                   printf("There is no intersection because the two lines are parallel");                   printf("\nThey are both vertical lines.");                  }                                        } /* instance where one of the lines is non-existent */          else if ((((A==0) && (B==0)) || ((D==0) && (E==0))) && !(((A==0) && (B==0)) && ((D==0) && (E==0))))          {               if ((A==0)&&(B==0)) /* instance where the first line is non-existent */                  printf("There is no intersection because the first line is non-existent");               else /* instance where the second line is non-existent */                  printf("There is no intersection because the second line is non-existent");            }          else if (((A==0) && (B==0)) && ((D==0) && (E==0)))           {   /* instance where the coefficients entered do not determine a line */               printf("Obviously there is no intersection");          }          else if (((-1*A/B)==(-1*D/E)) && (-1*C/B!=-1*F/E)) /* regular instance when the lines are parallel */          {               printf("There is no intersection because the two lines are parallel");          }          else  /* instance where the lines coincide */               printf("The lines are coincidental");                      }       else  /* instance when the user entered some number other than 0 for each coefficient */       { /* or when the determinant is not equal to 0 */                      printf("The two lines intersect at the point (%1.1f,%1.1f)",X, Y);                      /* instance of perpendicular lines */            if ((X==-1*C) && (Y==-1*F))              printf("\nThey are perpendicular lines");           if((-1*A)/B==((-1*D)/E)*-1)              printf("\nThey are perpendicular lines");                       }       printf("\n");                     }

Share this post


Link to post
Share on other sites

Hi..

 

I would like to help u for testing the program...

But can you provide some test data or sample question based on which i would input values and look for the output and report any bugs, if any?

216334[/snapback]


for instance, we enter no line at all for the first line.

IN general form that would mean, 0x +0y +0 =0 or in slope intercept form:

0y =0x +b. But the requires that the inputs be coefficients in general form

so,

 

Enter the coefficients A,B and C of the first line:

0

0

0

 

if we do the same for the second line,

 

Enter the coefficients A,B and C of the second line:

0

0

0

 

the output would be

 

The first line does not exist. There is no slope and y-intercept to compute.

The second line does not exist. There is no slope and y-intercept to compute.

Obviously there is no intersection.

 

 

cheers.

Share this post


Link to post
Share on other sites

One quick suggestion I have is to put include conio.h and put getch() before the last bracket of the file so that the user presses a key before the program exits instead of it exiting before results can be seen.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.