Pointer Arithmetic

My Problem: I am trying to created a program that will give me the average, median and mode of new dynamically allocated array. So far I have gotten the average and median to work. However the mode is giving me a bit of a challenge.
Here is a snippet of the code:

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
int main()
{
	
	int input = 0;
	cout << "How many students do you want to calculate?"<< endl;
	cin>> input;
	double *iptr;
	iptr = new double[input];
	if (iptr == NULL)
	{
		cout << "Error allocating memory" << endl;
		exit(1);
	}
	double *mptr;
	mptr = new double[input];
	if (mptr == NULL)
	{
		cout << "Error allocating memory" << endl;
		exit(1);
	}
	double *cptr;
	cptr = new double[input];
	if (cptr == NULL)
	{
		cout << "Error allocating memory" << endl;
		exit(1);
	}
	
	for (int c=0; c<input; c++)
		
	{
		cout << "Input the number of moveis for student " << c+1 << " :" << endl;
		cin>> iptr[c];
		double n = mode_finder(iptr, iptr[c], input); 
		if (n == -1)
			{
			mptr[c] = iptr[c];
			cptr[c] = 1;
			
		}
		else
		{
			1+= *(cptr + n); // Error!!!

		}
	}

	array_sort(iptr, input);
	double median = median_finder(iptr, input);

	cout << "The median of the number of movies watched: "<< median << endl;

	double average = average_finder(iptr, input);

	cout << "The average of movies watched: " << average << endl;
	

}


Here is the error I get:error C2111: '+' : pointer addition requires integral operand

I hope I provided enough code to see the problem. When i perform a linear search on the array for the number passed in, if pos == -1,which means it did not find it, I want it to be added into the cptr. If it does find the passed in number, It passes back the index and I want to add one to the index of where it found it. This way I will have two parralel arrays, one with the sorted integers, and one with the ammount each time the integer was found so I can output the first number in mptr, which after being sorted parralel with the cptr, mptr[0] will be my mode.

If I dont make sense, tell me! Thanks

EDITS: To fix copy + paste error and for code tags.
Last edited on
[code]put your code in code tags![/code]

'cptr' is a bad pointer because you're never setting it to point to anything.
Same with 'mptr'

I'm assuming you copy/pasted this code several times and forgot to change all of the 'iptr's.

1
2
3
4
5
6
7
8
9
double *cptr;  // uninitialized pointer -- points to garbage

cptr[c] = 1;  //  because cptr points to garbage, this would cause memory corruption
//  and/or an access violation (causing a program crash).  All of these are very bad things

1+= *(cptr + n);  // '1' is a constant value.  It can't be on the left side of an assignment operator.
// 'n' is also a double, which you can't add to a pointer (only integers can
//  be added to pointers)
//  what exactly are you trying to do here? 
Last edited on
Ok, thanks so much for catchign the copy and paste error and I put the code in the tags for you. Never knew how to do that... But I still have the same error.
errr... the error you're getting is because 'n' is a double... as I mentioned in my previous post =P. But there are other problems with that line as well.


*points to last 4 lines in his previous post*
Wow, somehow I didnt even see those... I thought it was an example of code. Ok, so I changed cptr and n to an int. It builds and I will have to play with it to see if I am still getting the desired output. Your post was very helpful and I really appreciate it.
Topic archived. No new replies allowed.