/dpt/ - Daily Programming Thread

Previous thread: What are you working on, Jow Forums?

Attached: compiling_templates.jpg (1143x1500, 154K)

Other urls found in this thread:

github.com/victorqribeiro/radialMenu
github.com/LoLi-Lang/LoLi
stackoverflow.com/a/20932020
asciitable.com/
twitter.com/NSFWRedditVideo

github.com/victorqribeiro/radialMenu

I'm thinking about change the way I construct the menu from canvas to svg graphics. people are bugging me about it

Working on a mock OS in Javascript.
>WHY
I'm bored. See pic related, file explorer seems to work nicely so far.

Attached: GIF (1).gif (676x504, 65K)

Should I negotiate an internship offer?

no

Ganbaruyo, Anone! I'm feeling very positive about your success at your JavaScript studies!

Attached: c1c40feb1c8a7f46ef6d9583228fe6be.jpg (1000x1500, 180K)

yes

Which language should I write my anime girlfriend in?

I'm teaching myself K&R C but having a tough time understanding how functions work in C. I want to write my own program that counts the length of a string. I can do it in a few lines in one function, but I'm trying to split the work up into a main() and a stringlength(char string[]) which returns an integer. I want a function which accepts input from a user and writes it into a string, then have the stringlength() function iterate over the string's length and return its length.

Can I separate requesting input from the user and counting each time a character is typed? How do I take the typed into main and pass it to the stringlength() function? I feel like a retard.

Is there anything worse than having to share code with other people?
Especially when they don't even work there anymore so you can't even yell at them

Attached: 1563287054164.jpg (460x591, 41K)

github.com/LoLi-Lang/LoLi

not having anyone to share code with

Attached: 1565983774366.jpg (591x716, 41K)

Post the code you've already got. It makes it easier for us to tailor our explanation to your current skill level.

>what is strlen

>what is learning

Reinventing the wheel can be useful if you don't know how a wheel works.

yes you can separate input and counting.
in this case, save what the user typed and pass what the user typed to the counting function.
an example of a way to save what the user typed is to have a 'big' array (200 chars), then using fgets() to populate the array, then passing a pointer to the beginning of the array to your function, along with the length (that fgets() tells you)

#include

int stringlength(char string[]);

int main()
{
int c, i;
char s[1000];
while((c = getchar()) != EOF)
s[i] = c;
printf("Length is: %d\n", stringlength(s));
return 0;
}

int stringlength(char string[])
{
int i;
char ss[1000];
for ( i = 0; string[i] != '\0'; ++i)
++i;
return i;
}

stringlength doesn't need its own array. pass it a pointer, don't try to copy data into it.

the big problem with your main() is that you don't terminate the string with a NUL assignment, s[i+1] = '\0', so stringlength has nothing to find
also, you need to increment i or you're only ever reassigning the same byte.
also, you need to initialize i

Ok, so this "works" most of the time.

#include

int stringlength(char string[]);

int main()
{
int c;
int i = 0;
char s[1000];
while((c = getchar()) != EOF) {
s[i] = c;
++i;
}
s[i] = '\0';
printf("Length is: %d\n", stringlength(s));
return 0;
}

int stringlength(char string[])
{
int i;
char ss[1000];
for ( i = 0; string[i] != '\0'; ++i)
++i;
printf("\n");
return i;
}


I know that it's much easier to just move pointers around in C when you need to deal with arrays for lots of things, but I haven't gotten to pointers yet so I don't know how they work.

Sample outpout:
blk@blk-MacBookPro:~/programs/stringlength$ ./a.out
;shf349i189th139fjq


as;dfjasd


Length is: 48
blk@blk-MacBookPro:~/programs/stringlength$ ./a.out
1
Length is: 2
blk@blk-MacBookPro:~/programs/stringlength$ ./a.out

Length is: 0
blk@blk-MacBookPro:~/programs/stringlength$

>Want to get into Machine Learning
>Literally every udemy course for every framework is 10 hours of rambling about some stupid shit without any practical projects
Fucking YIKES

Attached: 4453453454.gif (162x226, 777K)

post code gore
stackoverflow.com/a/20932020

In the spirit of prompting not telling:

is this necessary:
char ss[1000];


for ( i = 0; string[i] != '\0'; ++i)
++i;

why is this wrong?
Hint: what does ++i do exactly?

Should this be:
s[i] = '\0';

or:
s[i + 1] = '\0';

I fucking love you for saying that. Anyway:

char ss[1000] is not necessary. I have initialized a character array for no reason here, I am iterating over char string[].

++ is the increment operator.
for (i = 0; string[i] != '\0'; ++i)
++i;


This tells the program that beginning at 0, as long as i is not the NULL character, increment i by one and then... I don't really know what the second ++i is doing there.

I tried s[i + 1] = '\0' and this added 1 to my string length.

#include

#define SIZE 1024

int string_length(char *string)
{
int i;
char *s;

for (i = 0, s = string; *s; ++s) {
++i;
}
return i;
}

int main()
{
char buffer[SIZE];

fgets(buffer, sizeof(buffer), stdin);

int length = string_length(buffer);
printf("string_length -> %d\n", length);

return 0;
}

Here's how I might write this.

Attached: rei shop.jpg (619x800, 118K)

turtle girl is cute

Attached: 1558596635079.png (618x686, 63K)

And it works! Thanks anons. I have also rewritten it to accept long unsigned integers for bigger strings. How inefficient is this code?

#include

long unsigned stringlength(char string[]);

int main()
{
int c;
int i = 0;
char s[1000];
while((c = getchar()) != EOF) {
s[i] = c;
++i;
}
s[i] = '\0';
printf("\nLength is: %lu\n", stringlength(s));
return 0;
}

long unsigned stringlength(char string[])
{
long unsigned i;
for ( i = 0; string[i] != '\0'; ++i);
return i;
}


blk@blk-MacBookPro:~/programs/stringlength$ ./a.out

Length is: 0
blk@blk-MacBookPro:~/programs/stringlength$ ./a.out
1
Length is: 1
blk@blk-MacBookPro:~/programs/stringlength$ ./a.out
11
Length is: 2
blk@blk-MacBookPro:~/programs/stringlength$ ./a.out
aoihef;aklsdjfasdj943f
Length is: 22
blk@blk-MacBookPro:~/programs/stringlength$


Thanks user. I haven't gotten to pointers so it's taking me a while to understand this.

Alright this sucked.
I tested flatbuffers against serde-json and my own dumb JSON serializer. It's slower to serialize than both of them, produces bigger output, and it's painful to construct it correctly (I don't think I got the hang of it yet, since their provided examples aren't thorough and the rust library is very inconsistent with errors).
I guess I'll try capnproto or something.

>Machine Learning
>practical
just look up a library/framework tutorial if that's all you're interested in

>I have also rewritten it to accept long unsigned integers for bigger strings
As the array s is only 1000 elements and an unsigned long can hold a number 2^32 (~4 billion) it's a bit overkill.

Looks fine user. If you want to know how efficient it is. I suggest you learn to read assembly. Then you can use either gcc -S or godbolt.org to see the compiler output in assembler language.

stringlength is pretty much perfect though most would probably write
char *string

instead of
char string[]

though in this case there is no difference. Strictly speaking it's just preference. It's also about as efficient as you can.

You could make stringlength even tidier.

int length(char *string)
{
int i;
for (i = 0; string[i]; ++i);
return i;
}

this
machine learning is probably the most pragmatic area of programming right now. there's a lot of nice theory, but if you want to actually do machine learning it's mostly experimentation and shoveling coal into the fire
you don't need an in depth tutorial to use a python library

ok brainlet

This won't work because the for loop doesn't know to stop when it gets to a NULL character, right?

what do you think that string[i]; is doing in the for loop?
>NULL character
NUL, three letters. 'man ascii' for similar.

Pragmatic is a fancy way of saying "throw shit at the wall"

Attached: 1564451488717.jpg (451x393, 134K)

The test condition just checks for 0 or non-zero (false or true), so the check for 0 is implicit.

This is a question that's eluded me for a while for making games. When you code the game itself, how do you know when to break up the code? Like, does certain aspects work better in separate files? Could you possibly lump all the code in one file but separate it inside the file itself?

I understand this might be a really stupid question, moreso than normal. I feel like it's an abstract concept that one I 'get it' it'll make sense, whatever the answer is.

>machine learning it's mostly experimentation and shoveling coal into the fire
this is the reason the industry is infested with shitty "data scientists" who don't know anything

So a Null character in ascii is equal to the numerical value 0.
In c, any non zero number is true. Boolean checks work by returning either 1 or 0 if the expression is true or false.
So:
string[i] != '\0'

will be 1 (which is true) when the condition holds. The other user is pointing out you can just check the character directly since a null is equivalent to 0 which is false.

Sentdex

Well I removed != '\0' and it still works. How, though? Isn't the condition being tested that a string exists and doesn't end in NUL? That's how the loop knows when to terminate, right? I guess not.

It's mostly intuition. Also depends on what language you're using to a small extent, e.g. Java only lets you put one public class in a file.

Oh, so this is a bit logic specific to C and other languages which treat NUL the same as 0?

Normally by domain, so UI functions and AI functions would go in separate files. There's lots of cross domain stuff in a game though, for example loading a configuration file and a texture might share the same generic loading function. Initially I would just write everything in 1 file and see what structure emerges.

Attached: 1566312077808.jpg (563x675, 90K)

It's mostly just a readability issues I think.

#include

int main (void) {
char greet[] = {'h', 101, 0x6C, '\x6C', 0157, 0};
puts(greet);
return 0;
}

Using a game engine is such a different experience than developing software. I figured the skills would be interchangeable, but they really aren't. Would it be weird to specialize in game programming and not really give a shit about software development, or am I obligated to learn how to make applications in order to call myself a programmer? I just want to make games.

Technically null isn't the same as numerical zero as far as the type system is concerned, but most of the time you can assume they are the same.

Kind of. This wont work in every language because not every language allows you to turn a character into a boolean value implicitly (without writing code to tell it to do so).
However, ascii (and utf-8, and all text encoding) is independent of C. So internally every language will represent a null in ascii as a 0. You can see a full table here:
asciitable.com/

Break up the files by their functionality. For example in game you could have one file for each enemy type, assuming their behaviors are different.
Managing the game window is one standalone functionality so you would have a window file (and probably class). GUI has many different parts (buttons, text, list, etc) so each could be their own file.

So really to make it not a hellscape to read and have a million long line of code to sift through in one file. It definitely makes sense to separate certain functions. Thanks for the explanation. It helps me 'see' how things should be set up.

pay attention to pain points as you develop, and address them.
>man it's really annoying to slog through this function
>should I split it up then?
a trouble with geniuses is that they ignore their pain and therefore produce work that other people can't suffer through (or they get crippling RSI due to ignoring their pain, like RMS).

What should I read if I want to learn more about how to make a big interconnected system like Dwarf Fortress? Somehow the programming of the simulation allows new systems to interact with existing systems in interesting and unexpected ways. There's so much depth to how characters perceive things, how they react, and how they can even work cooperatively. It's truly amazing and I want to learn more about how to make it. Can anyone point me in the right direction?

Attached: df.jpg (780x520, 133K)

Is there a (simple) tutorial out there that shows how to link files together? I think that's what's making me feel it's hard to make things separate. I'm just brainstorming some ideas and figuring my novice skill level into the mix. Shits fascinating making a codepiece and seeing it work.

Thanks, this all sounds like something that will come naturally as I gain more skill. As long as I apply it in practice.

I'd start by writing the simplest simulation you can think of and making it more complex. Conaway's game of life is a good start.

No, the null character is always 0, just like a null pointer is always 0. Technically you could make a character encoding that used a different code point for null, but no one does that because 0 being null is standard, and everyone uses ascii or Unicode anyways, which both specify 0 to be null.

>So really to make it not a hellscape to read and have a million long line of code to sift through in one file.
Most independent games are around ~30,000 lines. My simple 3D game engine which is mostly feature complete is only 4,000 lines.

Quake 3 has 300,000 LOC but that's building on prior iterations of the same program. Only AAA shitters like Ubisoft are going to have games with millions of lines of bloat.

Attached: 1566619996100.jpg (564x752, 46K)

click the arrow by the ID this post and then 'report post'
then click it again and hide post
THE FBI IS COMING FOR YOU
CIRNO ABUSER

The logic part and the treating of every nonzero integer as true is specific to some languages though. Not every language will do that, in which case you just have to do if (c != 0) instead of if (c)

there's a difference between some guy wanting to do practical projects and a 150k/year data scientist
i spent quite a few hours reading up on neural nets, matrices, calculus etc. but the practical application was where the real learning happened

a gross exaggeration but still a lot of lines to dig through.

yes, its some gay typecasting stupidity that should be relegated to garblangs like js

Attached: dankmeme.png (638x479, 110K)

#include

void putmeme (char *s) {
char *t = s;
while (*t != 42) t++;
fwrite(s, t - s, 1, stdout);
fwrite("\n", 1, 1, stdout);
}

int main (void) {
char greet[] = {'h', 101, 0x6C, '\x6C', 0157, 42};
putmeme(greet);
return 0;
}
asterisk-terminated strings are possible too.

>95% of the integration test runtime is taken up printing millions of lines to the console
>can't even redirect it because actual the error messages use the same output stream
>denied permission to fix it because it's non-essential

Attached: 0.jpg (1200x666, 95K)

Actually there's no typecasting being done. An if statement is defined to take integers and treat 0 as true and any non zero as false, because this is how the cpu works. The 'bool' type in C is merely another integer type

>An if statement is defined to take integers and treat 0 as true and any non zero as false, because this is how the cpu works

Attached: 1559596981278.png (1000x1000, 18K)

>Is there a (simple) tutorial out there that shows how to link files together?

$ ls src
allocate.c entity.c main.c notify.c ui.c
build.c globals.c math.c opengl.c
control.c load.c mesh.c types.c
$ cat src/build.c
#include "globals.c"
#include "allocate.c"
#include "load.c"
#include "math.c"
#include "opengl.c"
#include "mesh.c"
#include "entity.c"
#include "control.c"
#include "main.c"

In the biz we call this a `unity build' and it is used solely by pros.

Attached: frogs riding tandem.png (500x573, 450K)

C didn't originally have a boolean type

Why the whole stm32 ecosystem is such a shitshow? Isn't non-free closed source software written by corporations supposed to just werk?

what

That's not how conditional jumps are run by the CPU

>this is how the cpu works

It's fine, don't worry about it

Attached: truth.jpg (390x430, 105K)

>me being retarded.

I suppose more how to..use those files. I guess it's the same thing but..how does the code know to pull from an outside source I guess is what I'm confused on.

you're not retarded on. that's bad advice. it's as if you asked for a guide on how to treat your girlfriend on a date and were told to grab her by the throat and drag her into an alley and rape her.

O-oh. I see...

I really need to just read some books on coding essentials again. It's been so long I remember the cover and that's it.

I'm writing a small interface for accessing my animu collection as I couldn't find anything that did what I wantd.

I like learning, can you tell us more? Sounds neat.

Attached: 1.png (114x162, 26K)

Yes, you jump of the register contains 0 (technically if the flag that says that register contains 0 is set)

Or you copy the bit into the carry flag and jump if it's one or zero.

And it's the other way around, 0 is false, anything else is true.

or if it's negative, or if the last computation overflowed, or if the last computation carried... don't be fucking retarded, there are more status bits than Z on CPUs that aren't 8 bit

Does Rust have a verified compiler yet?
Or is it still useless for safety critical software.

Please explain what's happening here.

Rust is still inappropriate for safety critical.
Use a language actually intended for that purpose.

I would prefer to follow some sort of initial exploration of the ideas

And if you just pass an integer and the compiler can't figure out any optimizations from context, the it just checks if the register contains 0 :^)

I installed loli-lang by user.

Attached: 1566307977491.gif (360x360, 1.96M)

Yes 1 is true for those flags, but a 0 in the register is what sets the flag to 1, so 0 is treated as true for numbers in the register, and everything I said is 100% correct

He takes a copy of the pointer and scans it along until he hits 42 (* in ascii). He then writes to the screen the number of bytes between the beginning and where 42 was found starting at s, effectively using 42 as a terminator. The string is just "hello*" written in various bases.

Also, flags are not necessarily related to the contents of a register. Think about arithmetic on a memory location or instructions like TEST/CMP.

point is that zero/nonzero isn't false/true because it represents the CPU, it doesn't. it's just simpler that way. See also: lisps where nil is false.

how come user? toady one didnt follow a guide

There's no reason for C to use zero = false and non-zero = true instead of proper boolean types of various widths (when they need to be spilled to registers or memory).

Doesn't really change anything, and in my original post I didn't even mention registers to keep it general but people for some reason took issue with my post that is 100% correct. Someone said that C is doing retarded typecasting there, when that is 100% false. The if statement represents those conditional jumps that test if an INTEGER is 0, negative, etc. and if it the compiler can't figure out an optimization it defaults to testing for 0.

>of various widths
why?

That's brilliant and annoying at the same time.