[try Beta version]
Not logged in

 
Issue with pointers

Feb 11, 2013 at 8:50pm
So this program is supposed to read an unknown number of values (max 30) and then attribute the grades entered into an array to track how many of each grade there are.

Im having issues with how I should pass these pointers. Rules are all parameters have to be pointers, but no matter how I try I cant get it to work.

I was going to replace the index in the calculate function with the values pointer. I just want it to be able to pass it properly before I change it.

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
#include <iostream>

using namespace std;

void inputArray(int, int);
void calculate(int, int, int);


int main() {
	int studentGrades[30];
	int gradeScale[101];
	int values = 0;
	
	inputArray(studentGrades, values);
	calculate(studentGrades, gradeScale, values);

	system ("pause");
	return 0;
}

void inputArray(int *studentGrades, int *values) {
	
	cout << "Enter test score for student " << *values + 1 << " (or -1 to quit): ";
	cin >> studentGrades[*values];

	while (studentGrades[*values] != -1) {
		values++;
		cout << "Enter test score for student " << *values + 1 << " (or -1 to quit): ";
		cin >> studentGrades[*values];
	}
}

void calculate(int *studentGrades, int *gradeScale, int *values) {
	for (int index = 0; index < 101; index++)
		gradeScale[index] = 0;

	for (int index = 0; index < *values; index++) { //index needs to be a pointer?
		int score = studentGrades[index]; //assign studentGrades[index] value to the corresponding vale in the gradeScale 1;
		gradeScale[score] += 1;

	}
}
Feb 11, 2013 at 10:54pm
Any help? Pointers are really frustrating me...
Feb 11, 2013 at 11:07pm
Your function prototypes all take int instead of int*.

Lines 14 and 15, you are trying to pass an int as an int*. Pass the address of values instead. inputArray(studentGrades, &values);

Line 27 increments values (the pointer) instead of incrementing the variable values points to.
Change it to ++(*values);

Last edited on Feb 11, 2013 at 11:08pm
Feb 11, 2013 at 11:20pm
Thank you so very much. the lack of the & before the name in the function call is what messed me up from the beginning.
Topic archived. No new replies allowed.