Liste des Groupes | Revenir à col advocacy |
On 6/10/2024 10:40 PM, candycanearter07 wrote:I knew there was an official implementation!DFS <nospam@dfs.com> wrote at 23:30 this Saturday (GMT):>On 6/8/2024 5:32 PM, Joel wrote:
>I can't imagine I'd need anything beyond C.>
>
I hope you're not gonna pull a Feeb and claim you're a C guru that can
'move the Earth', but refuse to show ANY decent code, then make lame
excuses like "I have other fish to fry".
Seems like a fun challenge.. especially since im kinda rusty with c
1) write your own C to sort these 3 names (by last then first):
>
William Thomas
Zachary Jones
Jim Thomas
I'll be honest, I might be bad but I completely forgot how to sort
strings.. sorry
>
Usually qsort().
>
>Right, I forgot isalnum was a thing.>2) write your own C to count the words in:
>
"Now is the time for all good men to come to the aid of their country."
#include <stdio.h>
#include <ctype.h>
const char phrase[] = "Now is the time for all good men to come to the
aid of their country.";
int main()
{
const char *phr = phrase;
int words = 0;
int space = 0; // does c have bool?
Yes.
>while(*phr)>
{
if(space && isalpha(*phr))
{
space = 0;
words++;
}
if(isblank(*phr)) space = 1;
phr++;
}
printf("Words: %d, words);
}
>
My wordcounter code used:
>
if(isspace(myStr[i]) && !isspace(myStr[i+1])) {countWords++;}
>
to also count words that might begin with numbers.
>
"For the right money cola Linux 'advocates' would work 4MS in 2ms."
>
>
>>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?
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 like the technique you use for the mode count, but something to
remember is there can be multiple modes, and your code as is will
identify only one.
>
And the numbers were already sorted, but if they weren't you would have
to sort them - forward or reverse - before getting the median.
>>Why is it I have a VERY strong feeling you and Feeb and C are about to
be pwned by python?
IMO, both are pretty good languages, with their own strengths..like, C
is good for dealing with the raw data and it can be really satisfying to
get everything working, and python is great for throwing something
together.
>
I like them both, too. But C is too time-consuming for me. I just
don't spend enough time using it exclusively to be productive vs python.
Another language I've had my eye on is Julia.
Les messages affichés proviennent d'usenet.