C++ Programming: 3) Passing arrays Copy-paste your code from warm-up 2 into a ne
ID: 3928395 • Letter: C
Question
C++ Programming:
3) Passing arrays
Copy-paste your code from warm-up 2 into a new file as a starting point for this problem. In main() there is a cout statement. Make a new function that displays the same information as this cout and call that function in main() instead. (Hint: Cut and paste the cout statement into a fuction and pass the correct argument.)
This is my warm-up 2 progrmming:
#include <iostream>
using namespace std;
string requestName();
double requestHeight(string fullName);
int requestNumberOfPartners();
int main()
{
string fullName[2];
double height[2];
for(int i = 0; i < 2; i++)
{
fullName[i] = requestName();
height[i] = requestHeight(fullName[i]);
}
cout << "If " << fullName[0] << " and " << fullName[1]
<< " stand on top of each other, their combined height will be "
<< (height[0] + height[1])<<endl;
}
string requestName()
{
string name;
cout << "Please enter full name: ";
getline(cin, name);
return name;
}
double requestHeight(string fullName)
{
double height;
cout << "Please enter " << fullName << "'s height: ";
cin >> height;
cin.ignore(2, ' ');
return height;
}
int requestNumberOfPartners()
{
int numberOfPartners;
cout << "How many partners are there?";
cin >> numberOfPartners;
return numberOfPartners;
}
Explanation / Answer
#include <iostream>
using namespace std;
string requestName();
double requestHeight(string fullName);
int requestNumberOfPartners();
void printArray(string fullName[], double height[]);
int main(){
string fullName[2];
double height[2];
for(int i = 0; i < 2; i++){
fullName[i] = requestName();
height[i] = requestHeight(fullName[i]);
}
printArray(fullName, height);
}
string requestName(){
string name;
cout << "Please enter full name: ";
getline(cin, name);
return name;
}
double requestHeight(string fullName){
double height;
cout << "Please enter " << fullName << "'s height: ";
cin >> height;
cin.ignore(2, ' ');
return height;
}
int requestNumberOfPartners(){
int numberOfPartners;
cout << "How many partners are there?";
cin >> numberOfPartners;
return numberOfPartners;
}
void printArray(string fullName[], double height[]){ //pass in the arrays fullName and height into this //function which prints out the final output
cout << "If " << fullName[0] << " and " << fullName[1]
<< " stand on top of each other, their combined height will be "
<< (height[0] + height[1])<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.