In: Computer Science
1) Write a program that defines a structure that represents a point. Your point structure should contain members for the X and Y value. Write a function called distance that takes two points as parameters and returns the distance between them. Your program should define & print two points and display the distance between them.
Copy your structure without formatting:
Copy your distance function without formatting:
2) Write a C program that contains an array that contains the
following value: {10, 3.24, “Exercise”}. Print the values. (Hint:
void pointers can point to any type.) Sample output:
./a
things[0] = 0x6000003a0: 10
things[1] = 0x6000003c0: 0.00
things[2] = 0x6000003e0: Exercise
Copy the part of your code where you declare the array without formatting:
Copy the part of your code where you fill the array without formatting:
-----------------------------------------------------------------------------------------------------------------------------------------
Please include comments and why you did what you did!!
#include<stdio.h>
int main()
{
void *things[10];
int a=10;
double b=2.34;
char *ch="hello";
things[0]=&a; //void pointer point to integer
things[1]=&b; //void pointer point to double
things[2]=&ch; //void pointer point to array of
characters
printf("things[0]=%d\n", * (int*) things[0]); //cast things[0] to
int , direct dereference wont work in c
printf("things[1]=%f\n", * (double*) things[1]);
printf("things[2]=%s\n", (char *)(* (char **)(things[2]))); //cast
thigs[2] to char** and then cast it into char *
}
/*
sai@sai-Lenovo-G50-80:~/C$ ./voidPointers
things[0]=10
things[1]=2.340000
things[2]=hello
/*
#include<iostream>
#include<math.h>
using namespace std;
struct point
{
double x; //to store x and y
double y;
};
void display(struct point p)
{
cout<<"( "<<p.x<<" , "<<p.y <<"
)";
}
double distance(struct point p, struct point q)
{
return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y)); //distance
between two points formula,sqrt is math function for square
root.
}
int main()
{
struct point p;
p.x=1;
p.y=4;
struct point q;
q.x=1.2;
q.y=2.5;
display(p);
display(q);
cout<<"="<<distance(p,q);
}
/*
( 1 , 4 )( 1.2 , 2.5 )=1.51327
*/