## PDB1982 Posting Whiz in Training

Can anyone tell me why I am getting this message:

"...error C2296: '%': illegal, left operand has type 'double' "

here is my code:

``````double Q1median(ifstream& students, string studentid[60], string fname[60], string lname[60],
int Q1[60], int Q2[60], int Q3[60], int MidTerm[60], int Q4[60], int Q5[60],
int Q6[60], int Final[60],int i)
{
double sum1,Q1median;
int cnt;

sum1 = 0.0;
cnt = 0;

for (i=0; i<6; i++)
{
Q1[i]; sum1+=Q1[i];
cnt++;
if ((sum1 % 2) == 0)
{
Q1median = sum1/cnt;
}
else
{
Q1median = ((sum1 -1)/cnt) - 1;
}
}
return Q1median;
}``````

## Clinton Portis 211

I found this article at msdn:

The multiplicative operators take operands of arithmetic types. The modulus operator (%) has a stricter requirement in that its operands must be of integral type. (To get the remainder of a floating-point division, use the run-time function, fmod.) The conversions covered in Arithmetic Conversions are applied to the operands, and the result is of the converted type.

According to the article, you might have better luck using the fmod() function from <cmath>.

## jonsca 1,059

That's also not the correct formula for the median I don't believe. AFAIK, if you have a set of numbers like 1 3 5 9 12, then 5 would be the median as it's the central value by count, 3rd from the beginning and 3rd from the last. When you have a list (not a sum) with an even number of members, like 1 2 9 12, the median is 5.5, the average of the two middle numbers.

## PDB1982

That's also not the correct formula for the median I don't believe. AFAIK, if you have a set of numbers like 1 3 5 9 12, then 5 would be the median as it's the central value by count, 3rd from the beginning and 3rd from the last. When you have a list (not a sum) with an even number of members, like 1 2 9 12, the median is 5.5, the average of the two middle numbers.

You're right, it seems like that would calculate the median for an odd number, so how would I do it for an odd number...hmmmm...

obviously, mathematically it's the middle two numbers, added, then divided by two...

ex. 1,2,3,4,56

(3+4) / 2 = (7)/2 = 3.5

I'm just not sure how to translate that to C++ when I have the variables in parallel array of Q1, Q2, etc....each containing around 60 total test scores.....any ideas?

## jonsca 1,059

This is one department in which your design will shine. I'm assuming you want the median for Q1 separate from Q2. Simply bubble sort your arrays (do a quick net search one how to do it), since you have 60 elements you know you'll have an even number, grab the element at index (60/2) -1 plus the element at 60/2 index divided by two.
I regret that I didn't push you more towards the 2D array when we were going through the other portions -- I didn't realize that you would have to do all of these manipulations too, but water under the bridge, you've stuck with it and hopefully you'll be happy with it in the end.

## PDB1982

This is one department in which your design will shine. I'm assuming you want the median for Q1 separate from Q2. Simply bubble sort your arrays (do a quick net search one how to do it), since you have 60 elements you know you'll have an even number, grab the element at index (60/2) -1 plus the element at 60/2 index divided by two.
I regret that I didn't push you more towards the 2D array when we were going through the other portions -- I didn't realize that you would have to do all of these manipulations too, but water under the bridge, you've stuck with it and hopefully you'll be happy with it in the end.

I'll never be happy with it! :P I know, I could've made it a tone easier on myself going with a 2D array, or even a structure, which I never even though about. But, it's due Monday, so I have to keep on keepin' on with this style. I've never done a bubble sort, so I'll have to look it up....thanks

Ok....looked up the Bubble Sort, but I'm not sure how to use it....how do I call my Q1 array in this function?

``````void BubbleSort( Q1[i] fits here......but how?)
{
int i, j, flag = 1;    // set flag to 1 to start first pass
int temp;             // holding variable
int numLength = num.length( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1] > num[j])      // ascending order simply changes to <
{
temp = num[j];             // swap elements
num[j] = num[j+1];
num[j+1] = temp;
flag = 1;               // indicates that a swap occurred.
}
}
}
return;   //arrays are passed to functions by address; nothing is returned
}``````

Would it be constructed like this?

``````void BubbleSort(int Q1[60])
{
int i, j, flag = 1;    // set flag to 1 to start first pass
int temp;             // holding variable
for(i = 1; (i <= 60) && flag; i++)
{
flag = 0;
for (j=0; j < (Q1[60] -1); j++)
{
if (Q1[j+1] > Q1[j])      // ascending order simply changes to <
{
temp = Q1[j];             // swap elements
Q1[j] = Q1[j+1];
Q1[j+1] = temp;
flag = 1;               // indicates that a swap occurred.
}
}
}
return;   //arrays are passed to functions by address; nothing is returned
}``````

## DavidRead

It's probably line #15, the % operator needs an integer on the left.
Try declaring sum1 as an integer.