C Programming 3 Passing arrays Copypaste your code from warm

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;
}

Solution

#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;
}

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site