Get the following C++ code, Lab4.cpp /* Lab 4 works with a struct type called he
ID: 3795547 • Letter: G
Question
Get the following C++ code, Lab4.cpp
/*
Lab 4 works with a struct type called height. It allows the user
to enter heights, have heights grow and print heights.
*/
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int inches, // number of inches to grow, user entered
inch; // counter for inches grown so far
cout << "Enter the first person's height";
input_height (p1);
cout << " Enter the second person's height";
input_height (p2);
cout << " Person 1";
print_height (p1);
cout << " Person 2";
print_height (p2);
cout << " Enter a number of inches for Person 1 to grow: ";
cin >> inches;
for (inch = 0; inch < inches; inch++)
grow (p1);
cout << " Person 1 after growing " << inches << " inches";
print_height (p1);
cout << " ";
return 0;
}
Declare a struct called height before main. height has two int fields called feet and inches.
Declare two variables of type height in main called p1 and p2.
Write a prototype and function definition for a function called input_height. It takes one call by reference parameter of type height. It does not return a value. It prompts the user for a number of feet and inches for a person and sets these fields for the given parameter.
Write a prototype and function definition for a function called grow. It takes one call by reference parameter of type height. It does not return a value. It increases the height of its parameter by 1 inch. The function should change feet when appropriate.
Write a prototype and function definition for a function called print_height. It takes one call by value parameter of type height. It does not return a value. It prints the number of feet and inches in its parameter in a nicely labeled fashion. For example:
feet: 6
inches: 3
The program should now run: prompts the user for 2 heights, prints the heights entered, prompts the user for an amount to grow and prints the height after growing.
1. Consider the following three statements as possibilities to place in main. For each one indicate whether or not the statement would compile for the program as written. If they would not compile, explain clearly what is wrong.
p1 = 7; // a.
height.feet = 4; // b.
feet = 6; // c.
2. Comment out the prompt and call to input_height for p2. What values are printed for p2 and why?
Change your program back.
3. Change the parameter in grow, prototype and definition, to call by value. Run your program (it should run!). How does this change the output and why?
Explanation / Answer
function definition for a function called input_height:
void input_height(height* p) {
cout<<"Enter Feet: ";
cin>>p->feet;
cout<<"inchs: ";
cin>>p->inches;
}
Write a prototype and function definition for a function called grow.
void grow(height *p) {
p->inches = p->inches + 1;
if(p->inches == 12) {
p->inches = 0;
p->feet = p->feet + 1;
}
}
Write a prototype and function definition for a function called print_height.
void print_heighr(height p) {
cout<<"Feet: "<<p.feet<<" inches: "<<p.inches;
}
1. a> p1 = 7; will give an error incompatible types when assigning to type 'height' from type 'int'
we get the error because p1 is a structure, not an integer, so we cannot assign such values.
b> height.feet = 4; will give an error expected identifier..., because we cannot use dot operator on a type.
Dot operator can be used only with structure/union type variable to access data members.
c> feet = 6; will give an error 'feet' undeclared (first use in this function), since we have not declared feet in the main function. feet is a member variable of height structure, but not of the class
2. Please clarify which prompt this question is talking about in the comment section, I shall answer u at the earliest!
3. If we change the parameter to call by value the changes will not be reflected in the main function, since with call by value parameters, the function grow will create its own data type height with values copied there. changing the local variable will be reflected locally and will be erased once the function gets revoked. It does not change the variables declared in the main...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.