### Pointer noob needs help

So we had a lecture on pointers... WOW a lot of things I dont understand.

Here is the instructions for one of my programs.

Write a program that will prompt a manager for the salaries of his employees and let the manager know the effect of giving each a 10% salary increase.
Assume the manager has 6 employees.

Use pointer notation (not array notation) for the whole program.
- in main():
-prompt the user for 6 salaries, each number must be validated using a while loop as if cant be necative or 0, store these values in an array.

- from main call a function that will incremembt each salary by 10% and store this in another array.

- From main call another function that will take both arrays, calculate the differece betweeen the original and incrementd salaries for each employee , sum these differentials and display the sum. This function should display the original salaries and the salaries with the 10% increase;

Here is my code so far... I am confused on when it comes to functions if I have to keep it in pointer notation.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````//Nico Kalin OConnell #include using namespace std; void incremented(int *); const int AMOUNT = 6; int main() { int count = 0; int original[AMOUNT]; int salary; int *originalPtr = nullptr; originalPtr = original; while (count < AMOUNT) { cout << "Amount of sallary number " << count + 1 << ": "; cin >> salary; originalPtr[count] = salary; count++; } incremented(originalPtr); } void incremented(originalPointer) { int incremented[AMOUNT]; int *incrementedPtr = nullptr; incrementedPtr = incremented; for (int count = 0; count < AMOUNT; count++) *incrementedPtr[count] = *originalPointer[count] * 1.10; } void display() { int total = 0; for (int count = 0; count < AMOUNT; count++) { total += (*incrementedPtr[count] - *originalPtr[count]); } }``````
Here's a start:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; void display(double*, int); void increment(double*, double*, int); int main() { const int NO_EMPLOYEES{6}; double* salary_a = nullptr; salary_a = new double[NO_EMPLOYEES]; double* salary_b = nullptr; salary_b = new double[NO_EMPLOYEES]; int count{0}; while ( count < NO_EMPLOYEES ) { cout << "Enter salary: "; cin >> salary_a[count]; if (salary_a[count] <= 0) cout << "Salary invalid do again\n"; else count++; } cout << '\n'; cout << "Salary list A\n"; display(salary_a, NO_EMPLOYEES); increment(salary_a, salary_b, NO_EMPLOYEES); cout << "Incremented salary list B\n"; display(salary_b, NO_EMPLOYEES); return 0; } void display(double* pay, int N) { for (int i = 0; i < N; i++) { cout << "Employee " << i + 1 << " Pay \$" << pay[i] << '\n'; } cout << '\n'; } void increment(double* pay_1, double* pay_2, int N) { for (int i = 0; i < N; i++) { pay_2[i] = 1.1 * pay_1[i]; } }``````

 ```Enter salary: 1 Enter salary: 2 Enter salary: 3 Enter salary: 4 Enter salary: 5 Enter salary: 0 Salary invalid do again Enter salary: 6 Salary list A Employee 1 Pay \$1 Employee 2 Pay \$2 Employee 3 Pay \$3 Employee 4 Pay \$4 Employee 5 Pay \$5 Employee 6 Pay \$6 Incremented salary list B Employee 1 Pay \$1.1 Employee 2 Pay \$2.2 Employee 3 Pay \$3.3 Employee 4 Pay \$4.4 Employee 5 Pay \$5.5 Employee 6 Pay \$6.6 Program ended with exit code: 0```
is there a reason you passed the number of employees vs using a global constant?
also, why do we always make the pointer null?
 ``1234567891011`` ``````void display(double* pay, int N) { if ( pay ) { for (int i = 0; i < N; i++) { cout << "Employee " << i + 1 << " Pay \$" << pay[i] << '\n'; } cout << '\n'; } }``````

We can test whether pointer is null or not. If it is null, then we know that it does not point to anywhere.

Also in (although one should not need to use `new` and `delete` directly most of the time):
 ``1234567`` ``````int* aa; // uninitialized delete aa; // logical error, attempt to deallocate memory that is at random address int* bb = nullptr; if ( cond ) bb = new int {42}; // bb could be null or point to dynamically allocated block of memory delete bb; // OK, delete knows to do nothing when pointer is null ``````

A function that depends on global variable is bound to that global variable. If your program needs arrays of different size, then the function cannot handle them all. Passing data via parameters makes function more generic, more reusable.
Last edited on
thanks for the info!
Topic archived. No new replies allowed.