Help with creation of 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. The problem is that i dont understand how can i do it, i check the tutorial, but i didn't solve my problem.
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
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <math.h>
using namespace std;


int main()
{
	//beginning of creation of random numbers
	srand(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_0 = sqrt(arrX[0] * arrX[0] + arrY[0] * arrY[0]);
	dist_1 = sqrt(arrX[1] * arrX[1] + arrY[1] * arrY[1]);
	dist_2 = sqrt(arrX[2] * arrX[2] + arrY[2] * arrY[2]);
	dist_3 = sqrt(arrX[3] * arrX[3] + arrY[3] * arrY[3]);
	dist_4 = sqrt(arrX[4] * arrX[4] + arrY[4] * arrY[4]);
	dist_total = dist_0 + dist_1 + dist_2 + dist_3 + dist_4;
	printf("total distance=");
	printf("%lf\n", dist_total);
	//end of cumulative distance
	return 0;
}

Last edited on
Topic archived. No new replies allowed.