Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Partially filled arrays Copy-paste your code from warm-up 3 into a new file a

ID: 3799728 • Letter: 1

Question

1) Partially filled arrays Copy-paste your code from warm-up 3 into a new file as a starting point for this problem. Modify your main() function to allow any number of names and heights to be entered (instead of just 2). You will need to use a partially filled array to do this. (You can assume that there will not be more than 100 people.)

My Code from warmup 3:

#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[]){
    cout << "If " << fullName[0] << " and " << fullName[1]
    << " stand on top of each other, their combined height will be "
    << (height[0] + height[1])<<endl;
}

Explanation / Answer

//C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>

using namespace std;

string requestName();
double requestHeight(string fullName);
int requestNumberOfPartners();
void printArray(string fullName[], double height[], int size);

int main()
{
int size= requestNumberOfPartners();
string fullName[size];
double height[size];

for(int i = 0; i < size; i++)
{
fullName[i] = requestName();
height[i] = requestHeight(fullName[i]);
}
printArray(fullName, height, size);
}

string requestName()
{
string name;
cin.ignore();
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[], int size)
{
double sum = 0;
for (int i = 0; i < size; ++i)
{
sum = sum + height[i];
}

cout << " If ";
for (int i = 0; i < size-1; ++i)
{
cout << fullName[i] << " and ";
}
cout << fullName[size-1];

cout << " stand on top of each other, their combined height will be " << sum << endl;
}

/*
output:

How many partners are there? 5
Please enter full name: Peter parker
Please enter Peter parker's height: 4.5

Please enter full name: james arthur
Please enter james arthur's height: 3.4

Please enter full name: Ruskin bond
Please enter Ruskin bond's height: 3.2

Please enter full name: Duska japta
Please enter Duska japta's height: 6.7

Please enter full name: mark craig
Please enter mark craig's height: 6.5

If Peter parker and james arthur and Ruskin bond and Duska japta and mark craig stand on top of each other, their combined height will be 24.3

*/