Untitled
unknown
plain_text
3 years ago
1.8 kB
4
Indexable
Never
C CODE- #include <stdio.h> #include <stdlib.h> struct point{ //define the structure to store points information int x; int y; }; int* quadrants(struct point* points, int n); //function header //main or driver method int main() { int n; struct point* points; //points int* quads; printf("Enter the value of n : "); //read the value of 'n' scanf("%d",&n); points = (struct point*)malloc(n*sizeof(struct point)); //dynamically allocate memory to points array //run a loop for 'n' times to store points information for(int i=0;i<n;i++){ printf("Enter co-ordinates of point %d : \n",i+1); //read points scanf("%d %d",&(points[i].x),&(points[i].y)); } //call the function quadrants() with correct parameters and store the result in array 'quads' quads = quadrants(points, n); //display the result printf("\nNumber of points in : \n"); for(int i=0;i<4;i++){ printf("Quadrant %d : %d\n",i+1,quads[i]); //print Quadrant content } free(points); //free memory free(quads); return 0; } //define the method quadrants() that takes in points and n as parameters //returns quadrant array of size 4, with each index storing the number of points in that quadrant int* quadrants(struct point* points, int n) { int *q = (int* )malloc(n*sizeof(int)); //create an array to store quadrants content //run a loop to iterate over points array for(int i=0;i<n;i++){ //if both x and y are +ve, then 1st quadrant if(points[i].x >= 0 && points[i].y >= 0 ){ q[0] = q[0] + 1; } //if x is -ve and y is +ve, then 2nd quadrant else if(points[i].x <= 0 && points[i].y >= 0 ){ q[1] = q[1] + 1; } //if both x and y are -ve, then 3rd quadrant if(points[i].x <= 0 && points[i].y <= 0 ){ q[2] = q[2] + 1; } //if both x is +ve and y is -ve, then 4th quadrant if(points[i].x >= 0 && points[i].y <= 0 ){ q[3] = q[3] + 1; } } return q; } IMAGE OF CODE-