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

must compile the program will read names, ages and weights for up to 100 people.

ID: 3635733 • Letter: M

Question

must compile
the program will read names, ages and weights for up to 100 people. This means that you need 3 arrays of 100 elements each. Then it will sort the data based on decreasing weights and finally print a report.

Here is the code for a bubble sort function sorting 1 array:

void sort ( double x[], int n )
{
int i;
bool swapped;

do {
swapped = false;
for ( i = 0; i < n-1; i++ ) {
if ( x[i] > x[i+1] ) {
swap ( x[i], x[i+1] );
swapped = true;
}
}
} while ( swapped );
}

The reason bubble sort works is that the for loop compares each pair of adjacent values of the x array. If any of the pairs are out of order, then these values are swapped with the generic swap function and the swapped variable is set to true. Eventually there will be a time when the for loop completes without swapping any values of x. At this point the array is sorted and the outer do-while loop will end since swapped will be false.

In your code for this lab you will adapt the bubble sort code to sort 3 arrays. The testing will be done only for the weights and swapping will be done on the weght values and corresponding values in the other 2 arrays.

Given appropriate input data you should produce a report like this one:

Name Age Weight
----------- --- ------
Fred 35 250.5
Barney 31 165.0
Betty 28 110.5
Wilma 33 115.0

Here is your skeleton code:

#include <iostream>
#include <string>
#include <cassert>

using namespace std;

const int MAX = 100;

int read_data ( string names[], int ages[], double weights[], int max )
{
int n;

//
// Write this function first.
// Below here is the loop to read the input data.
//


assert ( n > 0 && n <= MAX );
return n;
}

void sort ( string names[], int ages[], double weights[], int n )
{
assert ( n > 0 && n <= MAX );

//
// Write this function last.
// Below here is the bubble sort code.
//

}

void print_report ( string names[], int ages[], double weights[], int n )
{
assert ( n > 0 && n <= MAX );

//
// Write this function second, so you can verify that you have read the data.
// Below here is the code to print the report.
//
}

int main()
{
string names[MAX];
int ages[MAX];
double weights[MAX];
int n;

n = read_data ( names, ages, weights, MAX );
assert ( n > 0 && n <= MAX );
sort ( names, ages, weights, n );
print_report ( names, ages, weights, n );

return 0;
}

Explanation / Answer

#include<iostream.h>;
#include<conio.h>;
void main()
{

int age[100],n,i,j,temp;
char name[100];

int wt[100]

void sort()

{

clrscr();

for(i=0;i<100;i++)
{
for(j=0;j<99;j++)
if(wt[j]<=wt[j+1])
{
temp=wt[j];
wt[j]=wt[j+1];
wt[j+1]=temp;

temp=age[j];
age[j]=age[j+1];
age[j+1]=temp;

temp=name[j];
name[j]=name[j+1];
name[j+1]=name;

}

}

}

void main()

{

cout<<"Enter name age and weight";

for(int i=0;i<100;++i)

{

cin>>name[i]>>age[i]>>weight[i];

}

sort();

cout<<"REPORT IS AS SHOWN";

for(int i=0;i<=100;++i)

{

cout<<name[i]<<age[i]<<weight[i];

cout<<"/n";

}

}