LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-05-2014, 06:26 PM   #1
beginner lira
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Rep: Reputation: Disabled
Unhappy Code doesnt work, Urgenttt please help :'(


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int main()
{
char firstcity[15]; float latitudeoffirst; float longitudeoffirst;
scanf("%s %f %f ",firstcity ,&latitudeoffirst, &longitudeoffirst);

char secondcity[15]; float latitudeofsecond; float longitudeofsecond;
scanf("%s %f %f ",secondcity ,&latitudeofsecond, &longitudeofsecond);

char thirdcity [15]; float latitudeofthird; float longitudeofthird;
scanf("%s %f %f ",thirdcity ,&latitudeofthird, &longitudeofthird);


float distance1= abs(latitudeofsecond-latitudeoffirst)+abs(longitudeofsecond-longitudeoffirst);
scanf ("%f",&distance1);

float distance2=abs(latitudeofthird-latitudeoffirst)+abs(longitudeofthird-longitudeoffirst);
scanf("%f",&distance2);

if (distance1>distance2) printf ("secondcity");


else printf ("thirdcity");





return 0;
}
 
Old 12-05-2014, 06:33 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,240

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Well, I've done you the courtesy of making a more nicely formatted version:

Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char firstcity[15];
	float latitudeoffirst;
	float longitudeoffirst;

	scanf("%s %f %f ", firstcity, &latitudeoffirst, &longitudeoffirst);

	char secondcity[15];
	float latitudeofsecond;
	float longitudeofsecond;
	scanf("%s %f %f ", secondcity ,&latitudeofsecond, &longitudeofsecond);

	char thirdcity [15];
	float latitudeofthird;
	float longitudeofthird;
	scanf("%s %f %f ",thirdcity ,&latitudeofthird, &longitudeofthird);


	float distance1 = abs(latitudeofsecond - latitudeoffirst) + abs(longitudeofsecond - longitudeoffirst);
	scanf ("%f",&distance1);

	float distance2 = abs(latitudeofthird-latitudeoffirst)+abs(longitudeofthird-longitudeoffirst);
	scanf("%f", &distance2);

	if (distance1 > distance2)
	{
		printf("secondcity");
	} else
	{
		printf("thirdcity");
	}

	return 0;
}
What do you want to happen when you run it, and what actually happens when you run it?
 
Old 12-05-2014, 06:38 PM   #3
beginner lira
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
the question is this :

You are asked to find Eulidean distance of some cities. You will be given Latitude and Longitude coordinates of a city and then, two other citites. Find the closer city to the first one.
Input specification
You will be given a string (city name) and two floating point numbers (Latitude and Longitude coordinates of each city) in three lines where (W/E) -180 ≤ Longitude ≤ 180 and (S/N) -90 ≤ Latitude ≤ 90. And, city name is not longer than 15 characters and contains only English letters (no special characters).

Output specification
Show the name of the closer city.

When i run it it doesnt open , it just says this :Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Last edited by beginner lira; 12-05-2014 at 06:43 PM.
 
Old 12-05-2014, 06:42 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,240

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
So: what exactly is happening that's wrong when you try to run it?
 
Old 12-05-2014, 06:45 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,745

Rep: Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924
Welcome to LinuxQuestions.

In addition...
It is not urgent for the members who volunteer their time to help you with your homework. You have not provided any information as to what is wrong.

I will say that your equation to find distance is wrong. latitude/longitude is usually given in degrees minutes, seconds or degrees, minutes. Are you converting them correctly into degrees. East is plus and west is minus although with only calculating distance it will not make much difference as long as they are all the same.

Last edited by michaelk; 12-05-2014 at 06:47 PM.
 
Old 12-05-2014, 06:46 PM   #6
beginner lira
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
" it seems that the project isnt build up yet, do you want to build it now ? " - and when i click yes nothing shows up... and this is written in the part where it shows the errors : Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 
Old 12-05-2014, 06:54 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,627

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
well you might run into issues with this
" abs(latitudeofthird-latitudeoffirst)"

taking the abs


i am in the Detroit area and depending if one is using "PositiveEast or "PositiveWest"
or if one is using -180 to +180 or 0 to 360

so +83.1 AND -96.9 or E 276.9 Longitude are correct for Detroit


as for notation
BOTH analog and binary are correct
for the center of the telescope mount at the royal observatory
0 deg. 0 min. 0 sec.
and
0.0000000000000

-- yes one CAN be driven to MADNESS !!!!

Last edited by John VV; 12-05-2014 at 07:03 PM.
 
1 members found this post helpful.
Old 12-05-2014, 06:56 PM   #8
beginner lira
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
it doesnt matter on the city , the formu la doesnt change , its constant, im sure there's no problem with the formula or number...
 
Old 12-05-2014, 07:00 PM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,745

Rep: Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924
What development environment i.e. program are you using to create your code/project?

I guess your just given degrees so ignore conversion...

And yes your equation is wrong.

Last edited by michaelk; 12-05-2014 at 07:03 PM.
 
Old 12-05-2014, 07:06 PM   #10
beginner lira
LQ Newbie
 
Registered: Dec 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
im using codebloks... yes you just put the numbers for example Albania 5485.6565 46546.65465 and so on... and you use euclidian formula
 
Old 12-05-2014, 07:34 PM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,745

Rep: Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924Reputation: 5924
I don't use code blocks and could not find exactly what status 1 means.

The numbers in your last post are not +/-180 and +/-90...

PS.
Since you do not have any text asking for input you probably can not tell that the program was running. When you closed it out it probably gave you that error.

You also might want to change your
scanf ("%f",&distance1);
to a print statement.

Last edited by michaelk; 12-05-2014 at 07:47 PM.
 
1 members found this post helpful.
Old 12-05-2014, 07:52 PM   #12
turtleli
Member
 
Registered: Aug 2012
Location: UK
Posts: 206

Rep: Reputation: Disabled
I don't use CodeBlocks either, but your code runs fine on the command line. Exit status 1 usually just means a general failure, so it's difficult for anyone to diagnose what went wrong.

Will also have to mention that your calculation is definitely wrong. Example: I live at -179.9999, 0. Friend A live at 179.9999, 0. Friend B lives at 0, 0. I want to visit the friend who lives furthest away from me. Using your formula, Friend A is the furthest away, even though Friend A is probably my next door neighbour.

You need to fix the distance formula and add another calculation step before that (I won't tell you what that is, at least for now).
 
Old 12-11-2014, 08:12 AM   #13
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
Hi,

You might rethink your formula. To calculate dinstances in a 2-dimensional space, you would use Pythagoras' formula -> c^2 = a^2 + b^2, where a = abs(x1 - y1) and b = abs(x2 - y2).

For those who worry about the use of longtitude and latitude, the question only has to answer about the distance, not about in which direction you have to travel.

Cheers,
 
Old 12-11-2014, 08:29 AM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
The point cannot be emphasized enough ... that this is very-plainly homework, and that the intention, not only of "homework" but of the school/class in general, is to teach you the skill of figuring out for yourself what is wrong with your program. It teaches you how to translate a plausible set of requirements (which have in this case been liberally salted with clues) into a functioning program.

This is "the thing that we get paid the big bucks to do."

If you plead for those who do know how to do this, to do this for you, then you are depriving yourself of something that other people are giving both of their money and of their time to provide you. I say this in part because I have taught these courses at community colleges myself, and I didn't do it for the pay.

If, on the other hand, you approach us by saying, "hey, this is homework, and I'm really stuck and this is what I've done so-far and this, specifically, is what I don't understand ..." then many of us will certainly help you. They will strive to help you learn, as will your instructor. (You should be approaching him or her first, partly so that s/he can better gauge the level-of-understanding within the class, so as to fine-tune the instructional approach being taken.)

You have been given the clues that you need to resolve your problem. Without asking for any further clarification, now, go and do it. Yourself. "Practice, practice ..." (And, "patience.") No one around here is saying that it's easy.

Last edited by sundialsvcs; 12-11-2014 at 08:33 AM.
 
Old 12-11-2014, 08:54 AM   #15
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
Quote:
Originally Posted by gdejonge View Post
Hi,

You might rethink your formula. To calculate dinstances in a 2-dimensional space, you would use Pythagoras' formula -> c^2 = a^2 + b^2, where a = abs(x1 - y1) and b = abs(x2 - y2).

For those who worry about the use of longtitude and latitude, the question only has to answer about the distance, not about in which direction you have to travel.

Cheers,
The fact that it is lat/long means Pythagoras doesn't work. On spheres it's the haversine formula that is used. http://en.wikipedia.org/wiki/Haversine_formula
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Touchpad doesnt work lepass_7 Linux - Newbie 6 10-12-2009 10:50 AM
rmdir -rf does not seem to work in fc4 .what do i do?rm -p also doesnt seem to work vinay87 Linux - Newbie 2 05-09-2006 09:18 AM
Why doesnt my USB mouse doesnt work? barkha Linux - Hardware 2 08-16-2005 11:31 AM
Javascript Code doesnt work w/ firefox? swatward Programming 1 04-18-2005 06:54 PM
Apache doesnt like me, but sure likes Code Red Proud Linux - Networking 7 06-12-2003 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration