GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Sep-2005, 15:18
micheldenostra micheldenostra is offline
New Member
 
Join Date: Sep 2005
Posts: 19
micheldenostra is on a distinguished road

no output, usage of sqrt and pow


hey everyone please help me with my program, when i run it no answer comes up basically it is supposed to calculate which of three different locations is closeset to the others. 3 people are going to meet and the total distance traveled needs to be the shortest, so the closet location is what i need to be able to figure out from whatever coordinates are inputted.

this program is on C and i do not need to enter a math library because with putty you enter the match library by a special means of compiling (gcc -lm -o program_name program_name.c)

here is the distance formula for determinng the distance between two coordinates
distance=square root of [(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)].

i'm currently getting no out put, just a blank line. i think the problem may lie in my inexperience with math library usage or just math in general

any help greatly appreciated and by the way i'm another person who needs this code before midnight

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  //dist## means the distance between the two houses represented by '#' and '#'
  //tdh means total distance from house 1,2,3, the distance it takes for both people to get to that house
  int x1, y1, x2, y2, x3, y3;
  float dist12, dist13, dist23, tdh1, tdh2, tdh3;

  printf("What are the x and y coordinates of the first person's house? ");
  scanf("%d%d", &x1, &y1);

  printf("What are the x and y coordinates of the second person's house? ");
  scanf("%d%d", &x2, &y2);

  printf("What are the x and y coordinates of the third person's house? ");
  scanf("%d%d", &x3, &y3);

  dist12=sqrt(pow((x2-x1), 2)+pow((y2-y1), 2));
  dist13=sqrt(pow((x3-x1), 2)+pow((y3-y1), 2));
  dist23=sqrt(pow((x3-x2), 2)+pow((y3-y2), 2));

  tdh1=dist12+dist13;
  tdh2=dist12+dist23;
  tdh3=dist13+dist23;

  if (tdh1<tdh2 && tdh1<tdh3)
    printf("You should study at 1's house\n");
  if (tdh2<tdh1 && tdh2<tdh3)
    printf("You should study at 2's house\n");
  if (tdh3<tdh1 && tdh3<tdh2)
    printf("You should study at 3's house\n");

  return 0;
}
  #2  
Old 25-Sep-2005, 17:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: no output, usage of sqrt and pow


Quote:
Originally Posted by micheldenostra
this program is on C and i do not need to enter a math library because with putty you enter the match library by a special means of compiling (gcc -lm -o program_name program_name.c)

This makes no sense to me. Do you mean that gcc gives no errors even though you don't include <math.h>? Well OK, but that has nothing to do with the -lm switch on the command line.

I respectfully suggest that you use the following command line:
Code:
gcc -Wall -pedantic -lm -o program_name program_name.c

(You really, really, really, really should include <math.h>, and turn on those compiler warnings!)


Quote:
Originally Posted by micheldenostra
i'm currently getting no out put, just a blank line. i think the problem may lie in my inexperience with math library usage or just math in general

Make the program tell you what it is seeing. Something like this:

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

int main()
{
  int x1, y1, x2, y2, x3, y3;
  float dist12, dist13, dist23, tdh1, tdh2, tdh3;

  printf("What are the x and y coordinates of the first person's house? ");
  scanf("%d%d", &x1, &y1);
  printf("You entered (%d, %d)\n", x1, y1);

  printf("What are the x and y coordinates of the second person's house? ");
  scanf("%d%d", &x2, &y2);
  printf("You entered (%d, %d)\n", x2, y2);

  printf("What are the x and y coordinates of the third person's house? ");
  scanf("%d%d", &x3, &y3);
  printf("You entered (%d, %d)\n", x3, y3);



  dist12=sqrt(pow((x2-x1), 2)+pow((y2-y1), 2));
  printf("pow((x2-x1),2) = %f, pow((y2-y1), 2 = %f, dist12 = %f\n", 
          pow((x2-x1),2), pow((y2-y1), 2), dist12);

  dist13=sqrt(pow((x3-x1), 2)+pow((y3-y1), 2));
  /* etc with the print statements */

  dist23=sqrt(pow((x3-x2), 2)+pow((y3-y2), 2));
  /* etc with the print statements*/

  printf("dist12 = %f, dist13 = %f\n", dist12, dist13);
  printf("dist12 = %f, dist23 = %f\n", dist12, dist23);
  printf("dist13 = %f, dist23 = %f\n", dist13, dist23);
  tdh1=dist12+dist13;
  tdh2=dist12+dist23;
  tdh3=dist13+dist23;
  printf("tdh1 = %f, tdh2 = %f, tdh3 = %f\n", tdh1, tdh2, tdh3);

  if (tdh1<tdh2 && tdh1<tdh3)
    printf("You should study at 1's house\n");
  if (tdh2<tdh1 && tdh2<tdh3)
    printf("You should study at 2's house\n");
  if (tdh3<tdh1 && tdh3<tdh2)
    printf("You should study at 3's house\n");

  return 0;
}

Regards,

Dave

(Now try it with <math.h> and without <math.h>)
  #3  
Old 25-Sep-2005, 17:21
micheldenostra micheldenostra is offline
New Member
 
Join Date: Sep 2005
Posts: 19
micheldenostra is on a distinguished road

Re: no output, usage of sqrt and pow


thanks for the try but the problem actually was in my pow constructs, i replaced them by just timesing each equasion by itself.
btw my compiler does not recognize #include <math.h>
thanks for your help but i got it now
  #4  
Old 25-Sep-2005, 17:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: no output, usage of sqrt and pow


Quote:
Originally Posted by micheldenostra
thanks for the try but the problem actually was in my pow constructs, i replaced them by just timesing each equasion by itself.
btw my compiler does not recognize #include <math.h>
thanks for your help but i got it now

Well, putting printf() statements before and after every calculation is a valid way of debugging, regardless of the nature of the errors.

Now, I will indulge in a little editorial:


I have no idea what "putty" is, but gcc expects <math.h>. If you are using some trumped up toy interface that hides facts of life from you, you will be in for a rude awakening when (if) you ever get turned loose in (on) the so-called "real world".

There was nothing wrong with the syntax of your original program. If your compiler makes you break arithmetic expressions down into simpler forms than the language allows, your educational instution is doing you a huge disservice. That's just my opinion, of course, and worth every bit that you paid for it. But consider this: If your compiler doesn't recognize <math.h>, what other standard language constructs are being kept from you? How could you know what you are mis-learning?

Good luck.

Regards,

Dave
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.