problem functions

closed account (375jz8AR)
hi i created this programm, but now i need to divide it by functions, one for creation of points, an other for center of mass, an other for rotation and an other for cumulative distance. my problem arrives at the time of put the arguments that each function must use, there must be local variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  #include<stdio.h>
#include <time.h>
#include <cstdlib>
#include <math.h>



int main()
{
	//beginning of creation of random numbers
	srand((unsigned)time(NULL));

	int x, y;
	bool xsign, ysign;

	int arrX[5];
	int arrY[5];

	for (int i = 0; i < 5; i++)
	{
		x = rand() % 100;
		y = rand() % 100;
		xsign = rand() % 2;
		ysign = rand() % 2;

		if (xsign) x *= -1;
		if (ysign) y *= -1;

		arrX[i] = x;
		arrY[i] = y;
	}

	for (int i = 0; i < 5; i++)
	{
		printf("%3d %3d\n", arrX[i], arrY[i]);
	}

	//end of creation of random numbers
	//beginning of center of gravity 
	double cen_x, cen_y;
	printf("centre of gravity is");
	cen_x = (arrX[1] + arrX[2] + arrX[0] + arrX[3] + arrX[4]) / 5;
	cen_y = (arrY[1] + arrY[2] + arrY[0] + arrY[3] + arrY[4]) / 5;
	printf("%lf , %lf\n", cen_x, cen_y);
	//end of center of gravity
	//beginning of rotation of the set
	for (int i = 0; i < 5; i++)
	{
		double rot_x, rot_y;
		printf("rotate points are:");
		rot_x = arrX[i] * cos(0.122) - arrY[i] * sin(0.122);
		rot_y = arrX[i] * cos(0.122) + arrY[i] * sin(0.122);
		printf("%lf %lf\n", rot_x, rot_y);
	}
	//end of rotation of the set
	//beginning of the cumulative distance
	double  dist_0, dist_1, dist_2, dist_3, dist_4, dist_total, dist_00, dist_01, dist_02, dist_03, dist_04;
	dist_0 = (arrX[0] * arrX[0] + arrY[0] * arrY[0]);
	dist_1 = (arrX[1] * arrX[1] + arrY[1] * arrY[1]);
	dist_2 = (arrX[2] * arrX[2] + arrY[2] * arrY[2]);
	dist_3 = (arrX[3] * arrX[3] + arrY[3] * arrY[3]);
	dist_4 = (arrX[4] * arrX[4] + arrY[4] * arrY[4]);
	dist_00 = sqrt(dist_0);
	dist_01 = sqrt(dist_1);
	dist_02 = sqrt(dist_2);
	dist_03 = sqrt(dist_3);
	dist_04 = sqrt(dist_4);
	dist_total = dist_00 + dist_01 + dist_02 + dist_03 + dist_04;
	printf("total distance=");
	printf("%lf\n", dist_total);
	//end of cumulative distance
	return 0;
}
Morning.
If you create a function called createPoints() and then cut and paste lines 10 to 36 into it, you'll see that the only thing the rest of the program needs are you 2 arrays arrX and arryY, so in main(), if start it like this:

1
2
3
4
5
6
int main()
{
	int arrX[5];
	int arrY[5];

	createPoints(arrX, arrY);  // <-- this is your function to create your points. 


i'd work out the other functions in the same way (cut and paste the relevant code in and then determine which variables need to be passed in).

edit: Your main() might look like this (depending on if you want to display results in the functions themselves, or return values back to main and display them there, which i've done with the last function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	int arrX[5];
	int arrY[5];

	createPoints(arrX, arrY);
	calcCentreOfGravity(arrX, arrY);
	handleRotations(arrX, arrY);

	double distance = cumulativeDistance(arrX, arrY);
	printf("total distance=");
	printf("%lf\n", distance);

	return 0;
}
Last edited on
Building on the good advice from mutexe, You should pass the size of the arrays as a parameter. That way you can change the number of points easily:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
	const int Size=5;
	int arrX[Size];
	int arrY[Size];

	createPoints(arrX, arrY, Size);
	calcCentreOfGravity(arrX, arrY, Size);
	handleRotations(arrX, arrY, Size);

	double distance = cumulativeDistance(arrX, arrY, Size);
	printf("total distance=");
	printf("%lf\n", distance);

	return 0;
}


You will have to change your code for center of gravity and cumulative distance to compute the answers in a loop instead of hard coding points 0,1,2,3,4.
closed account (375jz8AR)
My program looks like that, it gives me 2 errors (Error 2 error C2082: redefinition of formal parameter 'arrY', and the same for arrX. and 2 warnings;Warning 3 warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning), they look the same. So i cannot run it. I add what "dhayden" told me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<stdio.h>
#include <time.h>
#include <cstdlib>
#include <math.h>
int randfun(int arrX[], int arrY[],int size);
void cenfun(int arrX[], int arrY[],int size);
void rotatefun(int arrX[], int arrY[],int size);
int distfun(int arrX[], int arrY[],int size);
int main()
{
	const int size = 5;
	int arrX[size];
	int arrY[size];

	randfun(arrX, arrY,size);
	cenfun(arrX, arrY,size);
	rotatefun(arrX, arrY,size);

	double distance = distfun(arrX, arrY,size);
	printf("total distance=");
	printf("%lf\n", distance);

	return 0;
}
int randfun(int arrX[], int arrY[],int size)
{
	srand((unsigned)time(NULL));

	int x, y;
	bool xsign, ysign;

	int arrX[5];
	int arrY[5];

	for (int i = 0; i < 5; i++)
	{
		x = rand() % 100;
		y = rand() % 100;
		xsign = rand() % 2;
		ysign = rand() % 2;

		if (xsign) x *= -1;
		if (ysign) y *= -1;

		arrX[i] = x;
		arrY[i] = y;
	}

	for (int i = 0; i < 5; i++)
	{
		printf("%3d %3d\n", arrX[i], arrY[i]);
	}
}
void cenfun(int arrX[], int arrY[],int size)
{
	double cen_x, cen_y;
	printf("centre of gravity is");
	cen_x = (arrX[1] + arrX[2] + arrX[0] + arrX[3] + arrX[4]) / 5;
	cen_y = (arrY[1] + arrY[2] + arrY[0] + arrY[3] + arrY[4]) / 5;
	printf("%lf , %lf\n", cen_x, cen_y);
}
void rotatefun(int arrX[], int arrY[],int size)
{
	for (int i = 0; i < 5; i++)
	{
		double rot_x, rot_y;
		printf("rotate points are:");
		rot_x = arrX[i] * cos(0.122) - arrY[i] * sin(0.122);
		rot_y = arrX[i] * cos(0.122) + arrY[i] * sin(0.122);
		printf("%lf %lf\n", rot_x, rot_y);
	}
}
int distfun(int arrX[], int arrY[],int size)
{
	double  dist_0, dist_1, dist_2, dist_3, dist_4, dist_total, dist_00, dist_01, dist_02, dist_03, dist_04;
	dist_0 = (arrX[0] * arrX[0] + arrY[0] * arrY[0]);
	dist_1 = (arrX[1] * arrX[1] + arrY[1] * arrY[1]);
	dist_2 = (arrX[2] * arrX[2] + arrY[2] * arrY[2]);
	dist_3 = (arrX[3] * arrX[3] + arrY[3] * arrY[3]);
	dist_4 = (arrX[4] * arrX[4] + arrY[4] * arrY[4]);
	dist_00 = sqrt(dist_0);
	dist_01 = sqrt(dist_1);
	dist_02 = sqrt(dist_2);
	dist_03 = sqrt(dist_3);
	dist_04 = sqrt(dist_4);
	dist_total = dist_00 + dist_01 + dist_02 + dist_03 + dist_04;
	printf("total distance=");
	printf("%lf\n", dist_total);

}


Last edited on
redefinition of formal parameter 'arrY'

You have (correctly) defined these arrays in your main, but then for some reason you try to create 2 more arrays in your 'randfun' function. You do not need to do this as you are passing them into the function. That's the whole point of the exercise. every arrY and arrX in your program, no matter which function you are in, are the ones you create on lines 12 and 13.

your warning probably has something to do with this:
 
xsign = rand() % 2;


you are generating a number and then trying to force it into a boolean variable.
Topic archived. No new replies allowed.