Pointer Program Help (Completely Lost)

So, I could really use some help, and an explanation here. I had a lot of trouble with my Pointers test and am trying to understand what to do with this assignment. It requires that, using a program I have previously written, that I change it to use a pointer to display the contents of the Array. I have the program written, but I'm not sure how to use a pointer to display the information. I'll post the actual assignment so I can better show what I mean. There are technically two, one using a pointer and another using pointer notation.

So, given by program I already have done, I just need to know how to use these pointers in the program to still have it work the way the assignment describes. I hope I've given enough information to get some help on this.

Information for Both Programs:
For all loop manipulating array, use the pointer (array address) as the starting point and the last element as the ending point,
For all loops manipulating arrays, do not subscripts to assign or compare array elements,
Set each array to a pointer and pass the pointer as a function parameter,
Please output all currency values using two decimal places.

Program 1:
Using the previous array program (Sort and Search), rewrite the
program using a pointer to display the original and sorted
contents of the array.
Ex: Use array++ where array is the name of the array

Program 2:
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

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>

#include<fstream>

#include<string>

#include<iomanip>

using namespace std;

void bubbleSort(string name[10], double scores[10], int year[10], int i)
{

	string tempS;

	int tempI;

	double tempD;

	for (int j = i - 1; j>0; j--)
	{
		for (int k = 0; k<j; k++)
		{
			if (year[k]>year[k + 1])
			{
				tempI = year[k];

				year[k] = year[k + 1];

				year[k + 1] = tempI;

				tempS = name[k];

				name[k] = name[k + 1];

				name[k + 1] = tempS;

				tempD = scores[k];

				scores[k] = scores[k + 1];

				scores[k + 1] = tempD;
			}
		}
	}
}
void display(string name[10], double scores[10], int year[10], int i)
{
	for (int j = 0; j<i; j++)

	{

		cout << left << setw(20) << name[j] << right << setw(10) << scores[j] << setw(10) << year[j] << endl;
	}
}
int linearSearch(string name[10], double scores[10], int year[10], int i, int y)
{
	for (int j = 0; j<i; j++)

		if (year[j] == y)

			return i;

	return 0;
}
int main()
{
	string name[10];

	double scores[10];

	int year[10];

	string fileName = "D:\\index.txt";


	ifstream in(fileName.c_str());

	if (!in)

	{
		cout << "Error: file could not be opened" << endl;

		return 0;
	}
	int i = 0;

	while (in)

	{
		getline(in, name[i], ' ');

		if (!in)

			break;

		in >> scores[i];

		if (!in)

			break;

		in >> year[i];

		in.ignore();

		i++;
	}
	in.close();

	cout << "Unsorted record :" << endl;

	display(name, scores, year, i);

	bubbleSort(name, scores, year, i);

	cout << "\nSorted record :" << endl;

	display(name, scores, year, i);

	int y;

	cout << "Enter the year to perform search: " << endl;

	cin >> y;

	i = linearSearch(name, scores, year, i, y);

	if (i)

		cout << "record found: " << endl;

	else

		cout << "record not found \n";

	return 0;
}
Last edited on
Where ever you are using the array syntax, change that to use a pointer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void display(string name[10], double scores[10], int year[10], int i)
    {
    double* pScore = NULL;

    int* pYear = NULL;

    for ( int j = 0; j < i; j++ )
        {
        pYear = ?;  /*  You figure this out    */

        pScore = ?;    /*    You figure this out    */

        cout << left << setw( 20 ) << name[ j ] << right << setw( 10 ) << 
            pScore << setw( 10 ) << pYear << endl;

        }    /*    for( j < i )    */
}

Ohhh okay. This makes sense. Let me give it a try and I'll let you know if I get it.
Ehhh... I'm still pretty confused here. On top not getting this, I'm also not understanding array++ as opposed to *(array + x)

Here's the code with what Koothkeeper gave me, which I may have not done right given that it ruins my output.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>

#include<fstream>

#include<string>

#include<iomanip>

using namespace std;

void bubbleSort(string name[10], double scores[10], int year[10], int i)
{

	string tempS;

	int tempI;

	double tempD;

	for (int j = i - 1; j>0; j--)
	{
		for (int k = 0; k<j; k++)
		{
			if (year[k]>year[k + 1])
			{
				tempI = year[k];

				year[k] = year[k + 1];

				year[k + 1] = tempI;

				tempS = name[k];

				name[k] = name[k + 1];

				name[k + 1] = tempS;

				tempD = scores[k];

				scores[k] = scores[k + 1];

				scores[k + 1] = tempD;
			}
		}
	}
}
void display(string name[10], double scores[10], int year[10], int i)
{
	double* pScore = NULL;

	int* pYear = NULL;

	for (int j = 0; j < i; j++)
	{
		pYear = year ;

		pScore = scores ;   

		cout << left << setw(20) << name[j] << right << setw(10) <<
			pScore << setw(10) << pYear << endl;

	}    
}
int linearSearch(string name[10], double scores[10], int year[10], int i, int y)
{

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

		if (year[j] == y)

			return i;

	return 0;
}
int main()
{
	string name[10];

	double scores[10];

	int year[10];

	string fileName = "D:\\index.txt";


	ifstream in(fileName.c_str());

	if (!in)

	{
		cout << "Error: file could not be opened" << endl;

		return 0;
	}
	int i = 0;

	while (in)

	{
		getline(in, name[i], ' ');

		if (!in)

			break;

		in >> scores[i];

		if (!in)

			break;

		in >> year[i];

		in.ignore();

		i++;
	}
	in.close();

	cout << "Unsorted record :" << endl;

	display(name, scores, year, i);

	bubbleSort(name, scores, year, i);

	cout << "\nSorted record :" << endl;

	display(name, scores, year, i);

	int y;

	cout << "Enter the year to perform search: " << endl;

	cin >> y;

	i = linearSearch(name, scores, year, i, y);

	if (i)

		cout << "record found: " << endl;

	else

		cout << "record not found \n";

	return 0;
}
What pointer syntax have they taught you?
Assuming I understand, we've been taught about & to get an address, *ptr to create a pointer variable, ptr = to store the address of a pointer, and (int *) to pass a pointer argument, in this case of an integer.

I'm really just struggling with the concept, and these are due tomorrow afternoon.

Try this much:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void display(string name[10], double scores[10], int year[10], int i)
    {
    double* pScore = scores;

    int* pYear = year;

    for ( int j = 0; j < i; j++ )
        {
        cout << left << setw( 20 ) << name[ j ] << right << setw( 10 ) << 
            pScore << setw( 10 ) << pYear << endl;

        pYear++;

        pScore++;

        }    /*    for( j < i )    */
}

It works, however the code shows the address and not the actual information.

For instance, instead of a year it displays 005CF8B8 in the first place it should be showing the year.

I also still don't understand the array++ apposed to *(array + x)
Perhaps you need to dereference with *pScore and *pYear?
Topic archived. No new replies allowed.