Question: Using the previous array program (Sort and Search), rewrite the program using a pointer to manipulate and display the original and sorted contents of the
Using the previous array program (Sort and Search), rewrite the program using a pointer to manipulate and display the original and sorted contents of the array.
Note: Use array++ where array is the name of the array.
Also
1. For search and sort functions, use the pointer (array address) as the starting point and the last element as the ending point,
2, Manipulating arrays, do not use subscripts to assign or compare array elements,
3. Set each array to a pointer and pass the pointer as a function parameter,
4. Please output all currency values using two decimal places.
This is my previous array program
#include
#include
#include
#include
using namespace std;
typedef struct Student
{
string name;
int year;
double fee;
}Student;
void bubbleSort(Student Array[], int n)
{
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (Array[j].year > Array[j + 1].year)
{
Student temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
}
}
//Prints the array of structures to screen.
void printRecords(Student Array[], int n)
{
for (int i = 0; i < n; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
}
//Linear Search which takes the array of structures, and the year as input, and return
//the index in which the year is found, or -1 if the year is not in the array.
int linearSearch(Student Array[], int n, int year)
{
for (int i = 0; i < n; i++)
if (Array[i].year == year)
return i;
return -1;
}
int main()
{
ifstream fin;
fin.open("TuitionFee.txt");
Student Array[10];
int count = 0;
for (int i = 0; i < 10; i++) //For a total of 10 records, reads the data from file to structure.
{
if (fin.eof())
break;
fin >> Array[i].name;
fin >> Array[i].year;
fin >> Array[i].fee;
count++;
}
printRecords(Array, count); //Prints the raw data i.e., in original order.
cout << endl << endl;
bubbleSort(Array, count); //Sorts the array.
printRecords(Array, count); //Prints the sorted data.
int year;
cout << "Enter the year for the records: "; //Prompt the user to enter a year.
cin >> year;
//Use the Linear Search algorithm to find the record with that year
int index = linearSearch(Array, count, year);
//Then, display the found record, if record not found, print Not Found.
if (index == -1)
cout << "Not Found." << endl;
else
cout << Array[index].name << " " << Array[index].year << " " << Array[index].fee << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
