Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,562 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,358 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



what;s wrong with the code?

 
Reply to this topicStart new topic

what;s wrong with the code?, media

azshteeg
post 7 Aug, 2008 - 02:30 AM
Post #1


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 2

I was able to compile the code. but when i try to run it. it prints the arranged number in the array then terminates. it does not print the median.

what do you think might have caused this? i dunno whats the problem.


#include <stdio.h>
#include <stdlib.h>

int median(const int [], const int);

int main(){

int array[50];
int i, b, t;
int num;

printf("This program gets the median of an array.\n");
printf("Press ^Z [CTRL + Z] after the last inputed number\n\n");

printf("How many numbers do you want to input? ");
scanf("%d", &num);

for (i=0; i<50; i++){
scanf("%d", &array[i]);
}

-----from here: i should've put this in the function definition part but there are errors(in red).. but when i placed it here, there are no errors but then i does not work properly-----

for(i=1; i<num; i++)

for(b=num-1; b>=i; --cool.gif{ //arrange numbers in array based on value

if(array[b-1] > array[b]){

t=array[b-1];
array[b-1] = array[b];

array[b]=t;
}
}

for(t=0; t<num; t++){ //print numbers in order

printf("%d\n", array[t]);

}

-----end of the part that i wanted to put in funct def----

printf("The median is %d", median(array,50));

return 0;

}

int median (const int array[], const int size){ //function definition
int median = array[50];

int num;


median = num/2;

if(num%2 ==1){ //for odd num(size of array)
median = array[median];
}

else { //for even num
median = (array[median]+array[median-1])/2;
}

return num;

}

help me guys!

This post has been edited by azshteeg: 7 Aug, 2008 - 02:31 AM
User is offlineProfile CardPM

Go to the top of the page


jwwicks
post 7 Aug, 2008 - 06:34 AM
Post #2


D.I.C Head

Group Icon
Joined: 31 Jul, 2008
Posts: 55



Thanked 5 times

Dream Kudos: 200
My Contributions


Hello,

QUOTE(azshteeg @ 7 Aug, 2008 - 02:30 AM) *

I was able to compile the code. but when i try to run it. it prints the arranged number in the array then terminates. it does not print the median.

what do you think might have caused this? i dunno whats the problem.



This function is incorrect...

cpp

int median (const int array[], const int size){ //function definition
int median = array[50]; // Why use the value in array[50] if size is less

int num;
median = num/2; // num never set to anything 0/2

if(num%2 ==1){ //for odd num(size of array)
median = array[median];
}

else { //for even num
median = (array[median]+array[median-1])/2; //what if I only enter in two values
}

return num;
}


You never set num to anything so set it to size. And median should start out as array[size/2]. Then do the mod check...

cpp

if( !(num%2) ){
// Calculate the median of the two values in the array
}


Hope this helps...

JW
User is offlineProfile CardPM

Go to the top of the page

azshteeg
post 8 Aug, 2008 - 08:56 AM
Post #3


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 2

QUOTE(jwwicks @ 7 Aug, 2008 - 06:34 AM) *

Hello,

QUOTE(azshteeg @ 7 Aug, 2008 - 02:30 AM) *

I was able to compile the code. but when i try to run it. it prints the arranged number in the array then terminates. it does not print the median.

what do you think might have caused this? i dunno whats the problem.



This function is incorrect...

cpp

int median (const int array[], const int size){ //function definition
int median = array[50]; // Why use the value in array[50] if size is less

int num;
median = num/2; // num never set to anything 0/2

if(num%2 ==1){ //for odd num(size of array)
median = array[median];
}

else { //for even num
median = (array[median]+array[median-1])/2; //what if I only enter in two values
}

return num;
}


You never set num to anything so set it to size. And median should start out as array[size/2]. Then do the mod check...

cpp

if( !(num%2) ){
// Calculate the median of the two values in the array
}


Hope this helps...

JW


thanks for the help. i have edited some parts and it worked! haha.
i have submitted the codes already to my professor. thanks again!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 9/7/08 11:23PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month