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.
Write a program (name it firstMiddleLast _yourInitials.java (yourInitials represents your initials) that will: 1. Ask the...
Write a program (name it firstMiddleLast _yourInitials.java (yourInitials represents your initials) that will: 1. Ask the user (include appropriate dialog) to enter their: first name middle name last name save each of the above as a String variable as follows: firstName middleName lastName 2. Print to the screen the number of characters in each of the names (first, middle and last) 3. Print to the screen total number of characters in all three names. Include appropriate dialog in your output....
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files. A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions: 1) setName to set the name member variable with a string...
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...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT