Assume the following structure declaration exists for the following questions: s
ID: 3878553 • Letter: A
Question
Assume the following structure declaration exists for the following questions:
struct Rectangle
{
int length;
int width;
};
Write a function that accepts a Rectangle structure as its argument and displays the structure’s contents on the screen;
Write a function that uses a Rectangle structure reference variable as its parameter and stores the user’s input in the structure’s members;
Write a function that returns a Rectangle structure. The function should store the user’s input in the members of the structure before returning it.
Explanation / Answer
#include "stdio.h"
struct Rectangle
{
int length;
int width;
};
// PRINTNIG length and width
void printStruct(struct Rectangle a)
{
printf("Length= %d and Width=%d",a.length,a.width);
}
// taking user input of length and width
void refStruct(struct Rectangle* a)
{
// when it is pointer, we have to use a->length instead of a.length
printf("Enter length: ");
scanf("%d",&(a->length));
printf("Enter width: ");
scanf("%d",&(a->width));
}
// taking user input of length and width and returning the structure variable
struct Rectangle input()
{
struct Rectangle result;
printf("Enter length: ");
scanf("%d",&(result.length));
printf("Enter width: ");
scanf("%d",&(result.width));
return result;
}
int main(void) {
printf("** Test for printing structure ");
struct Rectangle r = {1, 2};
printStruct(r);
printf(" ** Test for reference structure ");
struct Rectangle refS;
refStruct(&refS);
printStruct(refS);
printf(" ** Test for returning structure ");
struct Rectangle inS = input();
printStruct(inS);
return 0;
}
/*SAMPLE OUTPUT
** Test for printing structure
Length= 1 and Width=2
** Test for reference structure
Enter length: 3
Enter width: 6
Length= 3 and Width=6
** Test for returning structure
Enter length: 2
Enter width: 8
Length= 2 and Width=8
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.