Question

In: Computer Science

​​​​​1) Write a program that defines a structure that represents a point. Your point structure should...

​​​​​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!!

Solutions

Expert Solution

#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

*/


Related Solutions

use python 1. Write a program that a. defines a list of countries that are members...
use python 1. Write a program that a. defines a list of countries that are members of BRICS (Brazil, Russia, India, China, Sri Lanka) b. Check whether a country is a member of BRICS or not Program run Enter the name of country: Pakistan Pakistan is not a member of BRICS Enter the name of country : India India is a member of BRICS 2. Write a program to create a list of numbers in the range of 1 to...
Write a program to compute 2 point Gauss quadrature on a single interval. Your input should...
Write a program to compute 2 point Gauss quadrature on a single interval. Your input should be a function with f,a,and b. Verify it by checking that the error in using the rule for f(x)=x^3 - 3x^2 +x - 7 is zero. In matlab please.
1. Write a Python program that performs the following: 2. Defines an array of integers from...
1. Write a Python program that performs the following: 2. Defines an array of integers from 1 to 10. The numbers should be filled automatically without the need for user inputs 3. Find the sum of the numbers that are divisible by 3 (i.e., when a number is divided by 3, the remainder is zero) 4. Swap the positions of the maximum and minimum elements in the array. First, you need to find the maximum element as shown in the...
Write a Java program that will display different messages depending on your age. Your program should...
Write a Java program that will display different messages depending on your age. Your program should ask the user for his/her name and their age in years and give one or more answers from the following ones below: if the age of the user is less than 16, the program should print on the screen “You are not allowed to drive at the moment”. if the age of the user is less than 18, the program should print on the...
In this problem, you will write a program that reverses a linked list. Your program should...
In this problem, you will write a program that reverses a linked list. Your program should take as input a space-separated list of integers representing the original list, and output a space-separated list of integers representing the reversed list. Your algorithm must have a worst-case run- time in O(n) and a worst-case space complexity of O(1) beyond the input. For example, if our input is: 5 7 1 2 3 then we should print: 3 2 1 7 5 Please...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
Write a program that creates an image of green and white horizontal stripes. Your program should...
Write a program that creates an image of green and white horizontal stripes. Your program should ask the user for the size of your image, the name of the output file, and create a .png file of stripes. For example, if the user enters 10, your program should create a 10x10 image, alternating between green and white stripes. A sample run of the program: Enter the size: 10 Enter output file: 10stripes.png Another sample run of the program: Enter the...
Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
Write a program that defines an animal data type, with an animal name, age, and category...
Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. (ex. structType *arr[size];) Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program...
Write a program that defines an animal data type, with an animal name, age, and category...
Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers. Your program will prompt the user to enter the data for as many animals as they wish. It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure. Your program will then print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT