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

program 8 Using the previous array program (Sort and Search), rewrite the progra

ID: 3596391 • Letter: P

Question

program 8

Using the previous array program (Sort and Search), rewrite the program processing the array using pointer notation to display the original and sorted content of the array.

Ex: *(array + x) where array is the name of the array and x is the element of the array

#include <cstdlib>

#include <iostream>

#include <stdlib.h>

#include <string.h>

#include <algorithm>

#include <fstream>

using namespace std;

int main()

{

string name[10];

int year[10];

double tution[10];

int index = 0;

ifstream fin("index.txt");

while(true)

{

fin >> name[index];

fin >> year[index];

fin >> tution[index];

index++;

if(fin.eof())

break;

}

cout << "Original Data ";

cout << "Name Year Tution ";

cout << "---------------------- ";

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

{

cout << name[i] << " " << year[i] << " " << tution[i] << " ";

}

cout << endl;

// bubble sort

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

{

for (int j = 0; j < 10; ++j)

{

if(year[i] < year[j])

{

swap(year[i],year[j]);

swap(name[i],name[j]);

swap(tution[i],tution[j]);

}

}

}

cout << "Sorted Data ";

cout << "Name Year Tution ";

cout << "---------------------- ";

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

{

cout << name[i] << " " << year[i] << " " << tution[i] << " ";

}

int yr;

cout << " Enter year: ";

cin >> yr;

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

{

if (year[i] == yr)

{

cout << "Record found: " << name[i] << " " << year[i] << " " << tution[i] << endl;

return 0;

}

}

cout << "Record not found ";

return 0;

Explanation / Answer

#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <fstream>

using namespace std;

int main()
{
string name[10];
int year[10];
double tution[10];
int index = 0;

ifstream fin("index.txt");

while(true)
{
fin >> *(name + index);
fin >> *(year + index);
fin >> *(tution+index);
index++;

if(fin.eof())
break;
}

cout << "Original Data ";
cout << "Name Year Tution ";
cout << "---------------------- ";
for (int i = 0; i < 10; ++i)
{
cout << *(name+i) << " " << *(year+i) << " " << *(tution+i) << " ";
}

cout << endl;

// bubble sort
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
if(*(year+i) < *(year+j))
{
swap(*(year+i),*(year+j));
swap(*(name+i),*(name+j));
swap(*(tution+i),*(tution+j));
}
}
}

cout << "Sorted Data ";
cout << "Name Year Tution ";
cout << "---------------------- ";
for (int i = 0; i < 10; ++i)
{
cout << *(name+i) << " " << *(year+i) << " " << *(tution+i) << " ";
}

int yr;
cout << " Enter year: ";
cin >> yr;

for (int i = 0; i < 10; ++i)
{
if (*(year+i) == yr)
{
cout << "Record found: " << *(name+i) << " " << *(year+i) << " " << *(tution+i) << endl;
return 0;
}
}

cout << "Record not found ";

return 0;
}