/dpt/ - Daily Programming Thread

What are you working on, Sup Forums?


Previous Thread:

Other urls found in this thread:

github.com/desvox/q4/
twitter.com/AnonBabble

first for java

nth for GNU emacs

Third for the poster below me you are a great person :)

ty

im reading little schemer

VERY INTERESTING

first for D

Shame it's about scheme and not a good language

scheme is a cute language

when will we get an anime adaptation?

>doesnt like scheme
why you here bro

Scheme is shit

how can i distribute 18 random numbers from 1 to 18 into a 9x2 matrix without having any repeating numbers in c?

int alwaysRandom()
{ return 3; }

very carefully. you can get nasty bugs there.

...

making structs

I want to learn about systems programming for Linux and Unix, and I don't know whether I should start reading through Advanced Programming in the Unix Environment, or Linux Programming Interface. APUE seems to be more general, which is good, but I have heard that Linux has some different system calls that should be used in place of the Unix ones. Should I stop worrying and just work through APUE or should I use LPI and try to look the differences up if I end up programming for BSD or macos? Is there even enough of a difference that it matters?

>not making everything a class and all members classes and adding one setter and two getters and adding move constructors and variants for every possible initialisation because it's better that way ... for some reason

Why isn't long a subclass of int?

sizeof(long) >= sizeof(int)

>subclass

Create array with 1 to 18 in it.
Shuffle the array (Fisher-Yates shuffle is a good algorithm).
Read from the array into the matrix.

anything an int can do a long can do better

ive got an existing system that doesnt work, has unreferenced codepaths, event drivent driven elements that immediately pull the program from its current state onto a seperate codepath. how do i properly represent this shit as a diagram if i want to illustrate the design of the program?

im thinking UML activity diagrams, but i dont know if thats appropriate or not for this kind of thing.

Blocks that are too long

Because you can only have one mutable borrow at any given time, you might get issues if you want to mutably borrow something twice in the same function. Even if the borrows don’t overlap, the borrow checker will complain.

Let’s look at an example that does not compile.
struct Person {
name: String,
age: u8,
}

impl Person {
fn new(name: &str, age: u8) -> Person {
Person {
name: name.into(),
age: age,
}
}

fn celebrate_birthday(&mut self) {
self.age += 1;

println!("{} is now {} years old!", self.name, self.age);
}

fn name(&self) -> &str {
&self.name
}
}

fn main() {
let mut jill = Person::new("Jill", 19);
let jill_ref_mut = &mut jill;
jill_ref_mut.celebrate_birthday();
println!("{}", jill.name()); // cannot borrow `jill` as immutable
// because it is also borrowed
// as mutable
}

The problem here is that we have mutably borrowed jill and then try to use it again to print her name. The fix is indeed to limit the scope of the borrow.
fn main() {
let mut jill = Person::new("Jill", 19);
{
let jill_ref_mut = &mut jill;
jill_ref_mut.celebrate_birthday();
}
println!("{}", jill.name());
}

In general, it can be a good idea to limit the scope of your mutable references. This avoids problems like the one showcased above.

Do you already have 18 unique numbers and want to distribute them randomly? If so, Knuth's shuffle would do the job.
If you're asking about actually getting the 18 unique numberd in the first place you can look into rngs which produce a complete sequence of say the 32 bit range and grab a sample from that.

>Pick a random number between 1 and 18!
>Figure out which permutation it corresponds to.

>tfw I never have any good ideas
>tfw that makes me not want to program

I wish I just had some idea of what I wanted to do. I don't want to make the #341 platformer or another fucking stupid meme script.

I dunno, you guys have any cool ideas to throw at me? I kind of want to do something in C# but I'm not too sure.

Write an NFS 3 server.

Write a fightan game

and make it freedom source

is downloading unavailable on it-ebooks.info for anyone else?

yeah, it has been like that for the last 3 or 4 months.

rip

what's the point in doing this
why am i programming
does this really matter

Did anyone figure out what this guy suffers from?

I've been stuck on this for longer than I'd care to admit.

Say I've got a list of strings, some of which have incomplete data in them, like this:
34,22,01,04
22,14,,09
24,44,21,3
44,,33,17
09,31,33,

How would I replace the ",,"s with ",0," and the ",\n"s with ",0\n" in C? I've tried strtok, but it doesn't accept ",," as a delimiter (splits them at the first comma).

I write code because im dead on the inside and need something to distract me from the downward spiral I've thrown myself in :^) :^)

ANYWAYS HI IM GONNA SHILL MY EMACS BROWSER

New commit! The reply navigator is actually finished now, and better introductory documentation. Pic related (emacs is not very aesthetic, baka)

Write some useless library no one will ever use.

Just the other day I wrote a type-parametric arena-based block memory allocator generator for C. It used the preprocessor to generate type parameterized functions, so it was strongly-typed and had no overhead trying to determine block size. No one will ever use it though. And not just because I didn't put it up on Guthub =^)

(((pic actually related this time)))

Use strtok. Split on a single comma. If the field is empty, fill it. Of course you can't just insert, you are going to have to copy as you go.

im so bad at shilling I didn't even put a link

github.com/desvox/q4/

char* x = "12,,13,\n12";

while ((x = strchr(x, ',')) {
if (x) {
if (x[1] == ',' || x[1] == '\n') {
// important code to insert zeros and resize string if needed here
}
}
}

somethign liek that

I thought about hardcoding it too, but it's a list of over 3000 strings, and the incomplete data is missing from random spots all over the place.

I'll try that in a bit. Looks like it should work though.

>if (x) {
That's pointless, considering the while loop condition already guarantees that.

you're right
I didn't initially put it in a loop so I forgot to remove that

>resizing string every fill
*pukes*

>not using having a resizable string header to realloc more memory only if needed
*pukes*

>c
*pukes*

you win

Do you guys think I can program my lg v20 to start a timer for different lengths by using different fingers on the scan button? Or basically to program a particular action depending on what finger I scan?

you can try

I know I can try. Just wondering if anyone knows if it's possible.

if it can detect different fingers

>I thought about hardcoding it
Hardcoding it how?

Here's a little thing
char *last, *curr, c;
int i, j;
for (i = 0, j = 0; (c = str[i]) != '\0'; str++, j += (c == ','));
char *str_filled = malloc(strlen(str)+1+j);
if (str_filled == NULL) {
puts("ded");
exit(1);
}
for (last = strtok(str, ','); (curr = strtok(NULL, ',')) != NULL; last = curr) {
strcat(str_filled, ",");
if (last+1 == curr) {
strcat(str_filled, "0");
} else {
strncat(str_filled, last, curr - last);
}
}
str_filled = realloc(strlen(str_filled)+1);
if (str_filled == NULL) {
puts("ded");
exit(1);
}


Read above code bozo.

Realized I missed commas at the end. I'm sure you aren't retarded and can debug it.

Should I sign up for a C++ programming course at my local CC?

Might be fun

>tfw left my algorithms class project for the last day and realize i have no idea how to do it

im a fucking MORON

No

Yes you are

>things that didn't happen

Yea I think so. Hopefully the teacher is alright

Why don't you think so?

C++ is horrible

oh it did fucking happen. and i had such good grades in the written tests too. im such a fucking retard

im gonna kys, it was nice knowing you /dpt/

Explain.

>im gonna kys
thanks user, ive really been needing it too lately

Are you sure C is the right language for this task?

Please explain.

>Assuming I'm not retarded
That's where you're wrong.

It compiles, but I don't see where any commas are missing.

Has to be C. Practicing to become an embedded developer.

>but I don't see where any commas are missing
Like in the data.

Little Schemer is neat. What interpreter are you using? Chicken? Racket?

C newbie here, I was just learning how to compile codes then I came across a few compile parameters.

1. Why won't people compile with -O3 at all times anyway? Does it have any trade off?
2. If I am making a Linux amd64 binary, what flags should I put for the best performance?
Is there something like --march-native -O3 or something?

O3 and Ofast are not recommended by many people anymore. Certain optimization bloat the implementation and reduce performance, stick to O2 or Os. And the gcc parameter for architecture tuning is "--march=native".

install gentoo

O3 takes more "risks" with optimizations which some of the time will actually make the program slower.

Thanks anons.

$gcc -Wall -O2 --march=native hello_world.c -o hello_world

from random import randint
arr = []
temp = []
r = randint(1, 18)
for m in range(9):
t = []
for n in range(2):
while r in temp:
r = randint(1, 18)
temp.append(r)
t.append(r)
arr.append(t)
print(arr)

gcc -Wall -Wextra -pedantic -std=c11 -g -O2 --march=native hello_world.c -o hello_world
at a minimum

>-std=c11 -g
What do those do again?

g is debugging info, std=c11 compiles for the latest standard.

import numpy

def randperm(a):
return list(numpy.random.permutation(numpy.array(a)))

> -g -O2
Wew

>1. Why won't people compile with -O3 at all times anyway? Does it have any trade off?
Yeah, it completely messes up the generated assembly code in an attempt to further optimize it; in some rare cases that can lead to the program misbehaving or straight up crashing. Also it's only marginally faster than -O2, which is considered safe.

You do realize that the debugging info goes into a separate section and wont interfere with performance. You also realize that there are real reasons to want the DWARF info of release versions,right?

>I'm too stupid to infer skips due to code optimization

>-pedantic
>-Wextra
Gets in the way 99% of the time. Don't need it.

>-g
Only if you're debugging

>--march=native
Don't forget about mtune

Why won't he explain?

>Gets in the way 99% of the time
Bet you don't even run ubsan and asan before release you fucking dirt merchant.

>Don't forget about mtune
march implies mtune

>Only if you're debugging

or profiling, or if you're making a library, or you want to do dynamic probing, or if it's going to be part of an OS, or...

>or if you're making a library
>or if it's going to be part of an OS
You don't need debugging info for any of those two

When you compile with -O2 there is no longer a 1-to-1 correspondence of source code to machine code, which makes the program very hard to debug. There is no point in adding -g if you're going to enable -O2.

Alright, I've finally made sense of your code, but now it's not compiling for some reason.
I'm using VS 2015 (which I know is shit), and it appears to be breaking on this line
for (last = strtok(str, ','); (curr = strtok(NULL, ',')) != NULL; last = curr)

If you don't want to be a dick it's nice to. Obviously it's not required by law.

I won't write software for an OS I can't get the symbols for. Fuck that noise.

Can someone explain why C++ is bad?

It's a pain in the ass to debug, specially if it reaches 10k lines of codes

Same can be said about C.

There's more reasons than just debugging to want them.

C++ standards are more versatile. Your fellow programmers will utilize every single one of them.

Wanna tell me the error?

And this is why programs break in the wild, because you aren't debugging the code you intend to release. You think you can just pop on O2 and put it out? Have you ever heard of undefined behavior?

> Programming with undefined behavior