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...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
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...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the...
***C++ Coding*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Please include comments to understand code. Requirements Implement the following functions: int readData( int **arr) arr is a pointer to pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT