Answered Essay: Hello, I'm trying to change my code from using subscript to using pointers, I'm confused on how to do this. I

Hello, I’m trying to change my code from using subscript to using pointers, I’m confused on how to do this. I would appreciate an explanation but just some code reworking would be fine I can figure it out if I see it done.. Here is what I have:

https://onlinegdb.com/HkChI-r5-

Basically what I need to know or be shown is how to change ‘void enterRents’ into a for loop to input the rent amounts into an array using pointers (not subscripts) to access the array data. I understand what pointers do but I just cant wrap my head around how to edit my program. I also need to change displayRents and create a “sumRents” function to do the same thing.. sorry for the lengthy question but I’ve been working hard on this and could use some guidance.

Expert Answer

 

#include <iostream>
#include <iomanip>
using namespace std;
//Globals
const int SIZE = 5;
int property;
int rent[SIZE];

//Function Prototypes
void getMenuItem (bool hasMadeFirstSelection);
void selectionSort (int *rentp);
void displayMemoryLocations (int *rentp);
void enterRents();
int sumRents();
void displayRents();

int main ()
{
int option;
bool hasMadeFirstSelection = false;
cout << fixed << showpoint << setprecision (2);

while (true)
{

getMenuItem (hasMadeFirstSelection); // open the menu
cout << “Please make your selection: “;
cin >> option; // get input
cout << endl;
if (option == 1)
{
hasMadeFirstSelection = true;
}
while (option != 1 && !hasMadeFirstSelection) // if they didnt press 1 and also havent entered rent data yet, return invalid
{
if (option == 6)
{
break;
}
cout << “Your input was invalid, please try again.n” << endl;
cout << “Please make your selection: “;
cin >> option;
cout << endl;
if (option == 1)
{
hasMadeFirstSelection = true; // sets the first selection to 1 after they do it.
}
}
if (option == 6)
break;
switch (option) // switch for the menu items
{
case 1:
enterRents ();
cout << endl;
break;

case 2:
selectionSort (rent);
cout << endl;
break;

case 3:
displayMemoryLocations (rent);
cout << endl;
break;

case 4:
displayRents();
cout << endl;
break;
case 5:
cout << “The sum is: “<<“$” <<sumRents()<< endl;
cout << endl;
break;

default:
cout << “Your input was invalid, please try again.n” << endl; // any other input returns invalid
break;
}

}

system (“Pause”);

return 0;
}

void getMenuItem (bool hasMadeFirstSelection) // menu function
{
if (hasMadeFirstSelection)
{
cout << “Menu Of Options: n”;
cout << “1. Re-enter rent amounts n”;
cout << “2. Sort rent data (ascending) n”;
cout << “3. Display the memory location of the items in the array n”;
cout << “4. Show all rent amounts n”;
cout << “5. Sum all rent amounts n”;
cout << “6. Exit n”;
}
else
{
cout << “Menu Of Options: n”;
cout << “1. Enter rent amounts n”;
cout << “6. Exit n”;
}

}

void selectionSort (int *rentp) // sorts the data in descending order
{
int minIndex, index = 0;
double minValue = 0;
//sort taxes due
for (int startScan = 0; startScan < (property – 1); startScan++) {
minIndex = startScan;
minValue = rentp[startScan];
for (index = startScan + 1; index < property; index++) {
if (rent[index] < minValue) {
minValue = rent[index];
minIndex = index;

}
}
if (startScan != minIndex) {
//swap due amount
double temp = rent[minIndex];
rent[minIndex] = rent[startScan];

}
}
//display addresses and taxes due
int i = 0;
cout << “Rent data sorted in ascending order:nn”;
for (i = 0; i < property; i++) {
cout << left <<“$”<<rentp[i] << endl;
}
system(“pause”);
cout << “n”;
}
void displayMemoryLocations (int *rentp) // displays the memory location of the array items
{
int p = 0;
while (p < 6)
{
cout <<“The number “<< p <<” item in the array’s memory location is: ” << &rentp[p];
p++;
cout << endl;
}
}

void enterRents() // function to enter the data
{

cout << “Enter number of properties: n”;
cin >> property;
cout << “Enter the rental income of your ” << property << ” properties: n”;
for (int i = 0, count = 1; i < property; i++, count++)
{

cout << “Property ” << count << “: $”;
cin >> *(rent + i);

}
cout << endl;
}

void displayRents() // shows all the current data, using pointers
{
int d;
cout << “List of current rent incomes: n”;
for (d = 0; d < property; d++)
{
cout << “$” << *(rent + d) << endl;
}
}
int sumRents() // Calculate sum, using pointers
{
int d, sum = 0;
for (d = 0; d < property; d++)
{
sum = sum + *(rent + d);

}
return sum;
}
================================================
I have converted them into pointers and added SumRents(), have a look

See Output as well

Thanks, let me know if there is any concern

=======================

#include <iostream>
#include <iomanip>
using namespace std;
//Globals
const int SIZE = 5;
int property;
int rent[SIZE];
int* rentp = rent;

//Function Prototypes
void getMenuItem (bool hasMadeFirstSelection);
void selectionSort (int*, int);
void displayMemoryLocations (int*, int);
void enterRents(int*, int);
int sumRents(int*, int);
void displayRents(int*, int);

int main ()
{
int option;
bool hasMadeFirstSelection = false;
cout << fixed << showpoint << setprecision (2);

while (true)
{

getMenuItem (hasMadeFirstSelection); // open the menu
cout << “Please make your selection: “;
cin >> option; // get input
cout << endl;
if (option == 1)
{
hasMadeFirstSelection = true;
}
while (option != 1 && !hasMadeFirstSelection) // if they didnt press 1 and also havent entered rent data yet, return invalid
{
if (option == 6)
{
break;
}
cout << “Your input was invalid, please try again.n” << endl;
cout << “Please make your selection: “;
cin >> option;
cout << endl;
if (option == 1)
{
hasMadeFirstSelection = true; // sets the first selection to 1 after they do it.
}
}
if (option == 6)
break;
switch (option) // switch for the menu items
{
case 1:
enterRents (rentp, property);
cout << endl;
break;

case 2:
selectionSort (rentp, property);
cout << endl;
break;

case 3:
displayMemoryLocations (rentp, property);
cout << endl;
break;

case 4:
displayRents(rentp, property);
cout << endl;
break;
case 5:
cout << “The sum is: “<<“$” <<sumRents(rentp, property)<< endl;
cout << endl;
break;

default:
cout << “Your input was invalid, please try again.n” << endl; // any other input returns invalid
break;
}

}

system (“Pause”);

return 0;
}

void getMenuItem (bool hasMadeFirstSelection) // menu function
{
if (hasMadeFirstSelection)
{
cout << “Menu Of Options: n”;
cout << “1. Re-enter rent amounts n”;
cout << “2. Sort rent data (ascending) n”;
cout << “3. Display the memory location of the items in the array n”;
cout << “4. Show all rent amounts n”;
cout << “5. Sum all rent amounts n”;
cout << “6. Exit n”;
}
else
{
cout << “Menu Of Options: n”;
cout << “1. Enter rent amounts n”;
cout << “6. Exit n”;
}

}

void selectionSort (int* rentp, int property) // sorts the data in descending order
{
int minIndex, index = 0;
double minValue = 0;
//sort taxes due
for (int startScan = 0; startScan < (property – 1); startScan++) {
minIndex = startScan;
minValue = rentp[startScan];
for (index = startScan + 1; index < property; index++) {
if (rentp[index] < minValue) {
minValue = rentp[index];
minIndex = index;

}
}
if (startScan != minIndex) {
//swap due amount
double temp = rentp[minIndex];
rent[minIndex] = rentp[startScan];

}
}
//display addresses and taxes due
int i = 0;
cout << “Rent data sorted in ascending order:nn”;
for (i = 0; i < property; i++) {
cout << left <<“$”<< *(rentp + i) << endl;
}
system(“pause”);
cout << “n”;
}
void displayMemoryLocations (int *rentp, int properties) // displays the memory location of the array items
{
int p = 0;
while (p < properties)
{
cout <<“The number “<< p <<” item in the array’s memory location is: ” << &rent[p];
p++;
cout << endl;
}
}

void enterRents(int* rentp, int pro) // function to enter the data
{

cout << “Enter number of properties: n”;
cin >> property;
cout << “Enter the rental income of your ” << property << ” properties: n”;
for (int i = 0, count = 1; i < property; i++, count++)
{

cout << “Property ” << count << “: $”;
cin >> *(rentp + i);

}
cout << endl;
}

void displayRents(int *rentp, int property) // shows all the current data, using pointers
{

cout << “List of current rent incomes: n”;
for (int d = 0; d < property; d++)
{
cout << “$” << *(rentp + d) << endl;
}
}
int sumRents(int* rentp, int property) // Calculate sum, using pointers
{
int d, sum = 0;
for (d = 0; d < property; d++)
{
sum = sum + *(rentp + d);

}
return sum;
}
========
See Output

Thanks

========================================================

#include <iostream>
#include <iomanip>
using namespace std;
//Globals
const int SIZE = 5;
int property;
int rent[SIZE];
int* rentp = rent;

//Function Prototypes
void getMenuItem (bool hasMadeFirstSelection);
void selectionSort (int*, int);
void displayMemoryLocations (int*, int);
void enterRents(int*, int);
int sumRents(int*, int);
void displayRents(int*, int);

int main ()
{
int option;
bool hasMadeFirstSelection = false;
cout << fixed << showpoint << setprecision (2);

while (true)
{

getMenuItem (hasMadeFirstSelection); // open the menu
cout << “Please make your selection: “;
cin >> option; // get input
cout << endl;
if (option == 1)
{
hasMadeFirstSelection = true;
}
while (option != 1 && !hasMadeFirstSelection) // if they didnt press 1 and also havent entered rent data yet, return invalid
{
if (option == 6)
{
break;
}
cout << “Your input was invalid, please try again.n” << endl;
cout << “Please make your selection: “;
cin >> option;
cout << endl;
if (option == 1)
{
hasMadeFirstSelection = true; // sets the first selection to 1 after they do it.
}
}
if (option == 6)
break;
switch (option) // switch for the menu items
{
case 1:
enterRents (rentp, property);
cout << endl;
break;

case 2:
selectionSort (rentp, property);
cout << endl;
break;

case 3:
displayMemoryLocations (rentp, property);
cout << endl;
break;

case 4:
displayRents(rentp, property);
cout << endl;
break;
case 5:
cout << “The sum is: “<<“$” <<sumRents(rentp, property)<< endl;
cout << endl;
break;

default:
cout << “Your input was invalid, please try again.n” << endl; // any other input returns invalid
break;
}

}

system (“Pause”);

return 0;
}

void getMenuItem (bool hasMadeFirstSelection) // menu function
{
if (hasMadeFirstSelection)
{
cout << “Menu Of Options: n”;
cout << “1. Re-enter rent amounts n”;
cout << “2. Sort rent data (ascending) n”;
cout << “3. Display the memory location of the items in the array n”;
cout << “4. Show all rent amounts n”;
cout << “5. Sum all rent amounts n”;
cout << “6. Exit n”;
}
else
{
cout << “Menu Of Options: n”;
cout << “1. Enter rent amounts n”;
cout << “6. Exit n”;
}

}

void selectionSort (int* rentp, int property) // sorts the data in descending order
{
int minIndex, index = 0;
double minValue = 0;
//sort taxes due
for (int startScan = 0; startScan < (property – 1); startScan++) {
minIndex = startScan;
minValue = rentp[startScan];
for (index = startScan + 1; index < property; index++) {
if (rentp[index] < minValue) {
minValue = rentp[index];
minIndex = index;

}
}
if (startScan != minIndex) {
//swap due amount
double temp = rentp[minIndex];
rentp[minIndex] = rentp[startScan];
rentp[startScan] = temp;

}
}
//display addresses and taxes due
int i = 0;
cout << “Rent data sorted in ascending order:nn”;
for (i = 0; i < property; i++) {
cout << left <<“$”<< *(rentp + i) << endl;
}
system(“pause”);
cout << “n”;
}
void displayMemoryLocations (int *rentp, int properties) // displays the memory location of the array items
{
int p = 0;
while (p < properties)
{
cout <<“The number “<< p <<” item in the array’s memory location is: ” << &rent[p];
p++;
cout << endl;
}
}

void enterRents(int* rentp, int pro) // function to enter the data
{

cout << “Enter number of properties: n”;
cin >> property;
cout << “Enter the rental income of your ” << property << ” properties: n”;
for (int i = 0, count = 1; i < property; i++, count++)
{

cout << “Property ” << count << “: $”;
cin >> *(rentp + i);

}
cout << endl;
}

void displayRents(int *rentp, int property) // shows all the current data, using pointers
{

cout << “List of current rent incomes: n”;
for (int d = 0; d < property; d++)
{
cout << “$” << *(rentp + d) << endl;
}
}
int sumRents(int* rentp, int property) // Calculate sum, using pointers
{
int d, sum = 0;
for (d = 0; d < property; d++)
{
sum = sum + *(rentp + d);

}
return sum;
}
=================
See Output

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.