By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,457 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,457 IT Pros & Developers. It's quick & easy.

Logical error populating array in attempt to calculate median value

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
Mar 31 '07 #1
Share this Question
Share on Google+
73 Replies


sicarie
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?
Mar 31 '07 #2

Savage
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
Mar 31 '07 #3

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
Mar 31 '07 #4

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
Mar 31 '07 #5

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?
Mar 31 '07 #6

sicarie
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 pre-written 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
Mar 31 '07 #7

sicarie
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.
Mar 31 '07 #8

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
Mar 31 '07 #9

sicarie
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.
Mar 31 '07 #10

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.
Mar 31 '07 #11

sicarie
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?
Mar 31 '07 #12

Savage
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.
Mar 31 '07 #13

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.
Mar 31 '07 #14

sicarie
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.
Mar 31 '07 #15

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?
Mar 31 '07 #16

sicarie
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?
Expand|Select|Wrap|Line Numbers
  1. median = (array[61] + array[62])/2;
  2.  
Mar 31 '07 #17

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.
Mar 31 '07 #18

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?
Expand|Select|Wrap|Line Numbers
  1. median = (array[61] + array[62])/2;
  2.  
Mar 31 '07 #19

Savage
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=(numElements-1)\2;
first number a=x[i-1];
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
Mar 31 '07 #20

sicarie
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:
Expand|Select|Wrap|Line Numbers
  1. if (intsize%2==0) {
  2.    tmp1 = (intsize-1)/2; // gives first value
  3.    tmp2 = tmp1+1;
  4.    median = (ar[tmp1] + ar[tmp2]) / 2;
  5. }
  6.  
Mar 31 '07 #21

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=(numElements-1)\2;
first number a=x[i-1];
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
Mar 31 '07 #22

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:
Expand|Select|Wrap|Line Numbers
  1. if (intsize%2==0) {
  2.    tmp1 = (intsize-1)/2; // gives first value
  3.    tmp2 = tmp1+1;
  4.    median = (ar[tmp1] + ar[tmp2]) / 2;
  5. }
  6.  
Mar 31 '07 #23

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:
Expand|Select|Wrap|Line Numbers
  1. if (intsize%2==0) {
  2.    tmp1 = (intsize-1)/2; // gives first value
  3.    tmp2 = tmp1+1;
  4.    median = (ar[tmp1] + ar[tmp2]) / 2;
  5. }
  6.  
Mar 31 '07 #24

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:
Expand|Select|Wrap|Line Numbers
  1. if (intsize%2==0) {
  2.    tmp1 = (intsize-1)/2; // gives first value
  3.    tmp2 = tmp1+1;
  4.    median = (ar[tmp1] + ar[tmp2]) / 2;
  5. }
  6.  
Mar 31 '07 #25

sicarie
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.
Mar 31 '07 #26

Savage
Expert 100+
P: 1,764
ok i tried the code below and it returned a 4 digit number???
What numbers have you used?
Mar 31 '07 #27

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 = (numElements-1)/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???
Mar 31 '07 #28

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.
Mar 31 '07 #29

Savage
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 = (numElements-1)/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
Mar 31 '07 #30

P: 59
Thats an idea! give me a minute.

Maybe it is because median is double and temps are int
Mar 31 '07 #31

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.
Mar 31 '07 #32

Savage
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?
Mar 31 '07 #33

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?
Mar 31 '07 #34

Savage
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?
Mar 31 '07 #35

sicarie
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.
Mar 31 '07 #36

P: 59
npl is numperline for that particular function.

What is npl?
Mar 31 '07 #37

P: 59
do i just cout the array?

Try printing out those values in the array, see what is in them.
Mar 31 '07 #38

sicarie
Expert Mod 2.5K+
P: 4,677
do i just cout the array?
Or those elements.
Mar 31 '07 #39

P: 59
ok let me try
Or those elements.
Mar 31 '07 #40

P: 59
ok i printed element 61 and its 0???

ok let me try
Mar 31 '07 #41

sicarie
Expert Mod 2.5K+
P: 4,677
Then I don't think your sorting or populating methods are correct.
Mar 31 '07 #42

P: 59
do i need to build the array before the caller to the media function??

ok i printed element 61 and its 0???
Mar 31 '07 #43

Savage
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?
Mar 31 '07 #44

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??
Mar 31 '07 #45

P: 59
could point, hold on let me see so i can explain. now im confused by my own code. ha-ha!

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?
Mar 31 '07 #46

sicarie
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.
Mar 31 '07 #47

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.
Mar 31 '07 #48

sicarie
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.
Mar 31 '07 #49

P: 59
here:
Expand|Select|Wrap|Line Numbers
  1. buildArray(storescores)
  2.  
  3. int buildArray(int ar[])
  4. {
  5. int temp, i = 0;
  6. ifstream inFile;
  7.  
  8.  
  9. inFile.open("a8.txt");
  10.  
  11.     if(inFile.fail())
  12.     {
  13.     cout << "Error opening input file";
  14.     exit(1);
  15.     }
  16.  
  17. inFile >> temp;
  18.  
  19. while(!inFile.eof())
  20. {
  21. ar[i] = temp;        
  22. i++;            
  23. inFile >> temp;        
  24. }
  25.  
  26.  
  27. inFile.close();      
  28.  
  29. return i;
  30. }
Mar 31 '07 #50

73 Replies

Post your reply

Sign in to post your reply or Sign up for a free account.