some mistakes

closed account (375jz8AR)
I create this code everything seems to be fine, but when I check the results, the center of gravity is always 1 or 2 units higher that the value that must have and the rotate points I think they are wrong to, but i dont know how to check it, actually I never did this before. Here is my code.
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
91
92
93
94
95
96
97
 #include<stdio.h>
#include <time.h>
#include <cstdlib>
#include <math.h>
int randfun(int arrX[], int arrY[],int size);
int cenfun(int arrX[], int arrY[],int size);
int rotatefun(int arrX[], int arrY[],int size,double center_x,double center_y);
int distfun(int arrX[], int arrY[],int size);
int main()
{
	
const int size = 6;

    int arrX[size];
	int arrY[size];
	
	randfun(arrX, arrY,size);
	double b = 0;
	double a = cenfun(arrX, arrY, size);
	rotatefun(arrX, arrY,size,a,b);
	double distance = distfun(arrX, arrY, size);
	

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

	int x, y;
	int  xsign, ysign;
	printf("random numbers are\n");

	for (int i = 0; i < size; 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 < size; i++)
	{
		printf("%3d %3d\n", arrX[i], arrY[i]);
	}
	return 0;
}
int cenfun(int arrX[], int arrY[], int size)
{
	double center_x,center_y;
	
	center_y = 0;
	center_x = 0;
	for (int i = 0; i < size; i++)
	{
		
		center_x += arrX[i] / size;
		center_y += arrY[i] / size;
		
	}
	printf("center of gravity is ");
	printf("%lf %lf\n", center_x, center_y);
	return 0;
}
int rotatefun(int arrX[], int arrY[],int size,double center_x,double center_y)
{
	for (int i = 0; i < size; i++)
	{
		double rot_x, rot_y;
		printf("rotate points are ");
		rot_x = ((arrX[i] - center_x) * cos(0.122) + center_x) - ((arrY[i] - center_y) * sin(0.122) + center_y);
        rot_y = ((arrX[i] - center_x) * cos(0.122) + center_x) + ((arrY[i] - center_y) * sin(0.122) + center_y);
		printf("%lf %lf\n", rot_x, rot_y);
	}
	return 0;
}
int distfun(int arrX[], int arrY[], int size)
{
	double  dist_total = 0,dist;
	for (int i = 0; i < size; i++)
{
		dist =(arrX[i] * arrX[i] + arrY[i] * arrY[i]);
		dist_total += sqrt(dist);
}
	
	printf("total distance is ");
	printf("%lf\n", dist_total);
	return 0;
}
Last edited on
Hi,

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
int cenfun(int arrX[], int arrY[], int size)
{
	double center_x,center_y;
	
	center_y = 0;
	center_x = 0;
	for (int i = 0; i < size; i++)
	{
		
		center_x += arrX[i] / size; /* integer division */
		center_y += arrY[i] / size; /* integer division */
		
	}
	printf("center of gravity is ");
	printf("%lf %lf\n", center_x, center_y);
	return 0;
}


As my comment indicates, you have integer division which truncates the answer. The fact that it is implicitly cast to double upon assignment is too late. So I suggest that you make all your variables double to begin with, or cast them to double as soon as practicable.

Also, you have a magic number of 0.122 in the rotate function. Best to avoid doing that, instead make it a const double variable, and refer to it by the variable name. Better yet, make the angle of rotation a parameter to the function, so one vary the amount of rotation.

Btw, the fun in your function names is unnecessary - we all know they are functions.

Hope all is well with your C programming, and seasons greetings o<:+)
Last edited on
Topic archived. No new replies allowed.