Question

Can I get some help with this problem? Can someone show how do this in C?...

Can I get some help with this problem?

Can someone show how do this in C?

Thank you for your help.

Write a C object-oriented program that reads the coordinates of two points in the plane and shows the distance between them. In order to do that implement the class Point.

   Point{
      float x;
      float y;

      Point(): void

      set (float x, float y): void
      getX( ): float (optional implementation)
      getY( ): float (optional implementation)
      distance(Point): float
   };


In your testing program app.c, read the coordinates of the points p1 and p2, and as output print twice the distance between the points p1 and p2 in order to check that p1.distance(p2) = p2.distace(p1).



Concepts in practice: C Pointers, Pointers to function, C with classes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <math.h>
#include<stdlib.h>//for dynamic memory
//class Point
//is represented using c
typedef struct {
   float x,y;  
}Point;

//method to set x,y coordinates to point
void set(Point *p, float x,float y)
{
   p->x = x;
   p->y = y;
}
//method to get x
float getX(Point *p)
{
   return p->x;
}
//method to get y
float getY(Point *p)
{
   return p->y;
}
//method to find distance between two points
float distance(Point *p1,Point *p2)
{
   float d = (getX(p2)-getX(p1))*((getX(p2)-getX(p1))) + (getY(p2)-getY(p1))*((getY(p2)-getY(p1)));
   return sqrt(d);
}
int main()
{
   //initalizing two points
   Point *p1 = (Point *)malloc(sizeof(Point));
   Point *p2 = (Point *)malloc(sizeof(Point));
  
  
   float x,y;
   //reading input and storing data
   printf("Point1 :\n x:");
   scanf("%f",&x);
   printf(" y:");
   scanf("%f",&y);
  
   set(p1,x,y);
  
   printf("Point2 :\n x:");
   scanf("%f",&x);
   printf(" y:");
   scanf("%f",&y);
  
   set(p2,x,y);
  
  
   //now finding distance
   printf("Distance :%f\n",distance(p1,p2));
  
   return 0;
}

output:

Point1 :
x:25
y:15
Point2 :
x:35
y:10
Distance :11.180340


Process exited normally.
Press any key to continue . . .


Add a comment
Know the answer?
Add Answer to:
Can I get some help with this problem? Can someone show how do this in C?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the...

    INSTRUCTIONS: I NEED TO CREATE A PointApp PROGRAM THAT USES THE FOLLOWING API DOCUMENTATION. Below the API Documentation is the code I submitted. However, the output is different for what they are asking. I am looking for someone to fix the code to print out the correct output and to add comments so I can have an idea in how the code works. PLEASE AND THANK YOU. API DOCUMENTATION: public class Point extends java.lang.Object The Point class is used to...

  • need some help on these a little contested right now the answers i have are 18...

    need some help on these a little contested right now the answers i have are 18 : d 19 : e 20 : c would appriacte why i am right or wrong thank you sorry i added the picture of 17 by mistake 19. The default constructor sets x and y to (0,0) by calling the second constructor. What could be used to replace / missing code */ so that this works as intended? a b = = 0; 0;...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • CAN YOU HELP ME TO DO THIS EXERCISE PLEASE USING C++ Write a program that gives...

    CAN YOU HELP ME TO DO THIS EXERCISE PLEASE USING C++ Write a program that gives the user two options: (1) Transform a temperature value from Celsius to Fahrenheit or (2) Transform a temperature value to Fahrenheit to Celsius. The two options are communicated to the user by the console by means of "cout" with this text: "If you want to transform from Fahrenheit to Celsius between character C (capital)." "If you want to transform from Celsius to Fahrenheit between...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with...

    Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with x-and y-coordinates. The class contains: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point (0, 0). A constructor that constructs a point with specified coordinates using passed in arguments. Two get functions for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint...

  • Need Help! in English Can not get program to run  plz include detailed steps as comments of...

    Need Help! in English Can not get program to run  plz include detailed steps as comments of what i did not do what I have done so far class Point: def __init__(self): self._x = 0 self._y = 0 def getX(self): return self._x def setX(self, val): self._x = val def getY(self): return self._y def setY(self, val): self._y = val def __init__(self, initX = 0, initY = 0): self._x = initX self._y = initY def __str__(self): return '<'+str(self._x)+', '+str(self._y)+ '>' def scale(self, factor):...

  • i need this in C# please can any one help me out and write the full...

    i need this in C# please can any one help me out and write the full code start from using system till end i am confused and C# is getting me hard.I saw codes from old posts in Chegg but i need the ful complete code please thanks. Module 4 Programming Assignment – OO Design and implementation (50 points) Our Battleship game needs to store a set of ships. Create a new class called Ships. Ships should have the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT