449,173 Members | 1,049 Online Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

 P: n/a Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include int main() { int item; int a, b, t, mode; int median_index; float median, avg; int count; printf("How Many numbers do you want to enter? "); scanf("%d", &count); for(a=0; a=a; --b){ if(item[b-1] > item[b]){ t=item[b-1]; item[b-1] = item[b]; item[b]=t; } } /*median*/ median = count/2; if(count%2 ==1){ printf("There are odd sets of numbers.\n"); median = item[count/2]; } else { median_index = count/2; median = (item[median_index]+item[median_index-1])/2.0; } printf("The Median is %.1f\n", median); /*average*/ avg = 0; for(a=0; a
13 Replies

 P: n/a agentxx04 wrote: Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include insert #define NUMITEMS (100) int main() make that int main (void) { int item; make this NUMITEMS; int a, b, t, mode; int median_index; float median, avg; C does not give any guarantees about the relative integer ranges of floating point variables, but if sizeof(float)==sizeof(int) then there exist ints (for example INT_MAX from ) which cannot represented exactly by floats. So, I would suggest to use either double or long double for median and avg. int count; printf("How Many numbers do you want to enter? "); scanf("%d", &count); Check whether count <=NUMITEMS -- otherwise I would just try and enter NUMITEMS+1... for(a=0; a=a; --b){ if(item[b-1] > item[b]){ t=item[b-1]; item[b-1] = item[b]; item[b]=t; } } Determination of the median: You need a sorted array and the size of the array (and have to know the array type. You expect a float/double/long double value out of this. /*median*/ median = count/2; This is unnecessary. if(count%2 ==1){ printf("There are odd sets of numbers.\n"); median = item[count/2]; } else { median_index = count/2; median = (item[median_index]+item[median_index-1])/2.0; count = 3 => median_index = 1 => you are looking at item and item. Make it median_index+1. } printf("The Median is %.1f\n", median); Determination of average: You need array, type of array members and number of array elements. array has not to be sorted. You expect a float/double/long double value out of this. /*average*/ avg = 0; for(a=0; a=a; --b) { if (array[b-1] > array[b]) { tmp = array[b-1]; array[b-1] = array[b]; array[b] = t; } } } Example: Median: In: array, count Out: float/double/long double; in keeping with your program, I will use float but disadvise its use. float array_median (int size, int array[]) { int index; /* index, type like size */ float median; /* median variable, type like return type */ index = size/2; if ( (size%2) == 1 ) { printf("There are odd sets of numbers.\n"); median = array[index]; } else { median = (array[index]+array[index+1])/2.0; /* (*) Mark */ } return median; } Rest left to reader as exercise. Notes: - For array indices, it is better to use the type size_t (instead of int), as size_t ist guaranteed to work for all possible indices. (If you have 16-Bit integers and 2GB memory and use an array with more than pow(2,15)-1 elements (e.g. 33000), then int cannot used any longer as index whereas size_t can.) - (*): If array[index]+array[index+1]>INT_MAX, you run into trouble. If you demand that one of the two is converted to double or long double prior to addition then the sum will be done in (long) double: median = ((double)array[index]+array[index+1])/2.0; suffices. - Your sorting algorithm is the worst possible. Look for insertion sort or selection sort for (marginally) better "simple" algorithms and for Shell sort or quicksort for more sophisticated algorithms able to deal quickly (in comparison with the former) with large arrays. - There are some more issues but I suggest you work on your code and come back then. Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. Nov 14 '05 #2

 P: n/a agentxx04 wrote: Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: Try something like this (but don't try turning this in unless you can justify every line): #include #include int qsort_dblcmp(const void *, const void *); int qsort_cntcmp(const void *, const void *); double get_median(int n, double x[n]); double get_average(int n, double x[n]); void get_modes(int n, double x[n], int *nm, double m[n]); int main(void) { double *item, *modes; int ndx, nmode; double median, average; int count; printf("How Many numbers do you want to enter? "); scanf("%d", &count); if (count < 1) exit(EXIT_SUCCESS); if (!(item = malloc(count * sizeof *item))) { fprintf(stderr, "I could not get space for %d numbers.\n" "quitting ...\n", count); exit(EXIT_FAILURE); } if (!(modes = malloc(count * sizeof *modes))) { fprintf(stderr, "I could not get space for %d possible modes.\n" "quitting ...\n", count); exit(EXIT_FAILURE); } for (ndx = 0; ndx < count; ndx++) { scanf("%lf", &item[ndx]); } qsort(item, count, sizeof *item, qsort_dblcmp); median = get_median(count, item); average = get_average(count, item); printf("The Median is %g\n", median); printf("Average is: %g\n", average); get_modes(count, item, &nmode, modes); printf("There are %d nodes. They are: \n", nmode); for (ndx = 0; ndx < nmode; ndx++) printf("%g\n", modes[ndx]); free(item); free(modes); return 0; } int qsort_dblcmp(const void *p1, const void *p2) { return *(double *) p1 - *(double *) p2; } double get_median(int n, double x[n]) { if (n % 2) return x[n / 2]; return (x[n / 2] + x[n / 2 - 1]) / 2; } double get_average(int n, double x[n]) { double sum = 0; int i; for (i = 0; i < n; i++) sum += x[i]; return sum / n; } typedef struct { double v; int n; } Cnt; void get_modes(int n, double x[n], int *nm, double m[n]) { int i, this = 0; Cnt cnt[n]; if (n < 1) { *nm = 0; return; } for (i = 0; i < n; i++) { cnt[i].v = 0; cnt[i].n = 0; } cnt.v = x; cnt.n = 1; for (i = 1; i < n; i++) { if (x[i] != cnt[this].v) { this++; cnt[this].v = x[i]; } ++cnt[this].n; } qsort(cnt, this + 1, sizeof *cnt, qsort_cntcmp); *nm = 0; m = cnt.v; for (i = 1; i <= this && cnt[i].n == cnt[i - 1].n; i++) { ++*nm; m[*nm] = cnt[i].v; } ++*nm; } int qsort_cntcmp(const void *p1, const void *p2) { return ((const Cnt *) p2)->n - ((const Cnt *) p1)->n; } Nov 14 '05 #3

 P: n/a > double get_median(int n, double x[n]); double get_average(int n, double x[n]); I had a couple questions regarding the above function prototypes: 1. Is the 'n' in 'double x[n]' related to the first parameter 'int n'? I'm assuming it's not. 2. Does adding 'n' to 'double x[n]' mean anything in a function declaration? I'm assuming it doesn't. If my assumptions are correct, I'd think it's more clear to write 'double x[]' or just 'double[]' as your parameter. Nov 14 '05 #4

 P: n/a Method Man wrote:[Attribution inserted:] Martin Ambuhl wrotedouble get_median(int n, double x[n]);double get_average(int n, double x[n]); I had a couple questions regarding the above function prototypes: 1. Is the 'n' in 'double x[n]' related to the first parameter 'int n'? I'm assuming it's not. 2. Does adding 'n' to 'double x[n]' mean anything in a function declaration? I'm assuming it doesn't. If my assumptions are correct, I'd think it's more clear to write 'double x[]' or just 'double[]' as your parameter. Martin wrote C99 code; there, 1. is answered with yes and 2. is answered with x is a VLA of n doubles. Cheers Michael -- E-Mail: Mine is a gmx dot de address. Nov 14 '05 #5

 P: n/a Michael Mair wrote: agentxx04 wrote: Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include insert #define NUMITEMS (100) int main() make that int main (void) { int item; make this NUMITEMS; int a, b, t, mode; int median_index; float median, avg; C does not give any guarantees about the relative integer ranges of floating point variables, but if sizeof(float)==sizeof(int) then there exist ints (for example INT_MAX from ) which cannot represented exactly by floats. So, I would suggest to use either double or long double for median and avg. I would suggest type int for the median. -- pete Nov 14 '05 #6

 P: n/a pete wrote: Michael Mair wrote:agentxx04 wrote:Hi. Our assignment was to creat a program that can find the average, median& mode of a #of integers. Here's my program:#includeinsert#define NUMITEMS (100)int main()make that int main (void){ int item;make this NUMITEMS; int a, b, t, mode; int median_index; float median, avg;C does not give any guarantees about the relative integer rangesof floating point variables, but if sizeof(float)==sizeof(int)then there exist ints (for example INT_MAX from )which cannot represented exactly by floats. So, I would suggestto use either double or long double for median and avg. I would suggest type int for the median. The OP defines the median for an even number of array elements as average of the two in the middle. For a difference of 1 between the two we cannot find an int value to replace the average. This is consistent with, e.g, the definition on http://www.shodor.org/interactivate/dictionary/m.html (note: this is the first I came across when googling for definition of median) : |"Middle value" of a list. The smallest number such that at least half |the numbers in the list are no greater than it. If the list has an odd |number of entries, the median is the middle entry in the list after |sorting the list into increasing order. If the list has an even number |of entries, the median is equal to the sum of the two middle (after |sorting) numbers divided by two. The median can be estimated from a |histogram by finding the smallest number such that the area under the |histogram to the left of that number is 50% (cf Mean, Median and Mode |Discussion). so I think double is the right thing. However, we are doing C, not statistics, so I leave it to the OP which definition he wants :-) Cheers Michael -- E-Mail: Mine is a gmx dot de address. Nov 14 '05 #7

 P: n/a Michael Mair wrote: I would suggest type int for the median. The OP defines the median for an even number of array elements as average of the two in the middle. For a difference of 1 between the two we cannot find an int value to replace the average. However, we are doing C, not statistics, so I leave it to the OP which definition he wants :-) Sorry. I realize my suggestion was off topic too. -- pete Nov 14 '05 #8

 P: n/a "agentxx04" writes: Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include int main() { int item; int a, b, t, mode; int median_index; float median, avg; double mean(int* arr, int n); double median(int* arr, int n); int mode(int* arr, int n); Write the matching functions. arr is item (it's already a pointer), and n is the number of valid items in arr. I think the instructor asssumes that there *is* a mode and that n is at least 3 or so. It is customary to use double in C where common sense would lead you to use float. Float is usually treated as a poor relation. Nov 14 '05 #9

 P: n/a Michael Mair wrote: Method Man wrote: Martin Ambuhl wrotedouble get_median(int n, double x[n]);double get_average(int n, double x[n]); 1. Is the 'n' in 'double x[n]' related to the first parameter 'int n'? I'm assuming it's not. 2. Does adding 'n' to 'double x[n]' mean anything in a function declaration? I'm assuming it doesn't. Martin wrote C99 code; there, 1. is answered with yes and 2. is answered with x is a VLA of n doubles. If so, then why would he have bothered with the first parameter? The n in x[n] means exactly the same in C99 as it did in C89 (ie. nothing). You can't pass VLAs by value any more than you can pass regular arrays by value. So the answers are 'no' and 'no'. Nov 14 '05 #10

 P: n/a "agentxx04" wrote Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: The problem is that I need to have the average, median & mode as 3 separate functions. I don't know how to set those functions up. Your file goes like this. #include #include /* plus anything else you call */ /* prototypes */ double mean(int *vals, int N); double median(int *vals, int N); int mode(int *vals, int N); int main(void) { /* code to input an array of integers */ /* we will assume that a pointer called array points to the input list, and N is the number of values entered */ printf("Mean %g\n", mean(array, N)); printf("Median %g\n", median(array, N)); printf("Mode %d\n", mode(array, N)); /* free array and clean up here */ } /* mean - return the mean of a list of intergers Params: vals - the values N - number of values Returns: the arithemtical mean; */ double mean(int *vals, int N) { int i; double answer = 0; for(i=0;i

 P: n/a Old Wolf wrote: Michael Mair wrote:Method Man wrote: Martin Ambuhl wrotedouble get_median(int n, double x[n]);double get_average(int n, double x[n]);1. Is the 'n' in 'double x[n]' related to the first parameter'int n'? I'm assuming it's not.2. Does adding 'n' to 'double x[n]' mean anything in afunction declaration? I'm assuming it doesn't.Martin wrote C99 code; there, 1. is answered with yes and 2. is answeredwith x is a VLA of n doubles. If so, then why would he have bothered with the first parameter? 1) Because the *first* parameter is the array size, needed whether you use VLAs or not. I believe you are confused 2) Because I have a consistent policy of declaring all functions with array arguments with the size information, no matter what the dimensionality. The n in x[n] means exactly the same in C99 as it did in C89 (ie. nothing). But, you silly twit, the *first* parameter, which you feel is unnecessary has nothing to do with that (reason 1). And the fact that x[] and x[n] behave the same is irrelevent when a consistent style is adopted and including it costs nothing (reason 2). You can't pass VLAs by value any more than you can pass regular arrays by value. So the answers are 'no' and 'no'. What crap. Nov 14 '05 #12

 P: n/a Old Wolf wrote: Michael Mair wrote:Method Man wrote: Martin Ambuhl wrotedouble get_median(int n, double x[n]);double get_average(int n, double x[n]);1. Is the 'n' in 'double x[n]' related to the first parameter'int n'? I'm assuming it's not.2. Does adding 'n' to 'double x[n]' mean anything in afunction declaration? I'm assuming it doesn't.Martin wrote C99 code; there, 1. is answered with yes and 2. is answeredwith x is a VLA of n doubles. If so, then why would he have bothered with the first parameter? The n in x[n] means exactly the same in C99 as it did in C89 (ie. nothing). You can't pass VLAs by value any more than you can pass regular arrays by value. So the answers are 'no' and 'no'. Err... You are right. Thanks for pointing that out. Major brainfart by me. The difference comes out in #include #include void vlatest(int m, int vla[m][m]) { printf("%zu %zu\n", sizeof vla, sizeof vla); } int main (int argc, char **argv) { int size; if (argc>=2) { size = atoi(argv); int array[size][size]; printf("%zu %zu\n", sizeof array, sizeof array); vlatest(size, array); } return 0; } Even if using %lu and using int arr; vlatest(4, arr); instead of what I do in main(), a C89 compiler won't be happy :-) See also 6.7.5.2, especially # 10 (Array declarators, Example 4) in the C99 standard. Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. Nov 14 '05 #13

 P: n/a On 9 Nov 2004 12:53:35 -0800, ol*****@inspire.net.nz (Old Wolf) wrote: Michael Mair wrote: Method Man wrote: Martin Ambuhl wrote>double get_median(int n, double x[n]);>double get_average(int n, double x[n]); 1. Is the 'n' in 'double x[n]' related to the first parameter 'int n'? I'm assuming it's not. 2. Does adding 'n' to 'double x[n]' mean anything in a function declaration? I'm assuming it doesn't. Martin wrote C99 code; there, 1. is answered with yes and 2. is answered with x is a VLA of n doubles. If so, then why would he have bothered with the first parameter? The n in x[n] means exactly the same in C99 as it did in C89 (ie. nothing). You can't pass VLAs by value any more than you can pass regular arrays by value. So the answers are 'no' and 'no'. Not quite. In C89 the array bound must either be omitted -- double x[] -- or a constant expression, which n is not. The (topmost) bound of an array function parameter is ignored in rewriting to pointer, so in C99 T x[n] and T x[] both mean the same as T*x; in C89 only the latter is permitted and it does mean the same. - David.Thompson1 at worldnet.att.net Nov 14 '05 #14

### This discussion thread is closed

Replies have been disabled for this discussion. 