Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,165 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,635 people online right now. Registration is fast and FREE... Join Now!




C Program Euclidean Distance

 
Reply to this topicStart new topic

C Program Euclidean Distance

pigmitten
post 4 Oct, 2008 - 08:04 AM
Post #1


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 9

Hey, so I have been programming for a glorious total of three days, and I'm trying to code my first program that uses functions & calls functions. This is literally the 4th thing I have written in code, ever. I'm having issues with my program--I'm doing this in Xcode--it says I have an implicit declaration & conflicting types for the CalculateDistance function, but I really have no idea what that means. This is for a class assignment (link) which I need to get in asap. I'm working on the Euclidean distance problem. The assignment says that 1) This function should just calculate. It should not do any reading or printing. and 2) main() should call the function and print both the data and the answers. Can anyone help?

this is what I have:
CODE


[code] /*
*  distance.h
*  distance.c
*
*  Created by Nandita Seshadri on 10/2/08.
*  This function calculates the Euclidean distance
*    between two points.
*
*/

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

//Function Declarations
int main ()
{
/* Function Call */
double distance1 = CalculateDistance( 0.0, 0.0, 5.0, 5.0);
double distance2 = CalculateDistance (0.0, 0.0, 5.0, 0.0);
double distance3 = CalculateDistance (0.0, 0.0, 0.0, 5.0);

printf("   Point1      Point 2        Distance\n");
printf("(%4.1f, %4.1f)", 0.0, 0.0);
printf("(%4.1f, %4.1f)", 5.0, 5.0);
printf("%4.1f\n", distance1);

printf("(%4.1f, %4.1f)", 0.0, 0.0);
printf("(%4.1f, %4.1f)", 5.0, 0.0);
printf("%4.1f\n", distance2);

printf("(%4.1f, %4.1f)", 0.0, 0.0);
printf("(%4.1f, %4.1f)", 0.0, 5.0);
printf("%4.1f\n", distance3);    
    
return 0;
}

//Distance Function

double CalculateDistance(double x1, double y1, double x2, double y2)
{    
    double diffx = x1 - x2;
    double diffy = y1 - y2;
    double diffx_sqr = square (diffx);
    double diffy_sqr = square (diffy);
    double distance = sqrt (diffx_sqr + diffy_sqr);

return distance;
}
User is offlineProfile CardPM

Go to the top of the page

Sadaiy
post 4 Oct, 2008 - 08:11 AM
Post #2


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 35



Thanked 1 times
My Contributions


try adding function protoype
User is offlineProfile CardPM

Go to the top of the page

Sadaiy
post 4 Oct, 2008 - 08:18 AM
Post #3


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 35



Thanked 1 times
My Contributions


Ok here is my code, it works in visual studio 2008, i added a prototype and also apparently "square" doesn't exist in the math.h libraries, so i changed it to pow(base, exponent) function okay ????

here it is:

CODE

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

//Function Declarations

double CalculateDistance(double, double, double, double);

int main ()
{
/* Function Call */
double distance1 = CalculateDistance( 0.0, 0.0, 5.0, 5.0);
double distance2 = CalculateDistance(0.0, 0.0, 5.0, 0.0);
double distance3 = CalculateDistance(0.0, 0.0, 0.0, 5.0);

printf("   Point1      Point 2        Distance\n");
printf("(%4.1f, %4.1f)", 0.0, 0.0);
printf("(%4.1f, %4.1f)", 5.0, 5.0);
printf("%4.1f\n", distance1);

printf("(%4.1f, %4.1f)", 0.0, 0.0);
printf("(%4.1f, %4.1f)", 5.0, 0.0);
printf("%4.1f\n", distance2);

printf("(%4.1f, %4.1f)", 0.0, 0.0);
printf("(%4.1f, %4.1f)", 0.0, 5.0);
printf("%4.1f\n", distance3);    
    
return 0;
}

//Distance Function

double CalculateDistance(double x1, double y1, double x2, double y2)
{    
    double diffx = x1 - x2;
    double diffy = y1 - y2;
    double diffx_sqr = pow(diffx,2);
    double diffy_sqr = pow(diffy,2);
    double distance = sqrt(diffx_sqr + diffy_sqr);

return distance;
}
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 4 Oct, 2008 - 08:43 AM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,959



Thanked 95 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(Sadaiy @ 4 Oct, 2008 - 12:18 PM) *

apparently "square" doesn't exist


There's probably a reason. wink2.gif

Try this:
cpp

double diffx_sqr = diffx * diffx;

User is offlineProfile CardPM

Go to the top of the page

pigmitten
post 4 Oct, 2008 - 08:51 AM
Post #5


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 9

Hey!! the function prototype was it. I added it in and voila! It worked. So basically, any time I do a function, I have to add that in at the beginning? What exactly does it do?

Thanks so so much!!!!



QUOTE(Sadaiy @ 4 Oct, 2008 - 09:11 AM) *

try adding function protoype

User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 4 Oct, 2008 - 10:47 AM
Post #6


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 501



Thanked 44 times

Dream Kudos: 25
My Contributions


Apparently you'll be learning about functions this week, which should include prototyping.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 02:54PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month