P: 59

I have a function called int calcMedian(int ar[], int numElements) How do i code the function to calculate the median in the array? Please help
 
Share this Question
Expert Mod 2.5K+
P: 4,677

brady
Here are our Posting Guidelines, which I'm sure you're aware of.
What have you tried on this?
  Expert 100+
P: 1,764

I have a function called int calcMedian(int ar[], int numElements) How do i code the function to calculate the median in the array? Please help
Sort,your array in descanding or ascending order and element in middle is a median
Savage
 
P: 59

i created a seperate array for that
Sort,your array in descanding or ascending order and element in middle is a median
Savage
 
P: 59

and its even so somehow i need code to look at the two middle elements and add them and divide or something???
i created a seperate array for that
 
P: 59

what do you mean?
brady
Here are our Posting Guidelines, which I'm sure you're aware of.
What have you tried on this?
  Expert Mod 2.5K+
P: 4,677

Posting Homework or Coursework Questions and Answers
This site is not a place where you can get your homework and course work done for you. Ignoring the questionable morals of getting someone else to do your work towards a formal qualification you will learn a lot more by attempting the problem yourself, then asking for help with the bits that are not working. You will be more likely to get help if you appear to have made an attempt at the problem yourself.
* If it appears that the question has just been pasted directly from a text book or coursework assignment our moderators have been instructed to meet this with a set response. This is a prewritten message asking you to make some attempt at the assignment yourself before asking questions about specific problems and referring you to this FAQ.
* Do NOT post your complete source code. Remember, you found this site, so can your professors and tutors and they are likely to take a dim view if you hand in something that appears to be copied from this site. In most academic institutes the minimum response to copying of assignments is a 0 mark for the assignment in question. If you post your entire code your professor will not know it was your work, you may be penalised for copying from yourself.
* If an expert requests that you provide more of your code then you can PM it to them, however please do not PM them your code unless requested.
* Acceptable questions that students can ask are on functionality and theory or syntax corrections in small posted code snippets.
* Please make sure you have asked an actual question worded in English outside of the text of your assignment that you have posted.
It means we ask you to
1) read the posting guidelines
2) post a specific question
3) have tried to answer it yourself, and not just get us to do the work for you
  Expert Mod 2.5K+
P: 4,677

and its even so somehow i need code to look at the two middle elements and add them and divide or something???
That depends if your arraysize is odd or even. If it's odd, you can just look at the middle element, and if it's even, you can (though you have to check to make sure of the implementation with whoever assigned you the assignment) average the two middle values.
This does depend on a sorted array.
 
P: 59

I am completely aware of the posting guidelines, this is not a school assignment; just practice, and i have been working on this function for a good 2 hours now so yes i have tried it for myself. I dont want someone to give me the code, maybe some hints!
It means we ask you to
1) read the posting guidelines
2) post a specific question
3) have tried to answer it yourself, and not just get us to do the work for you
  Expert Mod 2.5K+
P: 4,677

I am completely aware of the posting guidelines, this is not a school assignment; just practice, and i have been working on this function for a good 2 hours now so yes i have tried it for myself. I dont want someone to give me the code, maybe some hints!
And we ask you to show that by asking the specific question you're having trouble with. What have you been working on for two hours?
"I can't get this sorting algorithm to work properly  it messes up the last element, can someone put a fresh pair of eyes on it?" is a specific question telling 1) you worked on it and 2) what is wrong.
"I can't figure out how to find the mean" and "please help me code the mean" are not specific questions.
 
P: 59

i have already called a function to sort the array and that works fine, i have a function that also calculates the average.. i wanted to calculate the median because i knew it would be challenging. just a bit confused...im a newbie with the C++. but its fun
That depends if your arraysize is odd or even. If it's odd, you can just look at the middle element, and if it's even, you can (though you have to check to make sure of the implementation with whoever assigned you the assignment) average the two middle values.
This does depend on a sorted array.
  Expert Mod 2.5K+
P: 4,677

i have already called a function to sort the array and that works fine, i have a function that also calculates the average.. i wanted to calculate the median because i knew it would be challenging. just a bit confused...im a newbie with the C++. but its fun
Ok, so you have it sorted, what part of the median calculation are you having trouble with?
  Expert 100+
P: 1,764

and its even so somehow i need code to look at the two middle elements and add them and divide or something???
That's only necsasary if numElements is not a odd number.If it is a odd number
element in middle is median.
For e.g
Array:12 8 5 23 17
sorted array: 5 8 12 17 23
avg: (5+8+12+17+23)/5=13,because there is no 13 in array median is 12.
 
P: 59

Right that i understand, if it is odd i can just take the middle element of the array, but i know that 124 ints are in the array. so i need to take the 2 middle values and add them and then divide by two. i just cant figure out how to code it correctly.
That's only necsasary if numElements is not a odd number.If it is a odd number
element in middle is median.
For e.g
Array:12 8 5 23 17
sorted array: 5 8 12 17 23
avg: (5+8+12+17+23)/5=13,because there is no 13 in array median is 12.
  Expert Mod 2.5K+
P: 4,677

Right that i understand, if it is odd i can just take the middle element of the array, but i know that 124 ints are in the array. so i need to take the 2 middle values and add them and then divide by two. i just cant figure out how to code it correctly.
124 will always be in the array  it is fixed size?
Then you could hardcode the values in there  elements 61 and 62.
 
P: 59

I just cant figure out how to pull the 2 middle elements of the array. if i can somehow tell the function to do this, adding and gthen dividing by 2 would be cake.
Ok, so you have it sorted, what part of the median calculation are you having trouble with?
  Expert Mod 2.5K+
P: 4,677

I just cant figure out how to pull the 2 middle elements of the array. if i can somehow tell the function to do this, adding and gthen dividing by 2 would be cake.
You tried this? 
median = (array[61] + array[62])/2;

 
P: 59

yea its fixed cause i created an array to read values in a txt file i created and then store them in the array. there are 124 ints always passed to each function. So i could do something like this:
sum = ar[61] + ar[62];
sum / 2;
cout << .......
124 will always be in the array  it is fixed size?
Then you could hardcode the values in there  elements 61 and 62.
 
P: 59

No not yet, because i wanted to use decision statements in the case that it wasnt fixed and could be even or odd. but yea let me try that. now if i call this function, should i call it after the showarray call to the sort function that i have?
You tried this? 
median = (array[61] + array[62])/2;

  Expert 100+
P: 1,764

I just cant figure out how to pull the 2 middle elements of the array. if i can somehow tell the function to do this, adding and gthen dividing by 2 would be cake.
Here:
i=(numElements1)\2;
first number a=x[i1];
secound number b=x[i+1]
e.g:
numElements =3
x=1 2 4
i=2/2=1;;
a=x[0]=1;
b=x[2]=4
  Expert Mod 2.5K+
P: 4,677

yea its fixed cause i created an array to read values in a txt file i created and then store them in the array. there are 124 ints always passed to each function. So i could do something like this:
sum = ar[61] + ar[62];
sum / 2;
cout << .......
You pass it intsize too, so if you wanted something a little more elegant, I'd try something like: 
if (intsize%2==0) {

tmp1 = (intsize1)/2; // gives first value

tmp2 = tmp1+1;

median = (ar[tmp1] + ar[tmp2]) / 2;

}

 
P: 59

ok let me try to see if this works. i just tried what i posted to moderator but i got a crazy value returnes: 2.1345e49595 or something
Here:
i=(numElements1)\2;
first number a=x[i1];
secound number b=x[i+1]
e.g:
numElements =3
x=1 2 4
i=2/2=1;;
a=x[0]=1;
b=x[2]=4
 
P: 59

i'll try this as well..one minute.
You pass it intsize too, so if you wanted something a little more elegant, I'd try something like: 
if (intsize%2==0) {

tmp1 = (intsize1)/2; // gives first value

tmp2 = tmp1+1;

median = (ar[tmp1] + ar[tmp2]) / 2;

}

 
P: 59

is intsize the number of elements in array?
You pass it intsize too, so if you wanted something a little more elegant, I'd try something like: 
if (intsize%2==0) {

tmp1 = (intsize1)/2; // gives first value

tmp2 = tmp1+1;

median = (ar[tmp1] + ar[tmp2]) / 2;

}

 
P: 59

ok i tried the code below and it returned a 4 digit number???
You pass it intsize too, so if you wanted something a little more elegant, I'd try something like: 
if (intsize%2==0) {

tmp1 = (intsize1)/2; // gives first value

tmp2 = tmp1+1;

median = (ar[tmp1] + ar[tmp2]) / 2;

}

  Expert Mod 2.5K+
P: 4,677

ok i tried the code below and it returned a 4 digit number???
Yeah, that was just rough coding  you'll have to tailor it and fix it up to fit your code.
  Expert 100+
P: 1,764

ok i tried the code below and it returned a 4 digit number???
What numbers have you used?
 
P: 59

The following code gave me, "The median of the set of scores is: 6810
int calcMedian(int ar[], int numElements)
{
double median;
double sum = 0;
int temp1, temp2;
if (numElements%2==0) //numElements was intsize
{
temp1 = (numElements1)/2; // gives first value
temp2 = temp1+1;
median = (ar[temp1] + ar[temp2]) / 2;
}
cout << "\nThe median of the stored scores is: " << median;
return median;
ok i tried the code below and it returned a 4 digit number???
 
P: 59

like pseudocode or something? ok i will play around with it for a bit. but i even tried just
median == ar[61] + ar[62]; and it gave me a four digit code. im not very experiences with C++ as you can prob tell.
Yeah, that was just rough coding  you'll have to tailor it and fix it up to fit your code.
  Expert 100+
P: 1,764

The following code gave me, "The median of the set of scores is: 6810
int calcMedian(int ar[], int numElements)
{
double median;
double sum = 0;
int temp1, temp2;
if (numElements%2==0) //numElements was intsize
{
temp1 = (numElements1)/2; // gives first value
temp2 = temp1+1;
median = (ar[temp1] + ar[temp2]) / 2;
}
cout << "\nThe median of the stored scores is: " << median;
return median;
Maybe it is because median is double and temps are int
 
P: 59

Thats an idea! give me a minute.
Maybe it is because median is double and temps are int
 
P: 59

No, still 6810!?!? i'm not sure why its returning that value. it shouldn't be
Thats an idea! give me a minute.
  Expert 100+
P: 1,764

No, still 6810!?!? i'm not sure why its returning that value. it shouldn't be
Are you sure that yours sort funcion is working corectly?
 
P: 59

Yes unless i have the caller in the wrong order? i have them as listed:
temp = buildArray(storescores); //passes array to function buildArray.
sortArray(storescores, temp); // calls sorting array
calcAverage(storescores, npl); // calls function to calculate average of scores in array
calcMedian(storescores, temp);
showArray(storescores, temp, npl);
Are you sure that yours sort funcion is working corectly?
  Expert 100+
P: 1,764

Yes unless i have the caller in the wrong order? i have them as listed:
temp = buildArray(storescores); //passes array to function buildArray.
sortArray(storescores, temp); // calls sorting array
calcAverage(storescores, npl); // calls function to calculate average of scores in array
calcMedian(storescores, temp);
showArray(storescores, temp, npl);
What is npl?
  Expert Mod 2.5K+
P: 4,677

Yes unless i have the caller in the wrong order? i have them as listed:
temp = buildArray(storescores); //passes array to function buildArray.
sortArray(storescores, temp); // calls sorting array
calcAverage(storescores, npl); // calls function to calculate average of scores in array
calcMedian(storescores, temp);
showArray(storescores, temp, npl);
Try printing out those values in the array, see what is in them.
 
P: 59

npl is numperline for that particular function.
What is npl?
 
P: 59

do i just cout the array?
Try printing out those values in the array, see what is in them.
  Expert Mod 2.5K+
P: 4,677

do i just cout the array?
Or those elements.
 
P: 59

ok let me try
Or those elements.
 
P: 59

ok i printed element 61 and its 0???
ok let me try
  Expert Mod 2.5K+
P: 4,677

Then I don't think your sorting or populating methods are correct.
 
P: 59

do i need to build the array before the caller to the media function??
ok i printed element 61 and its 0???
  Expert 100+
P: 1,764

Yes unless i have the caller in the wrong order? i have them as listed:
temp = buildArray(storescores); //passes array to function buildArray.
sortArray(storescores, temp); // calls sorting array
calcAverage(storescores, npl); // calls function to calculate average of scores in array
calcMedian(storescores, temp);
showArray(storescores, temp, npl);
So,u bulid a array of storescores which are also a array.But why your sort
function has 2 arguments is not 1 enough?Why is npl in call of average,why is there 2 arguments at median?
 
P: 59

i know im not supposed to do this but can i please copy and paste my whole program? maybe you will catch something i am not.
do i need to build the array before the caller to the media function??
 
P: 59

could point, hold on let me see so i can explain. now im confused by my own code. haha!
So,u bulid a array of storescores which are also a array.But why your sort
function has 2 arguments is not 1 enough?Why is npl in call of average,why is there 2 arguments at median?
  Expert Mod 2.5K+
P: 4,677

do i need to build the array before the caller to the media function??
Ummmm.... You are trying to find the median of an array you haven't put values into?
The C++ compiler is very interesting when it assigns memory space  when it assigns it, the variable takes on the value that was previously stored in memory. This is a "junk" variable, and why you should always initialize variables either upon instantiation, or immediately after.
 
P: 59

i built the array from a txt file. i thought that after you build it, it always holds what goes in there? maybe i need to build it again before the median call?
Ummmm.... You are trying to find the median of an array you haven't put values into?
The C++ compiler is very interesting when it assigns memory space  when it assigns it, the variable takes on the value that was previously stored in memory. This is a "junk" variable, and why you should always initialize variables either upon instantiation, or immediately after.
  Expert Mod 2.5K+
P: 4,677

Let's do a manual debug and cout after every step  this makes sure we're putting everything in correctly and you have a logic error.
cout the array after you put it in and check it, cout the array after it's sorted, make sure it's sorted.
 
P: 59

here: 
buildArray(storescores)


int buildArray(int ar[])

{

int temp, i = 0;

ifstream inFile;



inFile.open("a8.txt");


if(inFile.fail())

{

cout << "Error opening input file";

exit(1);

}


inFile >> temp;


while(!inFile.eof())

{

ar[i] = temp;

i++;

inFile >> temp;

}



inFile.close();


return i;

}
    Question stats  viewed: 3615
 replies: 73
 date asked: Mar 31 '07
