Liste des Groupes | Revenir à ol advocacy |
DFS <nospam@dfs.com> wrote at 13:15 this Wednesday (GMT):Did you print the contents of md_count[] ? That should give you a clue.On 6/12/2024 2:35 AM, candycanearter07 wrote:The mistake I made.DFS <nospam@dfs.com> wrote at 16:20 this Tuesday (GMT):>
>>>>3) write your own C to calc the mean, median and mode of:>
>
1,2,3,4,5,5,6,7,8,9
#include <stdio.h>
#define COUNT_CNT 10
>
int nums[] = {1, 2, 3, 4, 5, 5, 6, 7, 8, 9, -1};
>
int main()
{
double mean;
int median, mode, md_count[COUNT_CNT], len;
>
for( len = 0; nums[len] != -1; len++ )
{
mean += nums[len];
md_count[nums[len]]++;
}
mean /= len;
>
if(len % 2 == 0)
{
int mid = len / 2;
median = nums[mid] + nums[mid + 1]];
median /= 2;
}
else
median = nums[len / 2];
for( int i = 0; i < COUNT_CNT; i++ )
{
if(md_count[mode] < md_count[i]) mode = i;
}
>
printf("mean %g, median %d, mode %d", mean, median, mode);
}
>
These compiled fine on my machine (gcc 12.2.0)
>
Did you actually run the code?
Yes, but I also typed it into slrn by hand bc I was worried that pasting
it would mess up word wrapping
>I got a segfault (WSL Ubuntu, gcc 11.4). Had to make a few tweaks to>
get it to run and provide correct answers.
>
Do you want me to tell you what I did, or do you want to figure it out?
>
Hint: after the line "mean /= len;" print out the contents of
md_count[]. You should get:
>
1. 1
2. 1
3. 1
4. 1
5. 2
6. 1
7. 1
8. 1
9. 1
I don't see it sorry ^^:
>
You don't see what?
Les messages affichés proviennent d'usenet.