This problem is designed to make sure you can write a program that uses the data
ID: 3842668 • Letter: T
Question
This problem is designed to make sure you can write a program that uses the data passed into it such that the caller's data has been updated. We will use a struct, since it is the most important form of information used in Data Structures.
The Solution / Test Requirements
To do this problem, you will need to dynamically create a struct that contains 4 integer fields. The fields should be called X, Y, TheSum and TheProduct.
One function is needed to augment your test main. It is called Compute.
Compute is a function that receives the struct holding the data (X, Y, TheSum and TheProduct). How should Compute receive the struct such that the caller will see a sum of X+Y and a product of X*Y? What happens inside Compute?
Your Test Main must: 1. Declare the struct using a typedef.
2. Dynamically allocate the struct using the typedef and fill it with the following data:
a. X = 20
b. Y = -7
c. TheSum = 0
d. TheProduct = 0
3. Print out the four fields in the struct.
4. Call Compute passing the struct such that the fields in the struct can be updated
5. Print out the four fields in the struct on return from the function.
6. Free the memory that was dynamically allocated.
For your convenience, here is an output of my program:
Before call to Compute, X= 20, Y= -7, TheSum=0 and TheProduct = 0
After call to Compute, X= 20, Y= -7, TheSum=13 and TheProduct = -140
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
Explanation / Answer
#include <stdio.h>
typedef struct complex
{
int x;
int y;
int Sum;
Int Product;
} Cstruc;
int main()
{
Cstruc st ;
printf("Enter the numbers");
scanf("%d %d", &st.x, &st.y);
printf("Before call to Compute,x=%d and y=%d" st.x,st.y);
Compute(st);
return 0;
}
Cstruc Compute(st)
{
st.Sum = st.x + st.y;
st.Product = st.x + st.y;
printf("Before call to Compute,x=%d and y=%d" st.x,st.y);
printf("The Sum and product is,x=%d and y=%d" st.Sum,st.Product);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.