459,457 Members | 1,335 Online 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
73 Replies

 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

 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

 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

 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

 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

 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

 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

 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 median = (array + array)/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 + ar; 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 median = (array + array)/2;   Mar 31 '07 #19

 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=1; b=x=4 Mar 31 '07 #20

 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 + ar; 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 if (intsize%2==0) {    tmp1 = (intsize-1)/2; // gives first value    tmp2 = tmp1+1;    median = (ar[tmp1] + ar[tmp2]) / 2; }   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=1; b=x=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 if (intsize%2==0) {    tmp1 = (intsize-1)/2; // gives first value    tmp2 = tmp1+1;    median = (ar[tmp1] + ar[tmp2]) / 2; }   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 if (intsize%2==0) {    tmp1 = (intsize-1)/2; // gives first value    tmp2 = tmp1+1;    median = (ar[tmp1] + ar[tmp2]) / 2; }   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 if (intsize%2==0) {    tmp1 = (intsize-1)/2; // gives first value    tmp2 = tmp1+1;    median = (ar[tmp1] + ar[tmp2]) / 2; }   Mar 31 '07 #25

 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

 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 + ar; 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

 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

 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

 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

 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

 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

 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

 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

 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

 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 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; } Mar 31 '07 #50

73 Replies 