[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y / ] [Home]
4chanarchives logo

Daily Programming Thread


Thread replies: 330
Thread images: 23

File: 1422385664777_6.jpg (438KB, 2500x1000px) Image search: [Google] [Yandex] [Bing]
1422385664777_6.jpg
438KB, 2500x1000px
Old /dpt/ at >>52152484

What are you working on?

HAPPY NEW YEAR EVERYONE!
>>
First for Java
>>
Xth for Go.
>>
Programming is shit
>>
Java a shit
>>
File: 1451551029327.png (402KB, 1024x768px) Image search: [Google] [Yandex] [Bing]
1451551029327.png
402KB, 1024x768px
nth for FizzBuzz of the Christ
>>
nth for Go
>>
How good is Qt for C++?
>>
>>52158633
For loops are deprecated
>>
File: genocide.png (62KB, 1024x578px) Image search: [Google] [Yandex] [Bing]
genocide.png
62KB, 1024x578px
>>52158671
The only thing I hate about Qt is the fact that the wrote their own STL.
>>
File: functional-right.jpg (512KB, 1131x1600px) Image search: [Google] [Yandex] [Bing]
functional-right.jpg
512KB, 1131x1600px
spending too long on these advent of coding problems
will I make it /dpt/?
pic related
>>
QUICK; POST A FUNCTION TO COUNT THE AMOUNT OF WORDS IN A STRING, IN YOUR LANGUAGE OF CHOICE
>>
>>52158633
Now do it without using a loop with a similar number of characters. .
>>
>>52158558
So how do you actually use Qt?
>>
>>52158671
Qt is mediocre, which makes it roughly as good as or better than the other options.

Downsides: defining their own string, vector, etc types, Mocha (reflection system), defining a bunch of shit that should have been left to the OS (e.g. threads) *and insisting you use it* (e.g. "thread-safe" functions are only safe from another QThread, not from a vanilla OS thread).

Also, they didn't put much thought into subclassing (i.e. don't expect to be able to derive from any of the widget classes beyond the completely generic ones). Or into documenting event dispatch (there are a couple of dozen different methods you can override to affect event handling, and the interactions between them aren't documented anywhere).
>>
>>52158853
Assuming space delimited string
string => string.split(' ').length
>>
>>52158671
The best by far. Nothing even comes close.
>>
>>52158853
def count_words(text):
return len(text.split(" "))
>>
>>52158853
wordCount = length . words
>>
Daily reminder that if your language of choice is not on this list you should probably give up and kill yourself:
- C++
- C#
- Lua
- Python
- Elixir
- Haskell
- Bash
- Clojure
- D
>>
>>52158853
OCaml
String.split ~on:' ' s |> List.length
>>
>>52159024
>Bash
Daily reminder to kill yourself.
>>
>>52159024
Replace Haskell with Java and I would agree.
>>
>>52159024
>Bash
>Haskell
kek
>>
>>52158853
int func(char *s)
{
int ctr=0;
while(*s != '\0')
{
if(*s==' ')
{
ctr++;
s++;
while(*s==' ' && *s!='\0')
{
s++;
}
}
s++;
}
return ctr;
}
>>
>>52158954
count_words("                      ")


Literally kill yourself 2bh
>>
which is better python 2.7 or python 3.4?
>>
>>52159024
Lua
>meme trash
>>
>>52159116
haskell
>>
>>52159116
3.4 is technically better but there's a lot of 2.7 libraries that aren't compatible with it. many people still use 2.7 because of this.
>>
>>52158853
String input = "Any random string"; 
String [] output = input.split(" ");
int answer = output.length;
>>
>>52159116
>which is better stalin or hitler
hitler obviously
>>
>>52159116
I still wonder how they were able to fuck up the community so much.
>>
>>52159167
>>52159044
>>52158954
>>52158946
Wrong.
>>
>>52158853
unsigned word_count(const char *str)
{
unsigned count = 0;
unsigned len = strlen(str);
char *tmp = (char *) malloc(sizeof(char) * len + 1);
strcpy(tmp, str);
char *token = strtok(tmp, " ");
while (token != NULL)
{
count++;
token = strtok(NULL, " ");
}
free(tmp);
return count;
}
>>
>>52159191
>>52159107
def count_words(text):
return len(text.strip().split(" "))
>>
astring.match(/\w/g).length
>>
>>52159234
*gm
>>
>>52159226
count_words("Literally               kill            your                 self")
>>
>>52159162
so i should learn 2.7? is there much difference between them?
>>
>>52159193
>>52159074
C-ucks still argue their language isn't shit, lel.
>>
File: 1365497420654.png (58KB, 447x436px) Image search: [Google] [Yandex] [Bing]
1365497420654.png
58KB, 447x436px
>>52158853
int word_count(const char* s)
{
int c = 0;
char p = *s;
while (p != '\0')
{
s++;
if (p != ' ' && (*s == ' ' || *s == '\0'))
c++;
p = *s;
}
return c;
}
>>
>>52159351
But hey, at least it's the fastest, right?
>>
>>52159351
At least my solution works.
printf("%d\n", word_count("Literally               kill            your                 self"));

./word
4

>>52159261
4
>>
>>52159316
i would just learn 3.4, and if you find a library that needs 2.7 then go back and see what's different. there aren't a ton of differences.
>>
>>52159366
I hear a C program is three times as fast as a program in any other language
>>
>>52159191
You're right, should be
input.split("\\s+");
>>
>>52159383
>only 3 times as fast
I could lose 80 pounds by chopping off one of my legs too. Doesn't mean I'm gonna do it.
>>
>>52159358
why do c-ucks get sick kicks out of making their programs indecipherable with 1-letter variable names?
>>
>>52159358
>>52159074
>>52159193
Test it with
"   You     are     wrong     "

>>52159393
Not even sure what language that is so I'll take your word for it
>>
>>52159429
>i don't know how to implement the tools that I use
webdev babby detected
>>
>>52159434
Faster compilation. You should also never use comments or formatting, and should only use the minimum possible whitespace.
>>
>>52159261

Here it is in C#.

static int count_words(string s)
{
return s.SplNoEm(' ').Length;
}

:^)
>>
>>52159444

>>52159193 here
3
>>
>>52153138
>
auto sq = cast(typeof(num))sqrt(cast(real)num);

>Is just detritus left over from when I was trying to make something typecheck correctly.
auto/var fags everyone
>>
>>52159455
>if you want to build a house, you better know how to mine the ore, smelt your own steel, cut your own wood, and craft your own hammer, before you even think about using it to hammer in any nails
>>
>>52159477
>not knowing how to make a smelter
"C is low level" fag detected
>>
>>52159477
This is more akin to you forging your own hammer for use in your projects that works better for your situation than the hammers they sell in stores.
Stop being dense.
>>
>>52159477
scooby built his own house

contractors generally do a shit job at building houses

just look at those paper houses in murrica
>>
>>52159506
>specialised hammer
This is what C programmers actually believe
>>
Is there literally any reason to use separate compilation? Seems like it's totally fucking useless. If you have the source code why not just #include it?
>>
>>52158853
fite me cunt
def noOfWords(text):
wordCount=0
startWord=False
for i in text:
if startWord and i==" ":
startWord=False
wordCount+=1
if not startWord and i!=" ":
startWord=True
return wordCount
>>
>>52159521
>durr durr merican houses suck lmao

If I had a nickel for every time a retardo Yuropeon said this, I'd have quite a few nickels.
>>
>>52159383
not necessarily. for example, hello world


python:
print "hello world"


C:
#include <stdio.h>

int main(void)
{
for (int i = 0; i < 100000000; i++) {
continue;
}
printf("Hello World\n");
return 0;
}


my point is speed depends on the programmer's skill to some degree.
>>
>>52159525
When you start building large projects that take more than 1 second to compile, you'll learn to appreciate the amount of time saved when you only need to recompile objects and not the entire program.
>>
>>52159506
>wasting all this time forging your own shitty hammer to build a house when you could've just bought one machined by people with much better equipment, R&D, quality-control, and if their hammer breaks you can send it in for a refund
>>
>his hammer isn't made of adamantium
And you call your language low-level
>>
>>52159191
Str.split (Str.regexp "[ \t\n]+") s |> List.count ~f:(function | "" -> false | _ -> true)

HAPPY?
>>
>>52159541
you have no idea what you're talking about
write an algorithm that actually demands CPU time from the kernel before making such blanket statements
>>
>>52159541

The C program will still be 3x faster on a decent compiler.
>>
>>52159547
Is this true? I've converted a relatively large project (~400k LOC) to a single translation unit build and the compile time went from 4 minutes to 14 seconds.
>>
>>52159552
>aspiring to build these kinds of houses
>>
>>52159596
>implying
>>
>he didn't mine his own silicon ore and smelt it into his computer
lmao you lot aren't good enough for /g/
>>
>>52159457
>Implying variable names aren't solely for the programmer and they don't get changed to memory addresses upon compilation and your program won't run at exactly the same speed no matter how long you make your variable names
Cuck
>>
>>52159614
pretty sure he's trolling lad
if not, there's no saving this general
>>
>>52159552
>refund
lol
open source libraries offer no warranty
>>
>>52159614
Maybe you should read that again, Gosling.
>>
>>52159562
Str.(split (regexp "[ \t\n]+") s) |> List.length;;

Fixed.
>>
How many of you actually program in vim?
>>
>>52159612

I admire James May in a totally heterosexual way.
>>
>>52159647
I program in nano.
it's pretty much the same thing, but the learning curve is zero.
>>
>>52159647
Yes.
>>
>>52158853
(define (deep-length lst)
(cond ((null? lst) 0)
((atom? lst) 1)
(else (+ (deep-length (car lst))
(deep-length (cdr lst)))))
(deep-length (read (open-input-string str)))

looked around for deep-length but couldn't find a native implementation
>>
>>52159647
I program in Emacs with vim keybindings. In fact, I'm shitposting in this thread with my custom 4chan mode. :^)

Can't imagine anyone seriously uses vim, it's shit.
>>
>>52159657
But you're into black women so you're also a total homosex faggot
>>
>>52159647
I use it for quick scripts and general file editing, but when I'm doing any serious programming I use an IDE.
>>
>>52159530
Learn this: https://docs.python.org/2/library/
What you wrote can be done in 2 lines max using built-in methods.
>>
File: lol.jpg (9KB, 289x175px) Image search: [Google] [Yandex] [Bing]
lol.jpg
9KB, 289x175px
>>52159701
>python
>>
>>52159674
>(open-input-string str)
should be
 (open-input-string (string-append "(" str ")"))
>>
>>52159683
>black WOMEN
>homosex

Lad, I..
>>
>>52159647
I use Sublime 3 with vim keybindings. (the vim keybinding plugin sucks though, it pisses me off daily)
>>
>>52159701
not with his fucking demanding restrictions m8, u pullin me leg? i'll knock u out
>>
File: Niggerturd.jpg (95KB, 358x501px) Image search: [Google] [Yandex] [Bing]
Niggerturd.jpg
95KB, 358x501px
>>52159730
>sheboons
>""""""""""women""""""""""
>>
>>52159730
You're totally straight, you just want women as high test as possible
>>
>>52158853
llength $str
>>
>>52159468
>>52159261
Blows up on null. Wasteful memory requirement for large strings. Ignorant of UTF-8 space separators. Doesn't work with tabs, line-feeds or carriage returns.

Kids these days...
>>
>>52159749
kek
>>
>>52158853

$input = "asdfghjk qwerty sdfg "
echo $input | wc -w
>>
>>52159751

Look, I like big fat black asses. I can't help it.

>>52159749

Racism isn't funny. :)
>>
/dpt/, show me your best """frob"""
>>
>>52159434
because it looks cool senpai
>>
>>52158853
function countWords(sentence) {
return sentence.replace(/[ ]{2,}/g, " ").split(" ").length;
}


In JavaScript, God's favourite programming language.
>>
>>52159794
You won't survive the day of the rope
>>
>>52159719
>>52159738
def count_words(string):
return len(string.split())


Plebs
>>
>>52159444
>" You are wrong "
Gives 3 for >>52159358
>>
Hey buds. Im working with boids (intellegent particles) using pygame and python. For some reason the sprites always go to the bottom right of the window can someone please help. http://pastebin.com/ZeATad9M
>>
>>52159824
I was just pullin your leg lmao
>>
>>52159786
>Blows up on null. Wasteful memory requirement for large strings. Ignorant of UTF-8 space separators. Doesn't work with tabs, line-feeds or carriage returns.

string t0  = null;
Console.WriteLine(count_words(t0));
string t1 = "This\r\n\r\n\tis a test.";
Console.WriteLine(count_words(t1));


0
4
>>
>>52159191
But it isn't? You never stated what kind of string, faggot, so that means a space delimited one.
>>
>>52159823
DOTR is pure populism. I bet you have read neither Moldbug nor Evola. Now is a good time to fix that.
>>
>>52159867
He said count all the words in a string m8
Why the salt you aren't even hashing
>>
>>52158853
ulong count(string str)
{
return split(str).length;
}


Everyone else btfo
>>
>>52159822
God's favourite programming language is Python, and the holy fizzbuzz

>>52159848
>>>/g/sqt/
>>
Help, the binary search in K&R isn't working, can someone fix this for me?


int kr_binsearch(int x, int v[], int n)
{
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
int low, high, mid;
low = 0;
high = n - 1;
while (low <= high) {
mid = (low+high)/2;
if (x < v[mid])
high = mid + 1;
else if (x > v[mid])
low = mid + 1;
else /* found match */
return mid;
}
return -1; /* no match */
}

int main(void)
{
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 20};
int n = (sizeof(arr) / sizeof(arr[0]));
int x = 2;

kr_binsearch(x, arr, n);

return 0;
}

>>
I did it boys!
>>
>>52159234
>>52159260

NOTICE ME SENPAI
>>
>>52159701
That's not the point of the challenge
>>
>>52159927
the absolute madman
>>
>>52159866
error CS1061: Type `string' does not contain a definition for `SplNoEm' and no extension method `SplNoEm' of type `string' could be found. Are you missing an assembly reference?
>>
>>52159927
did what?
>>
>>52159965

You have to write the extension method first.
>>
>>52159927
>says it again
absolute madman
>>
>>52159927
source or gtfo
>>
So how does one learn javascript?
Do I use the interpreter in my browser, or do I start with a framework and avoid pure js, or what?
>>
>>59715528
Most jobs are like that,doesn't matter if you're a engineer or plumber.>>52159024
>Daily reminder that if your language of choice is not on this list you should probably give up and kill yourself:
>- C++
Ok, if you are developing software that need performance.
>- C#
Replace with Java.
>- Lua
Lol useless shit
>- Python
Ok, not to write enterprise applications but small application that do not need big performance
>- Elixir
Useless cral
>- Haskell
Useless crap
>- Bash
Important if you are working as Linux system admin or in a Linux environment in general
>- Clojure
Shit
>- D
Shit
>>
>>52160067
>webdev meme
>>
>>52159993
okay now i know you're just a troll, you fucking faggot homo
>>
>>52160067
Just learn jQuery.
>>
>>52160069
>>- D
>Shit
I disagree
>>
>>52160090
fuck jquery build a site with angular
>>
>>52160069
>Replacing C# with Java
Literally why? At least C# runs fast and has SIMD. Java is just shit.
>>
>>52160069
>>>59715528
time traveller???
>>
>>52160069
>Replace with Java.
Ha Ha
C# is literally just a better version of Java, why would you use Java over C#?
>>
>>52160082

I wrote the extension method myself. All it does is hide a few things because there's no split that takes (char, StringSplitOption), only (char[], StringSplitOption).
>>
>>52159647
I do, less distractions when I don't run a GUI
>>
>>52160110

both are shit tbqh
>>
>>52160110
if you prefer the language that's one thing but C# runs about as fast as or a bit slower than java
>>
>>52160130
No it doesn't. Java doesn't support SIMD. C# does. Therefore C# can run up to 8x faster than Java (on AVX-256 CPUs).
>>
Why is Haskell useless?
>>
>>52160069
Elixir is not cral. It's actually very nice for a lot of things.
>>
>>52160110
>>52160116
Portability. (mono a shit)
Huge community.
Millions of frameworks.
Android development.

>java slow meme
https://benchmarksgame.alioth.debian.org/u64q/csharp.html
>>
File: temp.png (71KB, 634x376px) Image search: [Google] [Yandex] [Bing]
temp.png
71KB, 634x376px
>>52160151
k tard

see pic related

now i know it has mono and not .NET

but if you think java is slow and C# is fast you're a memer
>>
>>52160192
Mono doesn't count. It's shit. Compare it to native code.
>>
>>52160200
F#, no!
>>
>>52160202
if C# were 8 times faster than java it'd be faster than C... lol
>>
>>52160200
>now i know it has mono and not .NET

You realize that's a pretty big deal, right?
>>
>>52160244
No, because C code can use SIMD as well.
>>
File: ew.png (272KB, 470x624px) Image search: [Google] [Yandex] [Bing]
ew.png
272KB, 470x624px
>>52160067
You make things in JavaScript, anon.

I recommend these:
https://developer.mozilla.org/en-US/docs/Web/JavaScript
https://www.youtube.com/watch?v=zf_cb_Nw5zY

Ignore the anti-webdev cucks, JavaScript is welcome here.

Don't start messing with a frameworks until you have a basic understanding of JavaScript.
>>
Can't wait till F# native desu
>>
Is there any way to make this method more efficient in C++? It's just supposed to find the sum of the numbers 1 to n.

int f(int n)
{
int t = n;
while (--n != 0)
t += n;
return t;
}
>>
>write a brainfuck interpreter in c
>try to run hello world on it
admin$ ./brainfuck brainfuck.txt
???
?
brainfuck(16636,0x7fff7f2bf000) malloc: *** error for object 0x7fb223c03162: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

wtf
>>
>>52160246
no because the claim was
>At least C# runs fast
implying java is slow

the chart shows that java isn't slow or at least around the same order of magnitude as C# .NET
>>
File: tj.gif (1023KB, 500x362px) Image search: [Google] [Yandex] [Bing]
tj.gif
1023KB, 500x362px
A cleanup goto at the end of a function is the single most readable, maintainable, and comprehensible way to clean up in C. Prove me wrong.
>>
>>52160269
return (n*(n+1))/2;
>>
>>52160269
int f(int n)
{
if (!n)
return 0;
return n + f(n - 1);
}

;^»
>>
>>52160269
yeah m8 use python as a dll, then run it from there
>>
>>52160276
The char is bad, though. They explicitly don't let you take advantage of things like SIMD, which if you care about speed, you *will* do.

C# can SIMD, Java can't. It's that simple.
>>
>>52160272
Pretty clear error desu
>>
>>52160282
Now I feel stupid, thanks anon.
>>
>>52160200
you can squeeze performance out of C# in some niche areas (SIMD/fixed memory + pointers) which control (or even possibility, afaik no compiler will even automatically unroll loops to use SIMD for Java, while in C# you can fine-tune it manually) you don't have in JAVA.

Of course, if you don't use that additional control, the performance is roughly the same on Windows, while on Linux Mono is obvioulsy lags behind Java. Would like to see those tests re-run with .Net Core now that it's out, but almost certainly Java still performs far better on Linux
>>
>>52160252
Java uses simd as well but for simple code.
There are several bug tickets about this issue and will probably be fixed with Java 9.
>>
>>52160304
No worries lol
Learn a bit more maths
>>
>>52160299
if you care about speed you will use C/C++, not C#
>>
>>52160289

genius detected

>inb4 bait
>>
>>52160299
>They explicitly don't let you take advantage of things like SIMD

source?
>>
>>52160303
I've already used calloc on the variable it returns an error.
>>
>>52159924
This should work.

#include <stdio.h>
#define DEBUG 0
int kr_binsearch(int x, int v[], int n)
{
/* binsearch: find x in v[0] <= v[1] <= ... <= v[n-1] */
int low, high, mid;
low = 0;
high = n - 1;
if (x == v[high])
return high;

while (high - low > 1) {
#if DEBUG
printf("%d %d %d\n", low, mid, high);
#endif
mid = (low + high)/2;
if (x < v[mid])
high = mid;
else if (x > v[mid])
low = mid;
else /* found match */
return mid;
}
return -1; /* no match */
}

int main(void)
{
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 20};
int n = (sizeof(arr) / sizeof(arr[0]));

for (int i = 0; i < n; i++) {
printf("Found %d at index %d\n", arr[i], kr_binsearch(arr[i], arr, n));
}
printf("Searching for %d... Got %d.\n", 2, kr_binsearch(2, arr, n));
printf("Searching for %d... Got %d.\n", -57, kr_binsearch(-57, arr, n));
printf("Searching for %d... Got %d.\n", 42, kr_binsearch(42, arr, n));
return 0;
}
>>
>>52159074
>no strtok
do you even C?
>>
>>52160328
Post source
>>
>>52160325
Yes. And if you care about speed but don't want to write C/C++, you will write C#. Java is useless, outdated shit.
>>
>>52160309
C# does not have proper SIMD support
>>
>>52160326
c# was the b8 m8
>>
>>52160254
look at all those beta low test limp-wristed cucks teaching javascript
>>
>>52160324
I actually knew about the formula that's why I felt so stupid for not realizing I could use it.
>>
>>52160269
Here's the recursive solution:
int kek(int n){
if(n > 0)
return n + kek(n-1);
return 0;
}
>>
>>52160309
You can fine tune the JVM as well.
>>
>>52160338
Awesome, thanks anon. I like K&R but the issues are annoying sometimes.
>>
>>52160325
if you want to develop a (functionally) complex app without at least a dozen code monkey/droids, you won't touch C/C++, that simple
>>
>>52160338
>>52160356
You're welcome but I found a bug in my code. Replace

    if (x == v[high])
return high;


with

    if (x == v[low])
return low;
if (x == v[high])
return high;
>>
File: 1451337320315.jpg (25KB, 400x386px) Image search: [Google] [Yandex] [Bing]
1451337320315.jpg
25KB, 400x386px
>the C# version was still slower than the Java version, despite my use of the special SIMD instructions
>Undeterred, I thought I would try to use SIMD to improve another C# benchmark, and took a shot at the N-body benchmark test. Once again, the version I wrote, in my hands, was much faster. But when submitted to the shoot-out and accepted, it was either much slower, or didn’t even run. My submission using the SSE instructions not only didn’t beat Java, it was actually slower than the original C# version!

>C# and Java have no meaningful performance differences.
>C# and Java have no meaningful performance differences.
>C# and Java have no meaningful performance differences.

>As a result of using all the registers used, the Java code has great pipelining. Note that up to 5 vmulsd instructions show up at once. The JVM is simply brilliant at packing operations together, and this means that even though I, the programmer, was smart and used SSE2, my C# code only won by 20%, and not 200%. It’s hard to beat Java pipelining.

http://evolvedmicrobe.com/blogs/?p=299
>>
>>52160346
What is it you can't manually SIMD in C# that you can in C++?
>>
>>52160384
It's only because mono is shit. Again, with native code and AVX-256, C# outperforms Java by a factor of 4+. Can't wait until it's up to 8x when AVX-512 becomes mainstream!
>>
>>52160355
>You can fine tune the JVM as well.

wat

You can, but it'll only run on your rig..
>>
Why is this:

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#define DIGITS 2;

bool palindrome(int x);

int main(){
int n1 = pow(10, DIGITS-1);
int n2 = n1;
//const int TOP = pow(10, 2*DIGITS);
int j=0;
while(j<5){
int a;
scanf("%d", &a);
if(palindrome(a))
printf("ye");
else
printf("lmao");
}
return 0;
}

bool palindrome(int x){
char num[DIGITS*2];
sprintf(num, "%d", x);
int i=0;
while(i<DIGITS){
if(num[i] != num[DIGITS-(i+1)])
return false;
}
return true;
}


giving me this?

jews@didNineEleven:~/Programs$ gcc 4.c
4.c: In function ‘main’:
4.c:9:19: error: expected ‘)’ before ‘;’ token
4.c: In function ‘palindrome’:
4.c:25:11: error: expected ‘]’ before ‘;’ token
4.c:26:10: error: ‘num’ undeclared (first use in this function)
4.c:26:10: note: each undeclared identifier is reported only once for each function it appears in
4.c:28:10: error: expected ‘)’ before ‘;’ token
4.c:29:20: error: expected ‘]’ before ‘;’ token
>>
>>52158853

int words (char *p)
{
int words = 0;
while (*p == ' ')
p++;

while (*p != '\0') {
if (*p == ' ') {
while (*p == ' ')
p++;
}
else {
words++;
while (*p != ' ' && *p != '\0')
p++;
}
}

return words;
}
>>
>>52160384
>http://evolvedmicrobe.com/blogs/?p=299

That's Xamarin Mono versus Oracle JVM comparison on Mac, misleadingly named by a Java (and most likely Apple-) shill
>>
>>52160340
nvm figured it out, it was because of the way I handled [
>>
>>52160069
Python is used a lot outside little scripts by a lot of big companies
>>
>>52160443
Remove the ';' in
#define DIGITS 2;
>>
>>52160443
>#define DIGITS 2;
>>
>>52160446
>8 space indent
use tabs or 4 spaces
>>
>>52160486
> t. Guido van Rossum
>>
>>52160488
>>52160491
OH, first time using #define
I've just been using const
thank you~
>>
>>52160431
No it won't.
You can ship every jre with your program.
>>
>>52160446
>>52159074
these are unreliable and blow up when you have lots of whitespace between words
just make a copy of the string, replace every space with a null byte, and then keep scanning forward through the string and count the number of uninterupted character streams that aren't null bytes.
Or you know, just use strtok, because that's literally what it does.
>>
>>52160200
>java is 2x faster than swift
>people still say android apps are slower than ios
>>
>>52160519
android still can't maintain 60fps
>btfo
>>
>>52160497
Yeah, i need to get on changing the vim config for this machine, usually I program on something else.
>>
>>52160519
people perceive iOS apps as faster because the fancy schmancy UI animations have top priority
>>
File: 1444519185838.jpg (122KB, 425x516px) Image search: [Google] [Yandex] [Bing]
1444519185838.jpg
122KB, 425x516px
>>52158853
#include <stdio.h>

int main(int argc, char *argv[])
{
printf("%d\n", argc - 1);
}
>>
>>52160519
Not the fault of Java but the fault of dalvik.
>>
>>52160508
dude, I don't think you understand what we're talking about and i'm kinda tired to explain it to you

you clearly don't actually develop software (professionally) either, so at least wait till you have coded for a few years before you become a shill pls
>>
>>52160525
yes it can
>>
>>52160384
>>52160411
You guys know C# and Java are BOTH shit right?
>>
>>52160539
Oh Nigga please,you have been BTFO hard.
Now you are going all passive aggressive “i know it better than you“.

Are you mad?
>>
desu most people think android is slow just because of the shitty curry phone sterotype

>>52160525
You know apart from how they can
>btfo

>>52160538
most phones use ART now
>>
>>52159575
3x faster? More like 30x faster
>>
>>52160571
they're the perfect middle ground for certain types of applications that don't need to squeeze out every last little drop of performance. they provide high-level features while keeping type safety and being up to 2 orders of magnitude faster than for example shit like python.
>>
>>52160536
holy
shit

i love you christ chan
>>
>>52160511
>these are unreliable and blow up when you have lots of whitespace between words
I see what you mean, but I would like you to give me an example string in which my program (the first one you quoted) will break on.
I tested it on
"                  Kill                   YOUR               SELF                  "

and some variations and it worked fine. I'm not trying to argue that the method you suggested isn't better than mine, since it definitely is, but I would like an example for better understanding of what the flaw in the other code is.
>>
>>52160610
I meant even if it still did the for loop ;)
>>
C++ is the only language anyone should be programming in.
>>
>>52160341
No.... If you care about speed you write your complete, correct, solution. Then profile it, THEN see what can be done to improve speed where it's slow.
>>
>>52160672
Someone needs to fork C++ now and just fix the broken fucking syntax and add standard FP
>>
>>52160684
In Java? You'll reach a hard cap pretty fast where you're running up against the JVM.
>>
Daily reminder the C# shills our out enmass since its 2x holiday pay for new years eve. Be sure to thank Rajeed for his hard work!
>>
>>52160768
>our out enmass

Are you sure your name isn't Rajeed?
>>
This finds actual real English words and prints how many there are:

If on not Windows or Mac:
sudo apt-get install enchant


In python
import pip
pip.main(['install', 'pyenchant'])
import enchant, enchant.tokenize
english_dict = enchant.Dict("en_US")
tokenizer = enchant.tokenize.get_tokenizer("en_US")
def count_words(string, dct, tknzr):
return sum(map(lambda x: dct.check(x[0]), tknzr(string)))

count_words(" a long time _ alskdjfs coming", english_dict, tokenizer)
>>
>>52160819
>English
He said words.
>>
>>52160857
I did but this is cool too, and there's already been about 4 generic python soloutions
>>
>>52160819
or you know, you could just strtok instead of installing like 10000 libraries to do a trivial string scanning fucntion.
>>
Where were you the day /dpt/ got blown the fuck out?

>>52160536
>>
local n = 0
for word in string.gmatch(str, "%w") do
n = n + 1
end
>>
>>52160536

Lord have mercy.
>>
File: 1406908022044.gif (933KB, 343x284px) Image search: [Google] [Yandex] [Bing]
1406908022044.gif
933KB, 343x284px
>>52160536
how does this wo- HOLY SHIT
>>
>>52160819
>pip install pyenchant
>no amd64 support
>>
>>52160933
it's a neat trick, but try doing that with stdin, or pretty much any use case in where a word counting program would be useful.
You're basically asking your shell to tokenize your input in this one specific way. How do you count the words in a file, or from stdin?
>>
>>52160536
>>52160933
>>52160974

./a.out "Literally still not correct"
>>
>>52160974
clever.
>>
echo "hay guys can i play??" | wc -w
>>
>>52158558
Starting CS student here. Over fall, I create a console calculator using queues and stacks in java. Does much as what a standard scientific calculator does.

Short term goal: create a GUI for the calculator.
Long term goal: try create a similar calculator program in C++ and a webpage (using javascript and such).
>>
>>52160104
Fuck Angular, build a site with small libraries and Backbone.
>>
>>52161011
My Casio represents intermediate results as proper fractions and doesn't convert to decimal until told to, does yours?
>>
>>52160988
prog.exe < file


>implying cmd line input isn't stdin
>>
>>52158853
counted = input(">>> ")
i = 0
for item in counted.split(" "):
if item != "":
i += 1
print(i)
>>
>>52161058
that wouldn't work for the argc trick.
>>
what was the first useful thing you programmed, /g/?
>>
I have a question. Let's say in C++ I have a class like

class foo
{
private:
int n;
public:
foo(int i) {n=i;}
int getN() {return n;}
}


If I initialize 1000 foos. Are there going to be 1000 "getN"s? Or do they share the same code and just have different "n"s?
>>
>>52161105
I guess a program to generate basketball-American names?
Example output:
Liatonta
Qunanima
Lajan
Linesha
Tondeen
Sha
Lantand
Tarickickararel
Kekan
Kekes
Lenandr
Tys
Lanar
Kat
Andea
Kique
Dedei
Kaya
Denik
Tays
Tori
Dan
Jaqual
Daranie
Nimatinylina
Keshan
Qua
Toryalauane
Lickis
Tyrqui
Majashata
Ryl
Nivo
Keicke
>>
>>52160511
Better?

int words (char *p)
{
int words = 0;
char *s = strtok(p, " ");
while (s != NULL) {
words++;
while (*s != ' ')
s++;
s = strtok(NULL, " ");
}

return words;
}
>>
>>52161114
Ideally, this is implemented as a function pointer to a single getN() outside of your class, but not accessible outside of a foo object.

Or at least, I hope.
>>
>>52161114
they share the same code
>>
>>52161114
There's only 1 getN

static int foo@@getN(foo* _this) { return this->n; }
>>
>>52161135
>No L'-a

you disappoint me
>>
>>52161157
I should probably add punctuation, kek
>>
>>52158558
I'm about done with a lisp interpreter I've been working on over break. It's kinda like scheme, but with common lisp macros (because they're easy to implement.) Closures are properly working, as well as all the higher-order-function goodness. I'm just putting the finishing touches on macros and the garbage collector and I'll be satisfied. I've been testing recursive functions and higher order functions, and it all works pretty flawlessly.

I wanted a lisp interpreter that I can statically link with C programs. I'm a student, so in my networking class next semester I can implement all the low-level stuff in C, and then tie it together with my lisp.
>>
Wait

is Scala actually the best language?
>>
>>52161105
A program that counted words in a String.
>>
>>52161137
you might want to make a second copy of the string, because strtok irreversibly modifies the original string.
char *s simply points to the original string *p + an internal index that gets advanced every time you call strtok().
>>
>>52159358
>>52159193
part of the c stdlib is
ispace(int)
. You should really use this
>>
>>52161196
I guess so.
>>
>>52161140
>>52161143
>>52161154
Thanks. Makes me feel better about making a huge amount of functions that generally just point to each other.
>>
>>52161227
isspace(int)
>>
When you write a Java application, do you have to install the JVM on the user's machine if it's not already installed, or can you just embed the JVM into your executable file to create a standalone executable?
>>
>>52161137
>Better?

Did you even test your own code, how about just looking at it? The damn thing has a glaring flaw.

Hint:
*s != ' '
>>
>>52161260
Kek. Do you think people would hate Java so much if you could just always bundle it with your executable?
>>
>>52161135
I Keicke'd
>>
>>52160719
>In Java? You'll reach a hard cap pretty fast where you're running up against the JVM.
Yes and no. There are usually things that become obvious ways to improve after profiling. Admittedly, some languages are easier to speed up than others: perl, python and ruby make it dead easy to write parts as C, and then call them.
>>
>>52161202
how is that useful?

>>52161135
Tarickickararel is amazing
>>
>>52161253
Every time you add a member function to a class/struct, you're adding additional bloat to hold a function pointer that is at least as large as the largest addressable memory address.
Consider using a vtable so you can cut down your use of function pointers in your class objects to just 1.
>>
>>52161275
No idea. Someone made a build system that lets you can create standalone Erlang executables, so I figure there'd be a similar project for Java since it's way more popular..
>>
>>52161135
Markov chain generator?
>>
>>52161297
What the fuck are you talking about?
>>
>>52161313
Yeah pretty much, with a few special rules. I fed it census data.
>>
>>52161328
class foo
{
private:
int n;
public:
foo(int i) {n=i;}
int getN() {return n;}
}

Every time you create an object of class foo, you are allocating memory for:
an integer
a function pointer to foo()
a function pointer to getN()
The memory taken up by both those function pointers is over 4 times as much as the memory taken up by that integer.

Use a vtable, you moron.
>>
>>52161260
>>52161275
Never tried it, but I think JSmooth and launch4j can do this.
>>
>>52161196
Scala great! If you want a Haskell-like on the JVM that can interact with Java code, I would definitely use Scala.
>>
>>52161342
I love census data. You can find out so much interesting stuff from it. I can only imagine what someone actually trained in stats can get from the sort of data google collects now.

>>52161297
>Every time you add a member function to a class/struct
Structs don't have member functions. They have variables. That's __all__. Is C just an abstract subset of C++ to you? There's a bit more to it than just that...
>>
File: 345677199.jpg (56KB, 583x498px) Image search: [Google] [Yandex] [Bing]
345677199.jpg
56KB, 583x498px
>>52161372
>>
rate my meme function
int wordCount(char *str){
int words = 0;
while(*str){
if(isalpha(*str)){
words++;
while(isalpha(*str))
str++;
}
str++;
}
return words;
}
>>
>>52161405
>Structs don't have member functions.
you can have function pointers that work exactly like member functions except there's no self variable abstraction
>>
>>52161372
... No you aren't.
>>
>>52161260
>>52161275
That would defeat the mission of Java -- write once, run anywhere.

That being said, a lot of popular Java Applications end up shipping with a JVM to make sure the user has the correct one. There are ways to make an executable wrapper, but I don't believe there are any native machine code compilers for Java.

>>52161399
>>52161196
Clojure is also worth looking at.
>>
>>52161372
no, he's not
you moron
>>
>>52161442
how do i code text???
>>
>>52161297
What I meant was that I have a lot of functions like

foo &foo::operator+=(foo &m) {
this->n += m.getN();
return *this;
}
void foo::add(foo &m) {
this->operator+=(m)
}


I just have a few pieces of code that uses foo::add. I don't want to go ahead change every single instance of it. Too much time to do it. Although I can turn it into an inline function. But still.
>>
>>52161469
Will look into clojure

>>52161486
Member functions don't use function pointers and shouldn't be using vtables, he doesn't know what he's talking about

Also, you don't need to do this->n in your member functions, you can just use n
And you can do operator+=(m), you don't need this->operator+=(m)
>>
>>52161272
It actually works fine, but I guess you mean I should have this instead

while (*s != ' ' && *s != '\0')


but I don't even need to bother doing that when i could be just doing this

int words (char *p)
{
int words = 0;
char *s = strtok(p, " ");
while (s != NULL) {
words++;
s = strtok(NULL, " ");
}

return words;
}


right?
>>
>>52161515
I use this-> for clarity's sake. I don't want to look at my code a few years down the line and think "Wait... where is this defined? Is this a class function what?
>>
Guys why does this not work?
isalpha(me)
>>
>>52161544
... it's in a member method.

this->n doesn't really give clarity

if you __really__ want verbosity, just call all your properties m_name
>>
>>52161572
I meant that it could be part of an outer scope.

And Hungarian notation doesn't really work for me. I constantly have to have a cheat sheet open to look up the most basic notation
>>
>>52161546
I dunno. It should be returning false like normal.
>>
>>52161602
If you use visual studio or any other modern IDE, you can just use intellisense
>>
>>52161643
Who do you think I am? A cuck? I use nano
>>
>>52161546
Because the method 'isalpha' is undefined for the object 'me' so the dispatch selects the default object implementation itself, which always returns true.
>>
>>52161546
Because it's supposed to be in camal case isAlpha(), and the inverse function, is_beta() uses underscores.
>>
>>52161643
Who do you think I am? A cuck? A nano-cocked? I use vim
>>
>>52161661
>what do you think i am? a cuck? no, i'm THE cuck
>>
File: 2012powerbig.jpg (250KB, 1024x776px) Image search: [Google] [Yandex] [Bing]
2012powerbig.jpg
250KB, 1024x776px
>>52161661
>Who do you think I am? A cuck? I use nano
So, you are a cuck then?
>>
>>52161671
Fuck off back to your country, jamal jase!
>>
>>52161543
>It actually works fine
No it segfaults on everything.

>but I guess you mean I should have this instead
it shouldn't exist at all, you've tokenized the string and removed all spaces but you've created a loop looking for spaces. You might never find a space. As such that condition might never be satisfied. Look up overflows.
>>
Is it hard to write an optimizing compiler? I am thinking about writing my own C-like language (but fix some of C's glaring flaws, without dipping into the sea of shit that is C++)
>>
>>52161720
Yes, and fuck off with your "expert fizzbuzz knowledge".
>>
>>52161712
>No it segfaults on everything.
It doesn't on my machine. Maybe you're using a deprecated compiler like gcc?
>>
File: 1434229519851.png (210KB, 871x900px) Image search: [Google] [Yandex] [Bing]
1434229519851.png
210KB, 871x900px
>>52160989

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>

void die(const char*, ...);
#define DIE(format, ...) die("File: '%s', line: %d\n" format, __FILE__, __LINE__, __VA_ARGS__)

char *concat(int argc, char *argv[])
{
char *buffer = malloc(strlen(argv[1]));
strcpy(buffer, argv[1]);

for (int i = 2; i < argc; ++i)
{
strcat(buffer, " ");
strcat(buffer, argv[i]);
}
return buffer;
}

int main(int argc, char *argv[])
{
if (argc < 2)
DIE("%s\n", "Too few arguments");

char *str = concat(argc, argv), *tok = strtok(str, " ");
uint_fast64_t n = 1;

while ((tok = strtok(NULL, " ")))
++n;

printf("%" PRIuFAST64 "\n", n);
free(str);
}

void die(const char *format, ...)
{
va_list vargs;
va_start(vargs, format);
vfprintf(stderr, format, vargs);
va_end(vargs);
exit(1);
}
>>
>>52161720
it's hard to write one that's really good
>>
>>52161720
>Is it hard to write an optimizing compiler?
LLVM has been under development for 12 years with massive financial backing and has only just caught up with gcc.

Your best bet would be to "compile" your language to C and then let gcc/llvm do the optimizing for you.
>>
>>52161720
>thinking about writing my own C-like language (but fix some of C's glaring flaws, without dipping into the sea of shit that is C++)
Before you do that, make sure you're up to date with the latest C standards. What most people are taught in school is ANSI C, or C89. There were major updates in 1995, 1999, and 2011 (called c95, c99 and c11), and they bring in a lot of what you might want.
>>
>>52161737
Really if we just had function overloading, namespaces, and arrays that didn't decay, the language would already be an improvement.
>>
These thread should be called /dp?c?t/.

haha!
>>
>>52161764
Really if you knew anything beyond fizzbuzz you wouldn't be stuck in the "c is the only language in the world" mindset.
>>
>>52161745
That's because you've got enough memory that a space probably exists somewhere in your memory. That doesn't change the fact that it's overflowing.
>>
>>52161720
Make sure you learn Ocaml or F# before you write your own C like language
>>
>>52161777
For performance, that's absolutely the case. Don't bring up Ada.
>>
>>52158633
what is the Math Wizardry behind this?
>>
>>52161760
>LLVM has been under development for 12 years with massive financial backing and has only just caught up with gcc.
>just caught up
>implying it hasn't surpassed gcc in every way

gnutards are so fucking delusional
>>
NEW

>>52161797
>>52161797
>>52161797
>>52161797
>>
>>52161784
so an amateur like you is going to make his own C with more features with the same performance as C
>>
>>52161784
>I'm retarded and there's nothing you can do about it!
>>
File: christ.jpg (60KB, 353x313px) Image search: [Google] [Yandex] [Bing]
christ.jpg
60KB, 353x313px
>>52161748
Deus vult, Christ-chan!
>>
>>52161796
LLVM might be better in design but only recently has it caught up with GCC performance wise.
>>
>>52161804
Probably fewer features, but features that actually matter. And maybe? It depends how hard it is to achieve comparable performance.
>>
>>52161796
It hasn't even caught up. Proof: it still can't even compile most valid C++. Checkmate, homosexuals!
>>
>>52161826
>fewer features than C
wew lad
>>
>>52161826
so assembly?
>>
>>52161780
You're right, I'm retarded. I'll remember this for next time.
>>
>>52161840
>implying ANYONE needs for loops
>>
>>52161829
>Checkmate, homosexuals!
Dude, be nicer to fags. No reason to be so hostile as to compare them to LLVM. That's just rude.
>>
>>52161870
literally kill yourself
>>
>>52160210
works better than Go
>>
>>52161114
In that case, sizeof(foo)==sizeof(int).

If you don't have any virtual methods or bases, the size of a class instance is the size of the equivalent C struct (i.e. the instance only contains the data members).

Non-virtual methods are accessed just like normal functions.

If you have one or more virtual methods, the language adds a hidden pointer to the vtable to each instance of the structure.

If you have multiple base classes (multiple inheritance), you get a vtable pointer for each base class which has one.

If you have any virtual base classes, you get an extra pointer for each one (in addition to any vtable pointers).
>>
>>52161822
so it's better designed and has equal or better performance. got it.
>>
Where can I rent servers with AMD Radeon GPUs, other than from IBM who have ridiculous prices?
>>
>>52160409
Well, you can of course write your own SIMD library in C++ and use it in C#
But C# itself only has very poorly implemented SIMD that is barely worth using
>>
>>52161372
god you are dumb. here is what the compiler will generate behind the scenes:
int getN(foo* this)
{
return this->n;
}


vtables get created by the c++ compiler automatically and are used with inheritance and virtual function calls, neither of which are present in this type of code.

please kill yourself. i'd suggesting standing on a (v)table and hanging yourself, then kicking the (v)table out from beneath your feet. less overhead that way.
>>
File: schopen.jpg (31KB, 300x358px) Image search: [Google] [Yandex] [Bing]
schopen.jpg
31KB, 300x358px
>>52161771
>>
>>52160903
strtok doesn't recognize English words.
>>
File: 1449341780248.png (166KB, 292x284px) Image search: [Google] [Yandex] [Bing]
1449341780248.png
166KB, 292x284px
>elem = container[ i ];
>[ i ]
>function( a, b );
>( a, b )
Thread replies: 330
Thread images: 23
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y / ] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
If a post contains illegal content, please click on its [Report] button and follow the instructions.
This is a 4chan archive - all of the content originated from them. If you need information for a Poster - you need to contact them.
This website shows only archived content and is not affiliated with 4chan in any way.
If you like this website please support us by donating with Bitcoin at 1XVgDnu36zCj97gLdeSwHMdiJaBkqhtMK