/dpt/ - Daily Programming Thread

old thread: What are you working on, Sup Forums?

Other urls found in this thread:

programiz.com/c-programming/examples/prime-number
nerderati.com/2018/03/09/github-is-forcing-me-to-change-my-username/
twitter.com/SFWRedditGifs

Ordered and linear types will save systems programming

Ok fine, come minimalize your cawk in my butt.

pls no bully,i'm just screwing around

#include
#include
#include
#include
#include
void noise(void *v, Uint8 *stream8, int len) {
Uint16 *stream = (Uint16 *) stream8;
len >>= 1;
while (len > 0) stream[--len] = rand();
}
void squarewave(void *v, Uint8 *stream8, int len) {
static int n = 0, m = 180;
static Uint16 p = 0;
Uint16 *stream = (Uint16 *) stream8;
len >>= 1;
for (int i = 0; i < len; ++i) {
stream[i] = p;
++n;
if (n >= m) {
p ^= 0xffff;
n = 0;
}
}
}
void sinewave(void *v, Uint8 *stream8, int len) {
static int deg = 0, m = 180;
Uint16 *stream = (Uint16 *) stream8;
len >>= 1;
for (int i = 0; i < len; ++i) {
stream[i] = (sin(deg*M_PI/m) + 1)*65535/2;
if (++deg >= 2*m) deg = 0;
}
}
void sawtooth(void *v, Uint8 *stream8, int len) {
static int n = 0, m = 180;
Uint16 *stream = (Uint16 *) stream8;
len >>= 1;
for (int i = 0; i < len; ++i) {
stream[i] = n*65535/(2*m);
if (++n >= 2*m) n = 0;
}
}
void triwave(void *v, Uint8 *stream8, int len) {
static int n = 0, m = 180, up = 1;
Uint16 *stream = (Uint16 *) stream8;
len >>= 1;
for (int i = 0; i < len; ++i) {
stream[i] = n*65535/m;
if (up) {
if (++n >= m) up = 0;
} else {
if (--n

>What are you working on, Sup Forums?
I'm trying to draw a triangle using Vulkan.
I'm already 500 lines deep.

that sounds much less pleasurable for both of us than it could be
unless small flaccid dicks are like your fetish or something

>Uint16
Pointlessly non-standard, although it's not your fault. I hate it when libraries do this.
>(Uint16 *) stream8
Breaks the strict aliasing rule, and is undefined behaviour.

i want to fug hakase

Rate my FizzBuzz without modulo or loops
let fz = { a: { a: { a: { }, b: "Fizz" },b: "" },b: "" } ; fz . a . a . a = fz;
let bz = {a:{a:{a:{a:{a:{ },b:"Buzz"},b:""},b:""},b:""},b:""}; bz.a.a.a.a.a=bz;
function fb(v,i){let z=fz.b+bz.b?fz.b+bz.b:i+1;fz = fz.a; bz = bz.a; return z};
Array.from(new Array(100), fb).join(" "); // Outputs "FizzBuzz" from 1 to 100 !

nice picture

>>Uint16
>Pointlessly non-standard, although it's not your fault. I hate it when libraries do this.
ugh i know right
i figured no point in #include if i already have to deal with this nonstandard bs, since using both will just get me confused
>>(Uint16 *) stream8
>Breaks the strict aliasing rule, and is undefined behaviour.
yeah but i feel like i should be able to trust the 8-bit buffer is allocated at a 16-bit-aligned position, since i did after all specify in the call to SDL_OpenAudioDevice that i wanted 16-bit audio
now you've got me curious, i'll have to look into that
idk wtf to do about it either way though, i mean doing it byte by byte rather than 16-bit word by 16-bit word would be really hard and dumb since i'm using 16-bit samples

>yeah but i feel like i should be able to trust the 8-bit buffer is allocated at a 16-bit-aligned position,
wait no, i was confused about what strict aliasing was

i should still be okay, if it's expecting to get 16-bit samples out of the buffer and i'm putting 16-bit samples in the buffer then in any implementation i can think of it shouldn't matter that the buffer itself is not of a type compatible with 16-bit samples, because it's being misused the same way on both sides
i think ???

>without modulo or loops

why not use recursion?

what the fuck /10

n--no !

He is. It's a recursive structure.

Can someone help me with my C homework?
Please... I'm such a noob and I don't know what am I doing bad... I'm supposed to list the given amount of prime numbers but I get pic related

#include
int main(){
int num, printed=0, cicle;
printf("How many prime numbers?: \n");
scanf("%d",&num);
do{
for(cicle=2;cicle

>why not use recursion?
he is using recursion
not functional recursion to be fair which is what recursion usually means
but technically still recursion

i want to nakadashi hakase while she ikus

It wouldn't be flaccid after the succ

In your printf
Replace cicle with num.
That's probably what you wanted.

Oh wait nvm I misunderstood.
Write a function that figures out if a number is a prime number or not first. Then try the exercise again.

thanks for the help kind user, I think I'll just kms

Here's the problem: you're getting all your variables mixed up and confused.
First of all, rather than testing prime numbers sequentially until you've found the number "num" of prime numbers requested, you have it set up so that you only test ONE number to see whether it's prime, specifically "num" -- again, the number of prime numbers requested.
Independent of that error, there's also a problem in the logic you then use to test whether "num" is prime (which, again, is not what you should be testing for primeness in the first place, but we're pushing that issue aside for the time being).
The trouble is that although you're iterating over every integer "cicle" (did you perhaps mean "cycle"?) from 2 to the number "num" you're testing, and determining for each whether "num" is divisible by "cicle", you're not using this information to decide whether "num" is prime, which is what would make sense; rather, you're erroneously using it to determine whether "cicle" is prime, a question completely unrelated to the procedure you're using to answer it.
Also, even if you tried to fix this by simply replacing "cicle" with "num" in the call to printf, you would still be faced with the problem that you're doing this for every integer from 2 to "num" by which "num" is not divisible, rather than FIRST making sure that "num" is not divisible by ANY such number and ONLY THEN reporting it as prime.

So, to recapitulate, you have two problems.
1) Instead of testing integers from 2 onward for primeness and stopping when you've reported "num" of them, you're testing "num" and "num" alone for primeness.
2) Your logic for testing primeness is mostly correct, but A) reports the wrong variable as prime, and B) reports this information prematurely.

Need help here.

Is there a way to update a SQL table, adding the information of a .txt file ( each column delimited by a | ) into the SQL table?
There's about 18 columns and more than 2000 rows.

Language: VB.net (Visual studio 2012)

...

if you find my explanation distasteful or inaccurate i'd appreciate constructive criticism
up to you tho

fun fact: the kinds of people who care about linear types are the same as those who have the extra time to post messages at the top of every /dpt/

>fun fact: the person who cares about linear types is the same as the one who has the extra time to post messages at the top of every /dpt/
ftfy

Oh come on.
It's a very explicit post detailing how you're probably thinking wrong according to this gal.
The code you posted is just very far from a solution so it becomes a lot of stuff.
I suggested In my opinion it's easier to deconstruct the problem and try again.
As you know your task is to find a number of primes. So you need to first have a piece of code that tells you if a given number is a prime or not.
programiz.com/c-programming/examples/prime-number
As an example.
When you have that you can focus on the rest of the task.

It wasn't distasteful or inaccurate at all, I really appreciate it user, what I ment with my pic is that I realized I'm beyond the point where I can be helped.
Sorry if you thought I was being rude, also english is not my first language (cicle lmao)

Which one is better Xming or MobaXTerm?
What is the advantage / disadvantage between those 2?

Oh, I see now.
Come on user, don't give up yet! Ganbatte, I know understanding even simple code can be hard and mystifying when you're coming into the field for the first time but I know you'll get it if you keep trying!

static void update_log(time_t *time, double *temperature)
{
int len = sprintf(line_buffer, "%lu %lf\n", *time, *temperature);
log(line_buffer, sizeof(char), len);
}

static void cb_exit(int sig)
{
keep_running = 0;

time_t now = time(NULL);
double zero = 0.0;
update_log(&time, &zero); /* error here */
}

>warning: passing argument 1 of ‘update_log’ from incompatible pointer type
>expected ‘time_t * {aka long int *}’ but argument is of type ‘time_t (*)(time_t *) {aka long int (*)(long int *)}’

Please help me I'm retarded.

>&time
nvm

You're passing the time function itself to update_log instead of the variable "now" which you obtained from it

>passing single double by reference to a log function
Oh please user stop this madness.

your time variable is called 'now', not 'time' brainlet

nowadays i pretty much throw away anything that takes me more than a day or two to write.

if it's any bigger than that i dont trust it, too complex.

oh ok cool nvm also then

nerderati.com/2018/03/09/github-is-forcing-me-to-change-my-username/

kek

javascript:quote('65041426');
I fixed it.

Use SDL2

C++. Are non-virtual methods more costly than free functions?

>github is that incompetent
not surprising, desu.

main[1

Not compared to free functions that do the same thing, or at least not significantly — they'll both have to pass something like a "this" reference to the data being worked on, just in the class method it's implicit/automatic.

non-virtual methods is basically free function that carry a this pointer
The following code will result in identical operation, the only difference is only on how the data passed on.
Member method:
class Foo{
void bar();
};
int main(){
Foo foo;
foo.bar();
}

Static member method:
class Foo{
static void bar(Foo *);
};
int main(){
Foo foo;
Foo::bar(&foo);
}

Free function:
class Foo{
};
void bar(Foo*);
int main(){
Foo foo;
Foo::bar(&foo);
}

Fuck the free function should be just
class Foo{
};
void bar(Foo*);
int main(){
Foo foo;
bar(&foo);
}

main[-1u]={1};

>What are you working on, Sup Forums?
Nothing and it feels bad. Anyone have the old Sup Forums roll for a challenge picture?

...

Do you have a single fact to back that up?

That is not the only one

The whole point is to use Vulkan. Obviously the triangle itself is not my end goal.

C is garbage, proove me wrong.

It's not.
QED

>proove me wrong
C and C++ are the only widespread high level language that did not have GC.
Other languages are complete garbage, and they need their GC to make it bearable

>C is garbage
Compared to what? Remember, most languages that would be compared favorably to C are much newer. That tends to be how technology progresses.

Thank you. I just remembered that I used to have a folder with a solution to a large number of these. I wish I knew where those were.

Is there a way to read a simple comma delimited file in C++ into an array that doesn't look like total ass? I figured you could do something simple like tell cin to treat commas as whitespace and just read it normally, but all of the implementations I see use stringstreams to do it. Is C++ such a garbage language that there's no easier way to do it or am I just being memed.

I need to make an evaluation function for a board game AI.

Any tips?

C++ is the most powerful and efficient programming language.

Haskell is better.
GC is necessary, don't tell me you like to malloc() and free()
Real programmers need to think in terms of the problem, not in terms of pointers and malloc();

>Is C++ such a garbage language that there's no easier way to do it
basically, yes

quads of truth

it's a gross language, though

should i learn C or C++(and try to avoid the shit)?

Nice
>(:i.100)(]`[@.(0:=#@>@]))"0 0(100$(2$a:),(100$(4$a:),

A few method can be done easily
1) use std::getline(std::ifstream &, std::string &, char);
2) read all data to a char array, then add null in the end of the array, and use std::strtok()
3)read the data to an array then parse it yourself //A bit complex
4)read it to a string then use std::string::find(char) and std::string::find_fist_not_of(char)

>Haskell — 1990
>C — 1972
Also, there's something between GC and malloc, user.

what shit are we talking about here

If it were me, I would stick to C. The nice thing about C is that is all works in C++ and it doesn't have all of the added bullshit that makes C++ so ugly and disgusting

>don't tell me you like to malloc() and free()
Wait did you mean, someone somewhere in the world in still write malloc() and free()???

millions of people user.
And the scary part is, a great portion of those people are so stockholmed by C, they regard everything as menial as malloc/free as "real programming".

def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
...


is there a construct for automating that?

use a better language

I can't the stuff i'm working on is for school assignments.

go to a better school

class Test(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)

t = Test(x=1,y=2,z=3)

print("{}, {}, {}".format(t.x, t.y, t.z))

Can you do
Test(self =1)

no, you'll get a TypeError.

well i mean Test(self=something)

no

>>> class Test(object):
... def __init__(self, **kwargs):
... [setattr(self, k, v) for k, v in kwargs.items()]
...
>>> t = Test(x=1)
>>> print(t.x)
1
>>> u = Test(self=t)
Traceback (most recent call last):
File "", line 1, in
TypeError: __init__() got multiple values for argument 'self'

this is cool
but i would still need to write it out for args with default values, unless you have more tricks up your sleeve.

also i could end up with a bunch of variables i don't want.

nvm didn't realize i have to specify the var names in the initializer call

class Test(object):
name = "john"
age = 18
sex = "male"

def __init__(self, **kwargs):
[setattr(self, k, v) for k, v in kwargs.items() if hasattr(self, k)]

Ok lads, I have decided to man up, and read "Introduction to Algorithms" by Cormen. I am a brainlet, but I will never stop being a brainlet unless I challenge my brain. Now, I have certain questions regarding this book.

1. What is the best way to read this book? Front to back? Or should I select certain parts of the books that I find interesting?

2. Is the math appendix of this book enough of a math background for me to understand all the "mathy parts" of the books?

Yes, I know, retarded questions, but I prefer to prepare a strategy on how to tackle this book, instead of charging foolishly against this behemoth. Hopefully my questions make sense. Thanks in advance.

Iirc there's a lot of chapters which use methods from previous chapters which may not be obvious, so you'll have to read it front to back. Theres not a lot of math in it, o doubt you need to read anything else before you can start it.

>b-b-b-but you c-can't PROVE it!
Linear types exist mainly for fizzbuzzers to post their memes. All the facts point to it.

C++ is one of the most primitive and least powerful programming languages still in use, and the more of its "powerful" features you use, the less efficient your program becomes. When will sepples monkeys understand this?

I just ran
pip3 install tensorflow


and now I feel like hackerman

Thank you, sir. Let this day be known as the day I stop being a brainlet. Wish me luck!

(1

reminder that the best way to learn is to make shit

Who needs functional programming when C has function pointers?

>that pic
she must be used to work with teletypes on mainframes

I have never been as disappointed as right now when I found out that affine types have nothing to do with affine vector spaces. I made a library for 'affine types' (as in affine vector spaces) and use in in almost all my programs, so when I found out that it's built into some languages I though it was great, but turns out it's something completely different.

Pardon my ignorance, new to pointers in general, but what are function pointers in C, and what are they useful for?

somebody PLEASE give me a quick rundown on how to do DMA memory-to-memory copying for the x86 platform in kernel-mode

Protip: linear types have nothing to do with linear algebra.