function with dynamic array

im having trouble making function which takes in two integers, two dynamic arrays and the arrays size.

1
2
3
4
5
6
7
8
9
10
11
This is the decleration
int shortest_distance(hnit loc1, hnit loc2, int *p, int *p1, int size);

    int n, dogs;
    cin >> n >> dogs;
    cin >> refur.location.x >> refur.location.y;
hundar = new hnit[dogs];

and this is in the main where im having trouble calling the function
int s_d = shortest_distance(fox.location.x, fox.location.y, hundar, hundar, dogs);
int *p,int *p1

but you pass hundar(which is hnit *)
i dont understand what you mean ?
..can you post your codes more detail?
#include <iostream>
#include <string>
#include <cmath>

using namespace std;
struct hnit
{
int x;
int y;
};


struct fox
{
bool escaped;
bool caught;
hnit location;
};

void print_fox(fox a);
hnit get_location(fox a);
bool on_boundary(hnit xy, int size);
double calculate_distance(hnit loc1, hnit loc2); punkta
//double calculate_distance(int x1, int y1, int x2, int y2);
int shortest_distance(hnit loc1, hnit loc2, int p[], int p1[], int size);

////////////////

int main()
{
fox refur;
hnit *hundar;

int n, dogs;
cin >> n >> dogs;
cin >> refur.location.x >> refur.location.y;

//upphafsstilli bool breyturnar
refur.escaped = false;
refur.caught = false;

hundar = new hnit[dogs];

for (int i = 0; i < dogs; i++)
{
cin >> hundar[i].x >> hundar[i].y;
}

char **p = new char*[n];
for (int i = 0; i < n; i++)
{
p[i] = new char[n];
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
p[i][j] = '.';
}
}

for (int i = 0; i < dogs; i++)
{
p[hundar[i].x][hundar[i].y] = 'D';
}

p[refur.location.x][refur.location.y] = 'F';

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << p[i][j];
}
cout << endl;
}

hnit tmplocation;
tmplocation.x = 0;
tmplocation.y = 0;

double d = calculate_distance(refur.location, tmplocation);

cout << d << endl;

d = calculate_distance(refur.location, hundar[0]);

cout << d << endl;
//hnit tmp = get_location(refur); //gríp upplýsingar úr fallinu

int s_d = shortest_distance(refur.location.x, refur.location.y, hundar.x, hundar.*y, dogs);
cout << s_d << endl;

delete [] hundar;
return 0;
}


//////////////////////


void print_fox(fox a)
{

}

hnit get_location(fox a)
{
return a.location;
}

bool on_boundary(hnit xy, int size)
{
return false;
}

double calculate_distance(hnit loc1, hnit loc2)
{
double dx = loc1.x - loc2.x;
double dy = loc1.y - loc2.y;
return sqrt(dx * dx + dy * dy);
}

//double calculate_distance(int x1, int y1, int x2, int y2)
//{
// return sqrt( (x1-x2)*(x1-x2) + (y1 - y2)*(y1-y2));
//}

int shortest_distance(hnit loc1, hnit loc2, int p[], int p1[], int size)
{
return size;
}



hundar.*y

I suggest you read about pointers and functions.
Last edited on
hnit *hundar;
Last edited on
and what do you want to implement?
im trying to make function that calculates the shortest distance from the fox to one of the dog
you can use stack variables in stead of new.

such as "string a[10];" in stead of "string *p = new string[10]"

which is more simple.
okay ill try that, thank you
Topic archived. No new replies allowed.