/dpt/ - Daily Programming Thread

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

Attached: 72491702_p0.jpg (2185x1553, 1.45M)

Other urls found in this thread:

pastebin.com/NytqedRi
github.com/nothings/stb/blob/master/stb_image.h
benchmarksgame-team.pages.debian.net/benchmarksgame/faster/go-csharpcore.html
twitter.com/AnonBabble

nth for Nim!

how's my kode bros?

Attached: Untitled.png (1176x948, 43K)

C# is a pretty good language I'd say. I like it more than Java.

Project ideas?

Hey Jow Forums beginner program here. i really need help with my final project, im so lost when it comes to c++, can anyone help me learn write it / have me pay them to write it? its my final and im borderline in the class

Attached: 4chan help me.png (1669x852, 92K)

hey how do i fix this

Attached: Untitled.png (586x310, 11K)

Some changes I'd make. Your's is ok just doesn't seem 'pythonic'

#!/usr/bin/env python
alpha = 'abcdefghijklmnopqrstuvwxyz'
cipher_dict = {}
for i in range(len(alpha)):
cipher_dict.update({alpha[i]: alpha[-i]})

def encodeWord(word):
cipher = ''
for letter in word:
if letter in cipher_dict:
cipher = "%s%s" % (cipher, cipher_dict[letter])
else:
cipher = "%s%s" % (cipher, letter)
return ''.join(cipher)


if __name__ == "__main__":
with open('input.txt', 'r') as in_f:
with open('output.txt', 'w') as f:
s = in_f.read()
cipher_text = encodeWord(s)
print('Original: %s\nEncrypted: %s' % (s, cipher_text))
f.write(cipher_text)

you forgot the apostrophe after .txt .... .txt', 'w')

what would this do if I sent an image as the argument?
#include "infile.h"


int main(int argc, char *argv[MAX]){

infile file1;

file1.input(&argv[MAX]);

return 0;
}

#include
#include

const int MAX = 1000;

class infile{

public:
infile(){ }
void input(char *argv[MAX]);// function which will take in the image data(or whichever data you're inputing)

};

#include "infile.h"

infile::infile(){ }

void infile::input(char *argv[MAX]){

std::cout

Depends how good you are and what you're actually interested in.

#!/usr/bin/env python
alpha = 'abcdefghijklmnopqrstuvwxyz'
upper_alpha = alpha.upper()
cipher_dict = {}
for i in range(len(alpha)):
cipher_dict.update({alpha[i]: alpha[-i], upper_alpha[i]: upper_alpha[-1]})


def encodeWord(word):
return ''.join([cipher_dict.get(c, c) for c in word])


if __name__ == "__main__":
with open('input.txt', 'r') as in_f:
with open('output.txt', 'w') as f:
s = in_f.read()
cipher_text = encodeWord(s)
print('Original: %s\nEncrypted: %s' % (s, cipher_text))
f.write(cipher_text)

How would you guys represent classes in C?
Also how would class inheritance work?

import caesar_cipher

ceaser_cipher.run()
Get on my level.

Hey girls, I have more of a general CS question.
How can I effectively explore all of the major CS fields to figure out what I find most interesting? Do I read a book on each topic, work on a project for each topic, or is there a more effective way?
For example, embedded systems, reverse engineering and TCS "sound" interesting, but I have no idea what each of them actually entails.

This is a very basic C question.
How do I properly malloc for a struct?
is malloc(sizeof(struct foo)); good enough? or do I have to add the sizes of all of the members

For reverse engineering you can try getting into the hackthebox.eu or overthewire.net communities. There are also a lot of pwn sites with good challenges and wargames.

For embedded systems try buying a PIC or a arduino or a raspi. They are cheap enough and you can blink an LED and see if that excites you.

TCS I am not familiar with, but I find that the best way to learn about something is to "just do it" and struggle and read until things start to make sense. If you are interested in it you will stick with it, in my opinion.

>classes
literally the same thing as structs
methods are just syntax sugar for functions that take "myStruct* this" as the first argument

>inheritance
the exact same fukken way it's implemented in C++, only you wouldn't have syntax sugar handling it for you:
first element in the derived struct is an instance of the base struct. since they both start at the same position in memory, you can simply cast `inherited*` to `derived*` to access the base class and use it with base-class functions.

Virtual functions are handled by creating a static table of function pointers, one per class, and giving each struct a pointer to the vtable of its appropriate type.

Then to call a virtual function I do object->vtable.functionname(&object, otherargs);

Just use structs, you don't need "classes" most of the time.
Occasionally you can do some faux-polymorphism shit with embedded structs, but you don't need it most of the time.
#include

struct base {
int a;

void (*func)(struct base *b);
};

struct fuck {
struct base base;

int b;
};

struct shit {
struct base base;

float c;
};

void base_func(struct base *b)
{
b->func(b);
}

void fuck_func(struct base *b)
{
/*
* You could also do some offsetof schenanigans
* to make it so base doesn't have to be the first element
*/
struct fuck *f = (struct fuck *)b;

printf("%d\n", f->b);
}

void shit_func(struct base *b)
{
struct shit *s = (struct shit *)b;

printf("%f\n", s->c);
}

int main()
{
struct fuck f = {
.base = { .func = fuck_func },
.b = 10,
};
struct shit s = {
.base = { .func = shit_func },
.c = 0.5f,
};

base_func(&f.base);
base_func(&s.base);
}

Do people who argue about languages all the time ever actually program?

By creating a new language with the features we need. We could give it a fun name too like C but a little more...C++ sounds good.

adding the sizes of all the members is bad and will produce wrong results, since the compiler may add padding between struct members to preserve alignment of the values in memory.

The way you did it is alright, but the best style is to simply do
struct foo * myVar = malloc(sizeof(*myVar));

instead of repeating the type.

I can't think of anything to make :(

Yeah, that's ok.
In fact, allocating the sum of every memeber instead is wrong in general: the struct's actual size may be larger due to alignment.

Yes, sizeof(struct foo) does include all of the size of the members. The only special case are structs with flexible array members, but that's something different.
I prefer to use sizeof variable though.
struct foo *foo = malloc(sizeof *foo);

Ok that was very informative
Thank you

Make something like a Chip-8 emulator, using whatever the fuck you want.
Yes, there are a million out there already, but making one yourself is a good way to "break the ice".

Make a diary program where you can log how pathetic your day to day activities are.

Attached: 1481416004839.png (581x383, 537K)

How would you malloc for a struct with a flexible array member?
malloc(sizeof(struct foo) + sizeof(char)*10)
then just reallocate or is there a better way?

I already do that but I write it on paper.

>a struct with a flexible array member?
what the fuck?
is this some C99 bullshit that I haven't read about before

I did not know you can just cast a struct to another struct.
thanks

That could be fun. Or maybe I should make a program that generates ideas for programs to make then I can use that to make more programs.

Wouldn't the array member just be a pointer anyways? It would have to be allocated dynamically unless the size was static.

no one? I'm trying to get the image data loaded into an object, am I on the right path here?

you can cast any pointer to any other pointer, though beware of blowing up your computer if you do any of the low-level C fuckery wrong

You would pass the filename as a string probably and then you would need to use FILE structure and functions to open the file and read the image data, or have an image library do that.

Why don't you try python and PIL?

Yes, that's how you do it.

It's casting a pointer. Honestly, it's sketchy as fuck based on how the language is defined, but it's used in a lot of "real" projects (including the linux kernel), so compilers do what you think it will.

Wouldn't the array member just be a pointer anyways?
no

>You would pass the filename as a string probably
I thought doing it this way would be cooler
>Why don't you try python and PIL
I want practice using c++

Flexible array members came about because of the "struct hack" by using a zero-sized array, and they just decided to make it standard with a slightly more consistent syntax.
struct s {
size_t n;
// int arr[0]; // Old syntax
int arr[]; // New syntax
};
It is not a single pointer. It truly is a single allocation and is more efficient.

anyone working with crypto technology? Ethereum etc

crypto means cryptography retard
the cryptocurrency world barely cares about technology it just wants hype so it can gamble money on investment bubbles

std::string a = "very long string";
std::string b = std::move(a);


does b take ownership of a's heap allocated data and invalidate a?

this can call a's destructor btw, don't let that trip you up

Anyone here going to railsconf?

(last thread)
This was harder to do than you would think... pastebin.com/NytqedRi

Use 2 int[26] arrays and tally up how many of each letter is in word or input. A match is when (dict[i] >= search[i]) for i=0..26-1; Don't forget to zero out those arrays before counting letters. A huge switch() statement should be adequate for identifying chars and what index to use...

That code makes no sense. Don't name a variable "argv" unless used in a way similar to how the normal argv is used.

Use this. github.com/nothings/stb/blob/master/stb_image.h

If the struct's last element is type T[], (i.e. struct { int a; float c; char c[0]; } then you do ptr = malloc(sizeof(struct_t) + N*sizeof(T)); .

I am confused. When getting integers from a JTextfield you have to parse them.I have done this before but I'm getting problems now

Attached: segsaergsegse.png (553x370, 14K)

Dull minds are easily bored. Know thyself.

Remi and Flan are such good girls

Youre declaring a variable and initializing it by calling a method on a variable with the same name.

Why do some libraries put everything into one gigantic, 10 kloc header? Why not just install a few dozen headers?

What is /dpt/'s opinion on this function declaration style for C?
int
function_name(int arg1, int arg2, int arg3)
It looks silly when it's just an int like that, but works out a lot more nicely for longer names and you're trying to keep to an 80-100 column limit.
static inline struct my_long_struct_name *
my_long_function_name(struct another_long_struct_name *argument)

I've been thinking about applying this style to my project.

Attached: 1540160462027.jpg (678x1512, 93K)

It doesn't really make a lot of difference at the end of the day.

Trying to fucking make a fucking C rogue-like on Windows but I can't fucking use fucking curses for fucking windows so I have to use fucking something called pdcurses that I don't know how to fucking build or fucking specify the fucking necessary things I need to fucking specify for a cmake fucking file nigger

BSD kernel normal form (KNF) says the newline is required.
That's because grep ^funcname -r * will always find the actual implementation.

I like it, but I haven't actually used it since I hardly write C anymore. Being able to grep ^function_name is really useful.

It's actually known as the "struct hack" in C89, but was formalized in C99 because it was actually illegal C89.

i don't know enough about programming to find what i'm looking for.
how do you get vim to autocomplete code like an IDE? specifically for c#, python R, and latex if possible. I've tried YouCompleteMe and VimCompletesMe but they don't have boxes appear with a list of autocomplete suggestions and they also cause random text to show up in spots. i'm most accustomed to how RStudio handles R code.

Attached: 68747470733a2f2f636c6f75642e67697468756275736572636f6e74656e742e636f6d2f6173736574732f34323738313133 (769x329, 44K)

That's a pretty good point. I'm going to use it.
Now the question for whether I should align split argument lists with the ( or not.

Just to "C" if you could?

this desu

>nimfaggotry in my /dpt/
i'll desu you, desu

There is literally nothing wrong with C. 99% of the instructions your processor processes are ultimately compiled in a C compiler, 99% of your operating system's back end is C, the vast majority of non-FPGA embedded systems only excecute C

I LOVE C

Agreed. Now build a quick web server that parses user input and puts the input into a database. It needs to handle UTF-8 correctly.

toehoes a cute

I am working on writing a python scraper script to rip porn videos from pornhub and use ffmpeg to cut them in half and post them to r a p e d a t D 0 T c0m

libhttp
mysql c api
sheredom/utf8.h for the parser but libhttp obviously has no problem with UTF-8

I'd rather write use 3 libraries and write 200 lines of code than use 200 libraries and write 3 lines of code

>I'd rather write [...] 200 lines of code than [...] write 3 lines of code
Not saying it's wrong just sounds exhausting if you apply this to everything that you do.

Attached: 1491955854885.jpg (604x516, 126K)

>forced to use perl5 for my latest job
I find it aesthetically distasteful, but it gets the job done I guess

perl is great once you can start to read it. Aesthetically it is like a hairless dog. It is comfy in a really ugly way.

perl is very performant for a scripting languge.

Being marginally faster than python, one of the slowest mainstream languages in existence, does not mean you are "performant".

Being a faggot doesn't mean you are barred from posting here.

Perl5 gets things done.

Attached: 1484893948181.jpg (566x581, 76K)

Can someone help me and find me a modern tutorial/course on how to implement C/C++/Rust/Go Modules in That slow as shit Python to boost it up? Because for fuck sake i cant find anything remotely readable and if it its from like 2014

Attached: 645674634.jpg (437x501, 42K)

Ditch that memesnek crap; just write your program in C to begin with. Trying to FFI shit just makes it way more complicated.

When you program in Haskell but are a traitor to your cause

Attached: Screenshot at 2019-04-26 09-33-15.png (920x406, 60K)

Looks like shit and doesn't work if the function you're declaring returns a pointer to a function. You may as well use int* ptr; as a global style.

tru

>doesn't work if the function you're declaring returns a pointer to a function
That already looks bizarre enough normally.
int (*my_fn(int arg1, int arg2))(int, int, int)
It's also a very rare thing to do, and even when it is done, there is almost always a typedef involved.
>int* ptr;
>Left
Absolutely disgusting.

>Dude just typedef everything lmao
C syntax is so disgusting even Cniles can't tolerate it.

> ;; If non-nil, the paste transient-state is enabled. While enabled, after you
;; paste something, pressing `C-j' and `C-k' several times cycles through the
;; elements in the `kill-ring'. (default nil)
dotspacemacs-enable-paste-transient-state 't

oh my god I love spacemacs

Any language with a recursive type system is capable of generating some fucking awful looking garbage.
C's declaration syntax is very consistent, but that didn't make function pointers look very good.

Internal syntaxtic consistency isn't the issue, but this style is inconsistent with the syntax.

>this style is inconsistent with the syntax
Why? The actual return type part isn't part of usage anyway.
When you're calling a function that returns an int, you don't write like
#include
int main()
{
int printf("Hello, world!\n");
return 0;
}

Any loop that can be written recursively can be written iteratively and should be.

Attached: cat.png (123x115, 41K)

Because the syntax isn't type name;. It's specifier declarator;.
Splitting a function declaration across two lines is incorrect for the same reason that putting the asterisk next to the type specifier is incorrect. Because that's not what the syntax is.

Nobody actually gives a shit about the formal grammar when it comes to formatting code, idiot.
Also, you don't even seem to be running off a grammar that is up-to-date.

I'll accept that if I was treating EVERY declaration the same,
int
my_fn(int arg);
is inconsistent, and I should write variables like
int
my_var = 10;
but people don't care about pure consistency to the same autistic level that you do.

Functions are special in C anyway; lots of styles treat them specially.

>Rust
Google pyo3

Can we got a Go without a shitty implemented Garbage Collection pretty please? God fucking damn it just thinking about a modern (not bloated) C makes me horny but they had to fuck it up

Attached: 1542664656214.png (450x489, 74K)

search for omnisharp-vim

C# can compile to native binaries along its GC

>that's not what the syntax is
both of those things are literally perfectly valid C syntax, anonymous
the compiler understands them just the the same way as it would if they were in your preferred style, and emits the same bytecode for either

>What are you working on, Jow Forums?
Creating more Electrical Engineering free and open source software in Linux(and then in BSD). As an EE, I find it really horrible how I'm mostly forced to use Windows instead of Linux to do my work and that is shit. So I decide to make my own softwares both for myself and to give people new options. I know there are some free EE softwares on Linux but not all of them are open source and, imo, that goes against the reaason for using Linux, simple as that. Besides, I hate the fact the good EE softwares are expensive as hell. Thus, I'm going to spend a lot of time doing these things, thankfully that is going to help me get better at programming, increasing my portfolio and also to have a way better understanding of EE concepts.

>Any language with a recursive type system is capable of generating some fucking awful looking garbage.

It would be much clearer, however, if things just read left to right. Putting the function's return type on the left of the function name was an awkward design decision

>shitty implemented Garbage Collection

Attached: 1451033022914.jpg (1197x627, 160K)

This.
>Sane languages
() -> ()
>C retardation
void(*)(void)

Literally no one would ever type that. It looks like a vagina

Well, people didn't think like that 40 years ago. It's hardly worth complaining about it too much now.
It's far too late to change it.

There are far worse mistakes in C that can't be changed, as much as people would want it to. That being the extremely low precedence of the bitwise and/or (&, | ) operators.

Can someone explain this to a brainlet

benchmarksgame-team.pages.debian.net/benchmarksgame/faster/go-csharpcore.html

Wasnt Go supposed to be fast as fuck? Then why is it getting trashed by a Java knockoff? Was C# always this fast or am i missing something.

Attached: 131342535.jpg (1106x1012, 70K)

(λ)