|
 |
|
 |
09-22-2004, 03:21 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
|
Median
What i'm tryin to do is find the median of an array of values. I'm using pointers and functions ... If I use an odd number of values in the array then I find my median just fine because its the middle number, however, when I go to find the median with an even number of values in the array my numbers are off? Any idea why this is ...
CODE:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
void swap2(float&m, float&n);
float median (float [], int);
int main ()
{
float array[6]={1,3,5,7,9,11};
int size=6;
float middle;
middle = median(array, size);
cout<<"Your median is "<<middle<<endl;
return 0;
}
float median (float list[], int size)
{
int count;
float *ptr;
float temp;
float answer;
bool swap;
int x,y;
float n=2;
ptr=list;
do
{
swap = false;
for (count=0; count<(size-1); count++)
{
if (ptr[count]>ptr[count+1])
{
temp = ptr[count];
ptr[count] = ptr[count+1];
ptr[count+1]=temp;
swap=true;
}
}
}while (swap);
if (size%2==0)
{
x=size/2;
y=x++;
cout<<x<<endl;
cout<<y<<endl;
cout<<n<<endl;
answer=(ptr[x]+ptr[y])/n;
}
if (size%2==1)
{
x=size/2;
answer=ptr[x];
}
return answer;
}
void swap2(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
|
|
|
09-22-2004, 03:50 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
This is math. Not coding.
The median of even amounts is: take the middle two then calculate their average.
Code:
#include <iostream>
#include <algorithm>
double Median (double*, int);
int main ()
{
//Note that in standard C++ "double" is the default.
double theArray[]={11,5,3,7,15,1,10,15};
int arraySize = sizeof(theArray)/sizeof(*theArray);
std::cout<<"Sorted array:\n";
for(int i=0; i<arraySize; ++i)
std::cout<<theArray[i]<<" ";
std::cout<<std::endl;
std::cout<<"The median = "<<Median(theArray, arraySize)<<std::endl;
std::cin.get();
return 0;
}
double Median(double* itsArray, int size)
{
std::sort( itsArray, itsArray+size );
if( size%2 )
return itsArray[size/2];
else
return ( itsArray[size/2-1] + itsArray[size/2] ) / 2;
}
__________________
Last edited by Valmont; 09-22-2004 at 05:49 PM.
|
|
|
09-22-2004, 06:08 PM
|
#3 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I didn't look at your program but now I did. I thought that you didn't know how to calculate a median. Instead you made a crucial coding mistake :)
Find the differences:
Code:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
void swap2(float&m, float&n);
float median (float [], int);
int main ()
{
float array[]={3,1,5,7,9,11};
int size=sizeof(array)/sizeof(*array);
float middle;
middle = median(array, size);
cout<<"Your median is "<<middle<<endl;
cin.get();
return 0;
}
float median (float list[], int size)
{
int count;
float temp;
float answer;
bool swap;
int x,y;
float n=2;
do
{
swap = false;
for (count=0; count<(size-1); count++)
{
if (list[count]>list[count+1])
{
temp = list[count];
list[count] = list[count+1];
list[count+1]=temp;
swap=true;
}
}
}while (swap);
for(int i=0; i<size; ++i)
cout<<list[i]<<" ";
cout<<endl;
if (size%2==0)
{
x=size/2-1;
y=x+1;
answer=(list[x]+list[y])/n;
}
if (size%2==1)
{
x=(size/2);
answer=list[x];
}
return answer;
}
void swap2(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
You have to understand that array indexing starts with '0', not '1'.
__________________
|
|
|
09-22-2004, 06:57 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
|
Was my error this part of my code
Code:
for (count=0; count<(size-1); count++)
|
|
|
09-22-2004, 08:48 PM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
No this:
Code:
x=size/2;
y=x++;
cout<<x<<endl;
cout<<y<<endl;
cout<<n<<endl;
answer=(ptr[x]+ptr[y])/n;
You didn't account for the fact that arrays start at index 0.
Compare it with mine.
__________________
Last edited by Valmont; 09-26-2004 at 06:39 AM.
|
|
|
09-23-2004, 05:32 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
|
I probably could have looked at my code for days and never found that, thanks Val your a lifesaver!!:rock:
|
|
|
09-23-2004, 10:12 AM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
In that case (in any case these days by the way), you'll should be motivated to at least *look at* STL.
STL stands for Standard Template Library. At least three reasons might motivate you to use it:
`Easy container handling.
`Lots of algorithms/utilities at your disposal.
`It is the "modern" way of C++ programming.
Easy Container handling
Containers in the STL are easy to use because they handle memory management for you already. That is the biggest advantage. And after a few excercises you'll be able to use then by intuition as well: all of the containers offer basically the same interfaces and methods to modify and access the contents of the containers. Learn a few, and you'll know them all. Also note that, although STL containers are filled with quite some hard-coding, they are efficient and with proper use (choices) they are just as efficient as scratching the metal with user defined array handling code.
Many companies don't want to use STL because it's "cool" to invent own code by using only the basic tools. They like to sell it as "high tech company code". They missed the point. The tools are there. Use them.
Lots of algorithms/utilities at your disposal
If you look at my first code, you will notice I used std::sort(). This sorting algorithm is extremely fast for containers containing unsorted data. It comes standard with C++. All I needed to do is to add the <algorithm> header. There are tons of basic but important algorithms at your disposal. You may ask yourself at one point: "why bother re-inventing the wheel? It's right there already!".
It is the "modern" way of C++ programming.
Is it? Well, depends on who you're asking to. Microsoft DLL coders might not be motivated at first sight. And many others sadly just don't want to learn new skills, techniques or insights. However. Coding is not only learning a language. It is also learning to put the things in its right perspective. Later when you advance in CS, you will learn design. And later even analisys. A style or even a vision might start to arise: a good coder is lazy. So lazy, that he wants to invest some time in new skills to stay lazy.
Programming is communicating.
Look at my first code once again. I am still using an array as container. However, the customer who is buying your software isn't interested in which containers you've used. As long as the application works properly and is delivered on time, he or she is happy.
Currently we are interested in double precision numbers. Programmers call this: problem domain.
So to save myself from using error-prone arrays, I'll might as well use std::vector. This container is defined in <vector> .
Your job:
Replace my "crappy" array with a vector.
Learn the very basics of containers already. Later, you'll get things done much faster with less errors. I'll be right here if you need me.
- Val -
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
median of a sequel
|
Andi55 |
Standard C, C++ |
1 |
10-27-2004 08:28 AM |
|
Class Array
|
kokkines |
Standard C, C++ |
5 |
05-11-2004 02:29 AM |
All times are GMT -8. The time now is 08:22 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|