C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 13th, 2002, 12:24 PM
Wise1 Wise1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 1 Wise1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Can anyone see what is wrong with this code?

What's up,

I'm writing a program to calculate the distance between two latitude/longitude coordinates. the below code is what I wrote. It is not working for some reason and I'm clueless to what is wrong. Maby you can catch something that I'm missing. You have any ideas? Any help would be great. Thanks in advance!

Peace
Wise1

Code:
#include<stdio.h>
#include<math.h>
#include<conio.h>

main(){
  //Declarations
  double pi=3.14159265358979,a,c,d,dlat,dlon,lat1,lat2,lon1,lon2;
  int days,hours,s,r=3956;

  //Header
  printf("Origin Coordinates \n  1) 16, 64 \t\t Speed: 20 mph \n  2) 32, 64 (Bermuda) \t Speed: 10 mph \n\nDestination Coordinates \n  1) 26, 80 (Ft. Lauderdale) \n\n\n");

  //Read Input
  printf("Please Enter the Origin Coordinates: ");
  scanf("%f, %f",&lat1,&lon1);
  printf("Please Enter the Destination Coordinates: ");
  scanf("%f, %f",&lat2,&lon2);
  printf("Please Enter the Speed of Travel: ");
  scanf("%d",&s);

  //Convert Coordinates From Decimal Degrees to Radians
  lat1*=pi/180;
  lat2*=pi/180;
  lon1*=pi/180;
  lon2*=pi/180;

  //The Haversine Formula
  dlat=lat2-lat1;
  dlon=lon2-lon1;
  a=((sin(dlat/2))*(sin(dlat/2)))+cos(lat1)*cos(lat2)*((sin(dlon/2))*(sin(dlon/2)));
  c=2*atan2(sqrt(a),sqrt(1-a));
  d=r*c;

  //Find Time
  hours=d/s;
  days=hours/24;
  hours-=days*24;

  //Print Output
  printf("\n\nDistance: %f Statute Miles \nSpeed: %d mph\nTime (Days/Hours): %d/%d",d,s,days,hours);

  getch();
}

Reply With Quote
  #2  
Old February 13th, 2002, 01:11 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
Perhaps you can be a little more informative. What is the problem(s) you are having with this code?
Have you tried running it through a debugger to watch the values and/or stack?

Reply With Quote
  #3  
Old February 13th, 2002, 01:17 PM
Wise1 Wise1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 1 Wise1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What's up Onslaught,

The problem is that I'm getting all zeros for my results except for the speed. For some reasons the doubles are not working with scanf(). I know the eqation works corectly because I tested it with defined variables but once I asked for user input I got all zeros. Dose that help.

Peace
Wise1

Reply With Quote
  #4  
Old February 13th, 2002, 01:31 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,827 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 22 h 57 m 29 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
Yes, that helps greatly.
Have you tried breaking the scanfs for the coordinate up into two seperate lines?
i.e.
Code:
printf("\nPlease Enter the Origin Coordinates:\n");
printf("Lat: ");
scanf("%f", &lat1);
printf("\nLon: ");
scanf("%f", &lon1);
printf("\nPlease Enter the Destination Coordinates:\n");
printf("Lat: ");
scanf("%f", &lat1);
printf("\nLon: ");
scanf("%f", &lon1);


edit: fix a typo

Reply With Quote
  #5  
Old February 13th, 2002, 01:41 PM
Wise1 Wise1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 1 Wise1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What's up Onslaught,

Yes, I tried that. It does not work.

Peace
Wise1

Reply With Quote
  #6  
Old February 13th, 2002, 01:51 PM
Wise1 Wise1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 1 Wise1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What's up,

nevermind I figured it out. I had to use %lf to scan it instead of %f. Thanks for your help anyways.

Peace
Wise1

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Can anyone see what is wrong with this code?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway