Question

Question 5: Three structs are nested as in the following struct P{ Q q1; }; struct...

Question 5: Three structs are nested as in the following

struct P{

Q q1;

};

struct Q{

R r1;

};

struct R{

int x[10];

};

Implement each of the functions (Marks 10)

a. readValues1(R&)

b. readValues2(Q&)

c. readValues3(P&)?

USE C++

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

Code:

#include <iostream>
using namespace std;
struct R {
int x[10];
};
struct Q {
R r1;
};
struct P {
Q q1;
};
void readValues1(R& r)
{
cout<<"Enter 10 numbers: ";
for(int i=0;i<10;i++)
{
cin>>r.x[i];
//cout<<r.x[i]<<" ";
}
}

void readValues2(Q& q)
{
cout<<"Enter 10 numbers: ";
for(int i=0;i<10;i++)
{
cin>>q.r1.x[i];
//cout<<q.r1.x[i]<<" ";
}
}

void readValues3(P& p)
{
cout<<"Enter 10 numbers: ";
for(int i=0;i<10;i++)
{
cin>>p.q1.r1.x[i];
//cout<<p.q1.r1.x[i];
}
}
int main() {
   R r;
   readValues1(r);
   Q q;
   readValues2(q);
   P p;
   readValues3(p);
   return 0;
}

Image of the Code:

Explanation:

  • We used the '.' (dot operator) to access the elements of the struct.
  • Use normal cin and cout to read and print values.

If the answer helped please upvote. It means a lot. For any query comment below.

Add a comment
Know the answer?
Add Answer to:
Question 5: Three structs are nested as in the following struct P{ Q q1; }; struct...
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
  • QUESTION 6 What is the output of following C code? struct numbers     {         int x...

    QUESTION 6 What is the output of following C code? struct numbers     {         int x = 2;         int y = 3;     } int main() {   struct numbers nums;         nums.x = 110;         nums.y = 100;         printf("%d\n%d", nums.x, nums.y);         return 0; } A. Compile-time Error B. 110 100 C. 2 3 D. Run-time Error 2 points    QUESTION 7 What is the output of following C code? typedef struct student {         char *stud; }s1; int main() {   s1 s;         s.stud...

  • In this exercise, you will make a struct that contains x and y coordinates: struct point...

    In this exercise, you will make a struct that contains x and y coordinates: struct point {int x; int y;}; The object of this exercise is to make a singly linked list of these point structures. Add one more member called "next" to your point struct. It is a pointer to the next member of the linked list. The next member of the last point struct should point to NULL, which indicates the end of the list. Your program will...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • In the following code, A and B are constants defined with #define: 1 typedef struct {...

    In the following code, A and B are constants defined with #define: 1 typedef struct { 2 int x[A][B]; /* Unknown constants A and B */ 3 long y; 4 } str1; 5 6 typedef struct { 7 char array[B]; 8 int t; 9 short s[A]; 10 long u; 11 } str2; 12 13 void setVal(str1 *p, str2 *q) { 14 long v1 = q-<t; 15 long v2 = q-<u; 16 p-<y = v1+v2; 17 } gcc generates the following...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • What does the following nested loop print out? for (int r=1; r <=5; ++r) { double...

    What does the following nested loop print out? for (int r=1; r <=5; ++r) { double x =10.0*r; for (int c=1; c<=5; ++c) { System.out.printf("%5.2f",c/x); } System.out.println(); }

  • Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have...

    Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have set up a program in Visual Studio that will use operation between fractions. I have written a basic code example that does all the resources that you need to follow. Your job is to complete the exercises presented below in quest2.cpp. For this assignment you will learn and demonstrate how to: Work with functions use Past-by-Value and Past-by-Reference. use structs. use cin to get...

  • Consider: typedef struct { int x; int y; } Point; Point p; Point* pp; Assign the...

    Consider: typedef struct { int x; int y; } Point; Point p; Point* pp; Assign the coordinates (10, 20) to point p. choose the following Multiple choice question 1.) p[x] = 10; p[y] = 20; 2.) p->x = 10; p->y = 20; 3.) p(x) = 10; p(y) = 20; 4.) p.x = 10; p.y = 20;

  • Implement the following functions with the indicated multiplexers and control inputs. 1. f(P,Q,R) = (P' +...

    Implement the following functions with the indicated multiplexers and control inputs. 1. f(P,Q,R) = (P' + Q')R', 8:1 multiplexer

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