Question

In: Computer Science

Illustrate, by example, how a C++ struct may be passed as a parameter by value or...

Illustrate, by example, how a C++ struct may be passed as a parameter by value or by reference. Also, show how it can be returned from a function. Be thorough in your example and explain your code.

Solutions

Expert Solution

Solution:

Passing a structure as parameter by reference is just same as passing an integer as reference.

In the same way a structure can be returned if it is declared as a return type.

In the example I have created a structure called pointer with 2 attributes x and y.

I used a call by reference function to set the values of the structure point.

I also defined a function midPoint which takes 2 points(structure) as argument and returns the midpoint which is also of type point.

Code:

#include<iostream>

using namespace std;

//Creating a structure for point
struct point
{
        float x;
        float y;
};

//A fuction that uses call by reference to set values to the point
void setPoint(point &p);

//A function that returns midPoint of 2 points
point midPoint(point a,point b);

//Driver function
int main()
{
        //Declaring two points a and b
        point a,b;
        
        //Setting values to point a
        a.x=0;
        b.y=0;
        
        //Call by reference to set values to the point B
        setPoint(b);
        
        //Creating a  Point for midpoint
        point m;
        m=midPoint(a,b);
        
        //Printing the details
        cout<<"Point a ("<<a.x<<","<<a.y<<")"<<endl;
        cout<<"Point b ("<<b.x<<","<<b.y<<")"<<endl;
        cout<<"MidPoint ("<<m.x<<","<<m.y<<")"<<endl;
        
        
}

void setPoint(point &p)
{
        cout<<"Enter value of x and y : ";
        cin>>p.x;
        cin>>p.y;
}

point midPoint(point a,point b)
{
        point m;
        m.x=(a.x+b.x)/2;
        m.y=(a.y+b.y)/2;
        
        return m;
}

Output:


Related Solutions

Use an example to illustrate the difference between parameter, estimator, and estimate.
Use an example to illustrate the difference between parameter, estimator, and estimate.
Illustrate with an example how comparability concept/assumption may be applied to accounting for inventory.
Illustrate with an example how comparability concept/assumption may be applied to accounting for inventory.
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
Illustrate with an example how faithful representation concept/assumption may be applied to accounting for inventory.
Illustrate with an example how faithful representation concept/assumption may be applied to accounting for inventory.
how to use the filename to be passed to the given function in c ? for...
how to use the filename to be passed to the given function in c ? for example the implementation accepts one command line argument, which is a filename. The filename is a file containing a starting state to be passed to the given function initial() ,which will produce the initial state of the game.
C++ Build a struct with many data members inside (for example, a large array). Design any...
C++ Build a struct with many data members inside (for example, a large array). Design any function that processes the data inside the struct (e.g. adding a few values together and returning the sum.) Write two versions of this function, one that passes by value and one that passes a const reference. Measure the time required to call each function 10,000 times.
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct...
list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct ListNode *list_prepend(struct ListNode *list, long value); int list_length(struct ListNode *list); struct ListNode *list_remove(struct ListNode *list, long value); #endif /* LIST_H_ */ given.c file #include #include "list.h" struct ListNode *list_prepend(struct ListNode *list, long value) { struct ListNode *node = malloc(sizeof(struct ListNode)); node->value = value; node->next = list; return node; } list.c file #include #include "list.h" /* Counts and returns the number of nodes in the...
Q1. Write an algorithm to do the following operation on an array passed as parameter: If...
Q1. Write an algorithm to do the following operation on an array passed as parameter: If an element of the array is having more than one occurrence, then keep the first occurrence and remove all other occurrences of the element.
A: Provide a numerical example of how Earned Value Management may mislead us as to whether...
A: Provide a numerical example of how Earned Value Management may mislead us as to whether the project in on time and on budget.  Use the Critical Chain concepts if you want to help illustrate your answer. B: What must management do to implement Critical Chain and how do they do it.  How must management focus change for Critical Chain to be successful compared to traditional project management?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT