Question

In: Computer Science

C coding • Implement, using structures and functions as appropriate, a program which requires you to...

C coding
• Implement, using structures and functions as appropriate, a program which requires you to enter a number of points in 3 dimensions. The points will have a name (one alphanumeric character) and three coordinates x, y, and z. Find and implement a suitable way to stop the input loop. The program, through an appropriate distance function, should identify the two points which are the furthest apart. Another function should calculate the centre of gravity of the point cloud (i.e., the average of each coordinate for all points entered)

• The output should show a list of the points entered, then name and list the two that are furthest apart, and finally list the centre of gravity. Note: you should use pointers when passing structs to functions to get full marks.  

Solutions

Expert Solution

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct Point{
int x,y,z;
char name;
struct Point *next;
}Point;

Point *head=NULL;

int x_sum=0;
int y_sum=0;
int z_sum=0;
int count=0;

Point* insert(Point *head,Point *new_val)
{
count++;
x_sum+=new_val->x;
y_sum+=new_val->y;
z_sum+=new_val->z;
if(head==NULL)
{
head=new_val;
}
else
{
new_val->next = head;
head = new_val;
}
return head;
}
double getDistance(Point *p1,Point *p2)
{
int d1=p1->x-p2->x;
if(d1<0)d1=d1*-1;
int d2=p1->y-p2->y;
if(d2<0)d2=d2*-1;
int d3=p1->z-p2->z;
if(d3<0)d3=d3*-1;
double distance=d1*d1+d2*d2+d3*d3;
distance=sqrt(distance);
return distance;
}
int main()
{
int x_temp,y_temp,z_temp;
char name_temp;
int choice=0;
do{
printf("Enter point (X Y Z name)\n");
scanf("%d %d %d %c",&x_temp,&y_temp,&z_temp,&name_temp);
Point *temp=(Point *)malloc(sizeof(Point));
temp->x=x_temp;
temp->y=y_temp;
temp->z=z_temp;
temp->next=NULL;
temp->name=name_temp;
head=insert(head,temp);
  
printf("Press 1 to continue : \n");
scanf("%d",&choice);
}while(choice==1);
  
printf("POINTS :\n");
for(Point *loop=head;loop!=NULL;loop=loop->next)
{
printf("Point %c : %d %d %d \n",loop->name,loop->x,loop->y,loop->z);

}
  
  
double max=0;
Point *p1,*p2;
for(Point *temp1=head;temp1!=NULL;temp1=temp1->next)
{
for(Point *temp2=temp1;temp2!=NULL;temp2=temp2->next)
{
double distance=getDistance(temp1,temp2);
if(distance>max)
{
max=distance;
p1=temp1;
p2=temp2;
}
}
}
  
//printf("Center of Gravity : %f %f %f\n",((double)x_sum)/count,((double)y_sum)/count,((double)z_sum)/count);
printf("Center of Gravity : %d %d %d\n",x_sum/count,y_sum/count,z_sum/count);

printf("MAXIMUM DISTANT POINTS %c :%d %d %d and %c :%d %d %d at distance %f\n",p1->name,p1->x,p1->y,p1->z,p2->name,p2->x,p2->y,p2->z,max);

}

OUTPUT:


Related Solutions

For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters,...
1.Write a C++ program using control structures, arrays, functions to calculate the number of dollars, quarters, dimes, nickels, and pennies in a given amount of money. The input should be a floating-point value representing a decimal value. Example: 63.87 à 63 [dollars and 87 cents] should output 63 dollars, 3 quarters, 1 dime, 0 nickels, and 2 pennies. 2. In trigonometry the cosine function can be calculated using cos(x) = 1 – x 2 /2! + x 4 /4!- x...
Write a Python program to implement one studied entropy coding method, including two separate functions for...
Write a Python program to implement one studied entropy coding method, including two separate functions for encoding and decoding. Use the lyrics of your favorite song as the message to be processed, and test the program on the message. Include the source code, and detail the program design and execution procedure in this section.
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer...
Design and implement a C++ program with functions to calculate the pre-tax charge: If the customer subscribes to a phone plan called Plan200, then he is charged $5 for the first 200 minutes. For each additional minutes, the customer will be charged $0.10. If the customer subscribes to a phone plan called Max20, then he is charged $0.05 for each minute up to $20. (I.e., the customer never needs to pay more than $20.) If the customer is not subscribed...
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings....
C++ program assignment asks to implement the following functions. Each function deals with null terminated C-strings. Assume that any char array passed into the functions will contain valid, null-terminated data. The functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT