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

C++ Take a user specified input file the file will contain a list of 10 people a

ID: 3701882 • Letter: C

Question

C++

Take a user specified input file

the file will contain a list of 10 people and their heights

the file is formated as:

Last name

First Name

Height

etc for all 10 people in the list

Read the list into 3 parallel arrays, and then sot them as:

If two people are different height, the shorter person goes first on the list.

if two people are the same height, then it goes alphebetically by their last name

If two people have the same height and last name, then it goes alphebetically by their first name.

output the sorted list on the screen

use strcmp function to compare names and c-strings to define strings (char firstName[20])

Explanation / Answer

// Below is the code

// Explanation in comments

#include <bits/stdc++.h>
#include <fstream>
using namespace std;
void replace(string *l, string *f,int *h, int i, int j) // this function swaps contents across positions i,j in all three arrays simultaneously
{
{
string t1,t2; // temporary variables used for swapping
int t3;
t1=*(l+i);
t2=*(f+i);
t3=*(h+i);
*(l+i)=*(l+j);
*(f+i)=*(f+j);
*(h+i)=*(h+j);
*(l+j)=t1;
*(f+j)=t2;
*(h+j)=t3;
}
}
int main()
{
printf("Enter the file name with extension: "); // Prompt
string file_name;
cin>>file_name;
ifstream file;

file.open(file_name.c_str()); //opening filename requires C type string and not C++ style string
// creation and initialization of parallel arrays
string last_name[10]={""};
string first_name[10]={""};
int height[10]={0};
while (!file.eof()) // this loop reads the 10 entries in the parallel arrays as specified in the format
{
for(int i=0;i<10;i++)
{
file>>last_name[i]>>first_name[i]>>height[i];

if(file.eof())
break;
}

}
file.close(); // closing the file
for(int i=0;i<9;i++) // sorting according to given criteria
{
for(int j=i+1;j<10;j++)
{
if( height[j] < height[i] ) // initially by height
replace(last_name,first_name,height,i,j);
else if( height[j] == height[i] && strcmp(last_name[j].c_str(),last_name[i].c_str())<0 ) // then by last name
replace(last_name,first_name,height,i,j);
else if ( height[j] == height[i] && strcmp(last_name[j].c_str(),last_name[i].c_str())==0 && strcmp(first_name[j].c_str(),first_name[i].c_str())<0 ) // then by first name
replace(last_name,first_name,height,i,j);
}
}

for(int i=0;i<10;i++) // this loop then prints the contents of the arrays
cout<<first_name[i]<<" "<<last_name[i]<<" "<<height[i]<<" ";

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote