Pointers, Loops, Array combo

Hello. Loved Java, but having trouble with c++. Been working for hours, and finally finished my project. I have one more small problem, and I can't figure it out and it's gotta be so simple please help.


I need to re-write this using pointers rather than arrays. It also says I have to include salary[20]; It says to name my pointer ps and to use initialize it to first space in the array and use it in the body of the loop.


ORIGINAL
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
   int salary[20];
   int i;
   for (i=0; i < 20; ++i)
   {
       cout << "Enter Salary: ";
       cin >> salary[i];
   }
   for (i=0; i < 20; ++i)
     salary[i]=salary[i]+salary[i]/(i+1);
   return 0;
}



MY CODE TRY :( probably awful. please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
#include <string>
using namespace std;

int main()
{
	int salary[20];
	int i;
	
	
	int* ps = 0;
	
	salary[0] = *ps;
	
	for (i=0; i<20; ++i)
	{
		cout << "Enter Salary: " <<endl;
		cin >> salary[*ps];
	}
	
	for (i = 0; i < 20; ++i)
	salary[i] = salary[i] + salary[i]/(i+1);
	
	return 0;
	
}



1
2
3
int* ps = 0;
	
salary[0] = *ps;

it should be :
int* ps = salary // ps now points to salary[ 0 ]

then you can dereference it just like a normal array :
1
2
3
4
5
for (i=0; i<20; ++i)
{
    cout << "Enter Salary: " <<endl;
    cin >> ps[ i ]; // <====
}
Topic archived. No new replies allowed.