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:
int main()
{
int input = 0;
cout << "How many students do you want to calculate?"<< endl;
cin>> input;
double *iptr;
iptr = newdouble[input];
if (iptr == NULL)
{
cout << "Error allocating memory" << endl;
exit(1);
}
double *mptr;
mptr = newdouble[input];
if (mptr == NULL)
{
cout << "Error allocating memory" << endl;
exit(1);
}
double *cptr;
cptr = newdouble[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.
'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?
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.
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.