Sean Sean - 18 days ago 8
C Question

Wrong function call in C programm

My programm is not working at all, because of some mistake in calling a function, but I can't find out how to fix it out. I was using pointers in order to call function and use values of multidimensional array.

#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <stdio.h>
#define PI 3.14159265
#define SIZE 7

double distanceKm(double, double, double, double);
double maximumDistance(int*, int*, double*, double*,double*);

int main(void)
{
char *names[SIZE] = { "West York", "Eiffel tower", "Palma de Mallorca", "Las Vegas", "Copacabana",
"Waikiki Beach","Surfer's paradise" };

double locations[SIZE][2] = {
{ 52.557029, 10.023109 },
{ 48.858363, 2.294481 },
{ 39.562553, 2.661947 },
{ 36.156214, -115.148736 },
{ -22.971177, -43.182543 },
{ 21.281004, -157.837456 },
{ -28.002695, 153.431781 },
};
double maxDistance=0.0;
int loc1=0, loc2=0;

maximumDistance(&loc1, &loc2, &maxDistance, locations,&distanceKm);
printf("The maximal distance is %.4f between %s and %s", maxDistance, names[loc1], names[loc2]);

getchar();
}

double maximumDistance(int* loc1,int* loc2, double* maxDistance, double *locations[SIZE][2],
double (*distanceKm)(double,double,double,double))
{
for (int i = 0; i < SIZE; i++) {
for (int j = i + 1; j < SIZE; j++) {
double distance = distanceKm(*locations[i][0], *locations[i][1], *locations[j][0], *locations[j][1]);

if (distance > *maxDistance) {
return *maxDistance = distance;
return *loc1 = i;
return *loc2 = j;
}
}
}
}

double distanceKm(double lat1, double longit1, double lat2, double longit2)
{
double param;

lat1 = lat1*PI / 180.0;
lat2 = lat2*PI / 180.0;
param = ((longit2 - longit1)*PI / 180.0);

return 6378.388*(acos((sin(lat1)*sin(lat2)) +
(cos(lat1)*cos(lat2)*cos(param))));
}

LPs LPs
Answer

Correcting language problems of your code:

#include <math.h>
#include <stdio.h>
#define PI 3.14159265
#define SIZE 7

double distanceKm(double, double, double, double);
double maximumDistance(int*, int*, double*, double[SIZE][2], double (*distanceKm)(double,double,double,double));

int main(void)
{
    char *names[SIZE] = { "West York", "Eiffel tower", "Palma de Mallorca", "Las Vegas", "Copacabana",
        "Waikiki Beach","Surfer's paradise" };

    double locations[SIZE][2] = {
        { 52.557029, 10.023109 },
        { 48.858363, 2.294481 },
        { 39.562553, 2.661947 },
        { 36.156214, -115.148736 },
        { -22.971177, -43.182543 },
        { 21.281004, -157.837456 },
        { -28.002695, 153.431781 },
    };
    double maxDistance=0.0;
    int loc1=0, loc2=0;

    maximumDistance(&loc1, &loc2, &maxDistance, locations, distanceKm);
    printf("The maximal distance is %.4f between %s and %s", maxDistance, names[loc1], names[loc2]);

    getchar();
}

double maximumDistance(int* loc1,int* loc2, double* maxDistance, double locations[SIZE][2],
                        double (*distanceKm)(double,double,double,double))
{
    for (int i = 0; i < SIZE; i++) {
        for (int j = i + 1; j < SIZE; j++) {
            double distance = distanceKm(locations[i][0], locations[i][1], locations[j][0], locations[j][1]);

            if (distance > *maxDistance) {
                return *maxDistance = distance;
                return *loc1 = i;
                return *loc2 = j;
            }
        }
    }

    return .0f;
}

double distanceKm(double lat1, double longit1, double lat2, double longit2)
{
    double param;

    lat1 = lat1*PI / 180.0;
    lat2 = lat2*PI / 180.0;
    param = ((longit2 - longit1)*PI / 180.0);

    return 6378.388*(acos((sin(lat1)*sin(lat2)) +
        (cos(lat1)*cos(lat2)*cos(param))));
}
  1. Prototype of maximumDistance is wrong. Take a look how I changed it.
  2. Function definition is also wrong.
  3. Using locations[i][0] you are already dereferencing the value of matrix, so the use * is wrong.
  4. Little last thing, maximumDistance must return a double.