Question

Structures as Function of C++ (myprogramminglab.com assignment)

1. Define a structure named StockItem with two string fields, supplier and productName and one int field, catalogNumber. Then define a structure named Customerwith string fields name, streetAddress, city, postalCode, phone. Assume that structures named Date and Money have already been defined (representing a date and amonetary amount respectively.
Finally, define a structure named Purchase that has these fields: buyer of type Customer, itemSold of type StockItem, dateOfSale of type Date, paid of type Money,returnable of type bool.
2. In mathematics, the distance between one point (A) and another point (B), each with coordinates (x,y), can be computed by taking the differences of their xcoordinates and their y coordinates and then squaring those differences. The squares are added and the square root of the resulting sum is taken and... voila! Thedistance. Assume that Point has already been defined as a structured type with two double fields, x and y. Define a function dist that takes two Point argumentsand returns the distance between the points they represent.
3. Assume that Point has already been defined as a structured type with two double fields, x and y. Write a function, getPoint that returns a Point value whosefields it has just read in from standard input. Assume the value of x precedes the value of y in the input.

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

Prgram 1:

Definition of each structure is given below along with comments.

// Definition of the structure Date.

struct Date

{

     char date[SIZE];

};

// Definition of the structure Money.

struct Money

{

     char amount[SIZE];

};

// Definition of the structure StockItem.

struct StockItem

{

     // Declare variables.

     char supplier[SIZE];

     char productName[SIZE];

     int catalogNumber;

};

// Definition of the structure Customer.

struct Customer

{

     // Declare member variables.

     char name[SIZE], streetAdress[SIZE], city[SIZE];

     int postalCode;

     int phoneNumber;

};

// Definition of the structure Purchase.

struct Purchase

{

     // Declare the variable buyer of the

     // type structure Customer.

     Customer buyer;

     // Declare the variable itemSold of the

     // type structure StockItem.

     StockItem itemSold;

     // Declare the variable dateOfSale of the

     // type structure date.

     Date dateOfSale;

     // Declare the variable paid of the

     // type structure Money.

     Money paid;

     // Boolean function of the type structure Purchase.

     bool get()

     {

          return 0;

     }

};

To test the above stuctures the below program is provided.

Program code:

#include "stdafx.h"

#include<iostream>

const int SIZE = 20;

// Definition of the structure Date.

struct Date

{

     char date[SIZE];

};

// Definition of the structure Money.

struct Money

{

     char amount[SIZE];

};

// Definition of the structure StockItem.

struct StockItem

{

     // Declare variables.

     char supplier[SIZE];

     char productName[SIZE];

     int catalogNumber;

};

// Definition of the structure Customer.

struct Customer

{

     // Declare member variables.

     char name[SIZE], streetAdress[SIZE], city[SIZE];

     int postalCode;

     int phoneNumber;

};

// Definition of the structure Purchase.

struct Purchase

{

     // Declare the variable buyer of the

     // type structure Customer.

     Customer buyer;

     // Declare the variable itemSold of the

     // type structure StockItem.

     StockItem itemSold;

     // Declare the variable dateOfSale of the

     // type structure date.

     Date dateOfSale;

     // Declare the variable paid of the

     // type structure Money.

     Money paid;

     // Boolean function of the type structure Purchase.

     bool get()

     {

          return 0;

     }

};

int main()

{

     bool flag;

     Purchase p;

     // call the method of bool type from the structure   Purchase

     // which returns a Boolean value.

     flag = p.get();

     std::cout << flag << "\n";

     system("pause");

     // Returns a Boolean value

     return flag;

}

Sample Output:

Press any key to continue . - .

Program 2:

Definition of the function dist that takes two Point arguments and returns the distance between the points:

Note: I have tested this function using the program.

// Definition of the funct ion double dist (Point templ, Point temp2) double powerl, power2, diff // Find the difference betw

Complete program code to test the above function:

#include stdafx.h # include<103 tream> #include-lama nip> # include<math. h>using namespace std; //Point structure struct

Sample Output:

nter point A <x1.yi nter x1-value: 6 nter y1-value: 8 nter point B <x2.y2 nter x2-value 8 Enter y2-value: 9 Distance between(

Complete program code to copy:

#include "stdafx.h"

#include<iostream>

#include<iomanip>

#include<math.h>

using namespace std;

//Point structure

struct Point

{

public:

     double x;

     double y;

};

// Funtion prototype.

double dist(Point, Point);

// Definition of the function

double dist(Point temp1, Point temp2)

{

     double power1, power2, diff;

// Find the difference between x coordinates and y coordinates

     // and then square the differences.

     power1 = pow((temp1.x - temp2.x), 2);

     power2 = pow((temp1.y - temp2.y), 2);

     // Add the sqaures.

     diff = power1 + power2;

     // Apply sqaure root to the sum.

     diff = sqrt(diff);

     // Return the distance between the points.

     return diff;

}

// Definition of the main() function.

int main()

{

     Point p1, p2;

     cout<<"Enter point A (x1,y1)."<<endl;

     cout << "Enter x1-value: ";

     cin >> p1.x;

     cout << "Enter y1-value: ";

     cin >> p1.y;

     cout<<"Enter point B (x2,y2)."<<endl;

     cout << "Enter x2-value: ";

     cin >> p2.x;

     cout << "Enter y2-value: ";

     cin >> p2.y;

cout << setprecision(2) << "Distance between" << "("<< p1.x << "," << p1.y << ") and" <<

"(" << p2.x << "," << p2.y << ") is" <<

    distancexy(p1, p2)<<endl;

     system("pause");

     return 0;

}

Progarm 3:

Definition of the structure Point and the function getPoint():

Note: i have tested this function using the program.

// Define structure Point. struct Point double x; double yi // Definition of the function getPoint ( struct Point getPoint ()

Complete progarm code to test the above function:

#include stdafx.h # include<103 t ream> using namespace std; Define structure Point struct Point double x; double y // Defi

Sample output:

nter x value of the point: 10 Enter y value of the point: 5 The point is: (10.5.> Press any key to continue . - .

Complete program code to copy:

#include "stdafx.h"

#include<iostream>

using namespace std;

// Define structure Point.

struct Point

{

     double x;

     double y;

};

// Definition of the function getPoint().

struct Point getPoint()

{

     struct Point a;

     //Read the Point value.

     cout << "Enter x value of the point: ";

     cin >> a.x;

     cout << "Enter y value of the point: ";

     cin >> a.y;

     // Return the Point value.

     return a;

}

// Define main() function.

int main()

{

     // Create an object a of type structure Point.

     struct Point p;

     // Call the function getPoint().

     p = getPoint();

     // Print the Point value

     cout << "The point is: " << "(" << p.x << "," << p.y     

     << ")" << endl;

     system("pause");

     return 0;

}


answered by: gavin
Add a comment
Know the answer?
Add Answer to:
Structures as Function of C++ (myprogramminglab.com assignment)
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
  • 1. Define a structure named StockItem with two string fields, supplier and productName and one int field, catalogNumber...

    1. Define a structure named StockItem with two string fields, supplier and productName and one int field, catalogNumber. Then define a structure named Customer with string fields name, streetAddress, city, postalCode, phone. Assume that structures named Date and Money have already been defined (representing a date and a monetary amount respectively. Finally, define a structure named Purchase that has these fields: buyer of type Customer, itemSold of type StockItem, dateOfSale of type Date, paid of type Money, returnable of type...

  • Arrays of Structure in C++ (Help for codelab assignment in C++)

    1. Given a type Money that is a structured type with two int fields, dollars and cents declare an array monthlySales with 12 elements of type Money.2. Given a type Money that is a structured type with two int fields, dollars and cents. Assume that an array named monthlySales with 12 elements, each of type Moneyhas been declared and initialized.Assume that a Money variable yearlySales has also been declared. Write the necessary code that traverses the monthlySales array and adds...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • C++ 1. Your program should use named string constant for the file name to open the...

    C++ 1. Your program should use named string constant for the file name to open the file. Do not add any path to the file name because the path on your computer is not likely to exist on the computer the instructor uses to grade the program. Points may be deducted if you don't follow this instruction. 2. Split statements and comments longer than 80 characters into multiple lines and use proper indentations for the split portions. (Lab2b.cpp) Write a...

  • Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...

    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. Two get functions for data fields x and y, respectively. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

  • 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...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...

  • using c++ Assume that BlogComment is a structured type with these fields, comment (a string, the...

    using c++ Assume that BlogComment is a structured type with these fields, comment (a string, the actual comment), and two int fields: like, dislike which count the number of "likes" and "dislikes" of the comment by visitors. Assume that nComments is an int variable that holds the length of an arraynamed blogComments whose elements are of type BlogComment. This array has been declared and initialized. You may assume that the array is not empty. Assume that a string variable mostControversial...

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