473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Logical error populating array in attempt to calculate median value

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
73 5336
sicarie
4,677 Expert Mod 4TB
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
1,764 Expert 1GB
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
brady
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
brady
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
brady
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
4,677 Expert Mod 4TB
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
4,677 Expert Mod 4TB
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
brady
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
4,677 Expert Mod 4TB
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
brady
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
4,677 Expert Mod 4TB
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
1,764 Expert 1GB
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
brady
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
4,677 Expert Mod 4TB
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
brady
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
4,677 Expert Mod 4TB
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
brady
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
brady
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
1,764 Expert 1GB
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
4,677 Expert Mod 4TB
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
brady
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
brady
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
brady
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
brady
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
4,677 Expert Mod 4TB
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
1,764 Expert 1GB
ok i tried the code below and it returned a 4 digit number???
What numbers have you used?
Mar 31 '07 #27
brady
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
brady
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
1,764 Expert 1GB
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
brady
59
Thats an idea! give me a minute.

Maybe it is because median is double and temps are int
Mar 31 '07 #31
brady
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
1,764 Expert 1GB
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
brady
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
1,764 Expert 1GB
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
4,677 Expert Mod 4TB
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
brady
59
npl is numperline for that particular function.

What is npl?
Mar 31 '07 #37
brady
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
4,677 Expert Mod 4TB
do i just cout the array?
Or those elements.
Mar 31 '07 #39
brady
59
ok let me try
Or those elements.
Mar 31 '07 #40
brady
59
ok i printed element 61 and its 0???

ok let me try
Mar 31 '07 #41
sicarie
4,677 Expert Mod 4TB
Then I don't think your sorting or populating methods are correct.
Mar 31 '07 #42
brady
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
1,764 Expert 1GB
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
brady
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
brady
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
4,677 Expert Mod 4TB
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
brady
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
4,677 Expert Mod 4TB
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
brady
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

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

Similar topics

2
by: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
4
by: ron | last post by:
Hi, I have class object that i serialize using the System.Xml.Serialization class. Intermittently the object is not getting serialized correctly, using System.Xml.Serialization classes....
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
0
by: Sebastien.LICHTHERTE | last post by:
Hello, I need to calculate the median and percentile of values in a group by query satisfying several criteria the user is asked to fill in when opening the query. A have a table called RX with...
7
by: Bhadan | last post by:
Hello, I have several jagged arrays which have been sorted. I'm trying to find the median of each array. Any tips appreciated. TIA. Bhads.
13
by: pereges | last post by:
I've an array : {100,20, -45 -345, -2 120, 64, 99, 20, 15, 0, 1, 25} I want to split it into two different arrays such that every number <= 50 goes into left array and every number 50 goes...
1
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
13
by: JOYCE | last post by:
Please help me,I don't know where the error is.The following is my programming. #include<stdio.h> #define SIZE 99 void mean (const int answer); void median(int answer); void mode(int...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.