[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Voyeur Cams | Click for more| Home]

/dpt/ - Daily Programming Thread

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 324
Thread images: 18

File: shig pee.jpg (133KB, 960x624px) Image search: [iqdb] [SauceNao] [Google]
shig pee.jpg
133KB, 960x624px
Old thread >>51482173

What are you working on, /g/?
>>
First for D
>>
animooooooooooooooo
>>
Fourth for Sepples
>>
File: beyond-fizzbuzz.jpg (33KB, 381x499px) Image search: [iqdb] [SauceNao] [Google]
beyond-fizzbuzz.jpg
33KB, 381x499px
Hey /g/, do you know how to average 2 ints in C?

>protip: you don't!
>>
File: C.png (136KB, 2000x2126px) Image search: [iqdb] [SauceNao] [Google]
C.png
136KB, 2000x2126px
>>
>>51488714
return a + (b - a) / 2;
>>
>>51488714
return (a * 7 * 5 / 7 / 5 + b * 5 * 3 / 5 / 3) / 8 / 7 / 6 / 5 / 4 / 3 / 2 * 3 * 4 * 5 * 6 * 7 * 8;
>>
>>51488714
#include someAveragingLibrary.h

int function(int a, int b) {
return Average(a, b);
}


Better question I would waste my time on that.
>>
>>51488679
JUST
>>
>>51488814
FUCK
>>
>>51488797
kill yourself
>>
>>51488823
MY
>>
>>51488752
Wrong.
>>51488778
Inane.
>>51488797
You don't even know C syntax.

>>protip: you don't!
>>
>>51488829
SHIT
>>
>>51488830
>Wrong
literally retarded
>>
>>51488836
UP
>>
>>51488679
Getting a feel for associated types in Swift protocols.

infix operator <- {}

func <-(lhs: Car, rhs: String) {
lhs.fuck(rhs)
}

protocol Fuckable {
typealias Type

func fuck(thing: Type)
}

extension Fuckable {
func fuck(thing: Type) {
print("Going to fuck a \(thing)")
}
}

struct Car: Fuckable {
typealias Type = String
}

let a = Car()

a <- "gas nozzle"
>>
>>51488839
Yes, you are. That's what my comment implied.
>>
>>51488852
how is it wrong you fucking retard
>>
>>51488714
Why would I use ints when the average of the first two integers is a float?
>>
File: 9xz7l.png (26KB, 98x173px) Image search: [iqdb] [SauceNao] [Google]
9xz7l.png
26KB, 98x173px
>>51488814
>>51488823
>>51488829
>>51488836
>>51488844
>>
>>51488714
what type of average do you want? mean, median or mode? and do you want it floating point or floored integer?

there's a lot of different averages, specify one fuckwit.
>>
>>51488714

int average(int a, int b) {
int differenceBetweenAAndB;
if (a < b) {
differenceBetweenAAndB = b - a;
} else {
differenceBetweenAAndB = a - b;
}

int averageOfTwoInt32Numbers
if (a >= b) {
averageOfTwoInt32Numbers = b;
} else if (a < b) {
averageOfTwoInt32Numbers = a;
}

averageOfTwoInt32Numbers += (int) (differenceBetweenAAndB / 2);
return averageOfTwoInt32Numbers

}


EASY MATE EASY

GIVE US SOMETHING HARD NEXT TIME
>>
this
ip = input("Enter IP address.\n")
a,b,c,d = map(int, ip.split("."))
print(a,b,c,d)
desu = a*256+b
desu2 = desu*256+c
desu3 = desu2*256+d
print([desu3^i for i in range(100)])

Going to add more obscurification later.
>>
>>51488865
This is a good reason to install someAveragingLibrary, because it can do all of those. Too bad you can't just `pip install someAveragingLibrary` in C++...
>>
>>51488865
it's safe to assume that the task requires truncated integers. anything else would require more information from the questioner.
>>
>>51488714
return (a / 2) + (b / 2);
>>
>>51488857
It doesn't work.
>>51488858
Performance.
>>51488865
Arithmetic mean, like any sane person would conclude. Rounded towards zero.
>specify one
It wouldn't matter, you'd fail regardless.
>>
>>51488881
Wow, I almost thought you were writing C# for a second.
>>
>>51488912
>the average of 1 and 1 is 0
>>
<code> (a+b)/2
>>
>>51488914
>Sacrificing the correct answer for performance

This is how C++ programmers actually think.
>>
>>51488881
Shit style, doesn't compile and also wrong.
>>
>>51488914
>It doesn't work.
FUCKING AUTIST tell me HOW it doesn't work
>>
>>51488912
No.

>>protip: you don't!
>>
>>51488937
RETARD tell us HOW it is wrong
>>
int avg(int x, int y){ return (a+b)>>1; }
>>
>>51488958
Wrong.
>>
>>51488937
>doesn't compile

Lmao. Alright how about this. Does this satisfy your autism.

double averageVERSION2ELECTRICBOOGALOO(int a, int b) {
double differenceBetweenAAndB;
if (a < b) {
differenceBetweenAAndB = b - a;
} else {
differenceBetweenAAndB = a - b;
}

double averageOfTwoInt32Numbers
if (a >= b) {
averageOfTwoInt32Numbers = b;
} else if (a < b) {
averageOfTwoInt32Numbers = a;
}

averageOfTwoInt32Numbers += (differenceBetweenAAndB / 2.0);
return averageOfTwoInt32Numbers;

}
>>
>>51488923
>>51488958
>overflow
>rightshift rounds to negative infinity instead of to zero
>>
>>51488954
>doesn't compile
>how it is wrong
m8....
>>
>>51488881
clean that shit up
#define ABS(x)    (x > 0) ? (x) : -(x)
#define MIN(x, y) (x < y) ? (x) : (y)
int average(int a, int b)
{
return MIN(a, b) + ABS(b - a) / 2;
}
>>
>>51488912
>2015
>pretending to be retarded
>>
>>51488958
int avg(a,b){
return (a+b)>>1;
}


and the assembly
avg:
addu $v0, $a0, $a1
srl $v0, $v0, 1
jr $ra
>>
>>51488968
It's about satisfying the compiler. Still doesn't.

>>protip: you don't!
>>
>>51488968
You fuckhead, it doesn't compile because you forgot a semicolon.
>>
>>51488984
This is correct.
>>
>>51488984
>>51489010
It's not.

>>protip: you don't!
>>
long x = a+b
return int(x/2)

we did reddit
>>
>>51488996
>>51489008

Calm down lads I was just writing obfuscated code for jokes.
>>
>>51489035
You don't know enough C to even begin to approach this problem.
>>
Stop replying to the faggot with the averaging function or this entire DPT will be filled with bullshit replies to a troll
>>
Looking to get into programming.
What is some suggested material to read/study?
>>
int average(int a, int b) {
while(a>b) { a--; b++; }
while(b>a) { a++; b--; }
return b;
}
>>
>>51489038
>guise, I was just PRETENDING!
>>
int difference = b - a;
if(b < a) {
--difference;
}
return a + difference / 2;
>>
>>51489041
Yeah, let's discuss anime and traps instead!
>>
>>51489041
>I don't know C
>stop talking about programming
>post some animu, guise!
>if you keep talking about C, I'l go back to my homescreen thread
m8...
>>
File: 1447770600684.png (206KB, 331x339px) Image search: [iqdb] [SauceNao] [Google]
1447770600684.png
206KB, 331x339px
>>51489042
structure and interpretation of computer programs is a good first read, google anything you dont know
>>
File: Andy.png (487KB, 692x692px) Image search: [iqdb] [SauceNao] [Google]
Andy.png
487KB, 692x692px
>>51489054

>the retarded names, multiple redundant if's, and redundant variables weren't enough of a clue
>>
>>51489042
SICP
>>
>>51489062
>>51489066

Just give the answer so we can all learn something and move on with our lives
>>
File: kramer tips.jpg (70KB, 719x686px) Image search: [iqdb] [SauceNao] [Google]
kramer tips.jpg
70KB, 719x686px
>>51489071
>I didn't terminate a declaration with a semicolon
>I was just writing """""""""obfuscated code""""""" guise!
>>
This thread is going places.
>>
is this really the way you're supposed to do it in C/C++:
const char *some_variable = "defined somewhere else, but potentially of any length";
char *buf[100];
sprintf(buf, "something %s", some_variable);


... answered my own question
char *buf[sizeof(some_variable)+5];
sprintf("%s1234", some_variable);

I hope adding 1 for the \0 at the end of the string is enough

I really don't like segfaults :<
>>
>>51489048
muh nigga
>>
>>51489059
nope
>>
>>51488714
int average(int x, int y)
{
size_t sx = x;
size_t sy = y;

return (sx + sy) / 2;
}
>>
>>51488714
>>51488921
Whoops, forgot some casts.
return (int64_t)((float)a / 2.0) + ((float)b / 2.0);
>>
I jacked the other guy's solution but whatever
#define ABS(x)    (x > 0) ? (x) : -(x)
#define MIN(x, y) (x < y) ? (x) : (y)

double average(int a, int b)
{
return (double) (MIN(a, b)) + (double) (ABS(b - a)) / 2;
}
>>
>>51489099
there's no C/C++
>sprintf("%s1234", some_variable);
no
>>
>Integer averaging. Cast edition

return  ( (double)( a + b ) ) / 2.0 ;


Can you even cast like that in C
>>
>>51488714
template <typename T = int32_t>
T average(const T a, const T b, std::enable_if<std::is_integral<T>::value>::type *)
{
return a + ((b - a) / 2);
}
>>
>>51488679
>What are you working on, /g/?
Thank you for asking. I am working on the official /dpt/ C toolkit, a general utility, state of art, FOSS, community driven, library for C.
>>
>>51489048
Correct.
>>51489059
Wrong.
>>51489113
Wrong.
>>51489116
Correct (mostly).
>>51489121
Wrong.
>>51489134
Wrong.
>>51489137
Wrong.
>>
>>51489194
but can you average two int variables, faggot?
>>
>tfw we just did some scrub's homework
>>
File: Seobot.jpg (51KB, 449x700px) Image search: [iqdb] [SauceNao] [Google]
Seobot.jpg
51KB, 449x700px
Don't forget to cast your variables, kids.

return (double)((float)((short)a+(long)b)/(double)((int)0x2);
>>
>>51489233
>scrub
>99% of /dpt/ doesn't know C
anon...
>>
>>51489214
>Correct

please explain how an O(n) loop is more efficient than calculations on a hardware floating point unit

genuinely curious
>>
>>51489214

>>51489121
>Wrong.

How?
>>
>>51489233
naw he's some smug idiot that learned from his prof that the naive avg approach won't work in edge cases so he keeps posting the question in /dpt/ tryna act smart
>>
>>51489249
Wrong.
>>
>>51489260
>99% of /dpt/ doesn't know C
Other languages do exist anon
>>
>>51489279
>we don't know the simplest language
>we know the others, I swear!
>>
>>51489263
>is more efficient
It isn't.
>>
>>51489263
He asked to find the average of two integers. Efficiency was not of concern, as long as the result is correct.
>>
>>51489292
>omg why don't you just ignore the modern alternatives and learn this obsolete garbage?!
>>
>>51489266
>I'm dumb but I have excuses
>stop trying to educate me
>>
>>51489305
>>51489306

ok but

>>51488858
>Why would I use ints when the average of the first two integers is a float?

>>51488914
>Performance.

I don't understand what's wrong with the standard mathematical method of finding an average
>>
>>51489307
>I like to pretend I know these modern languages so I don't have to actually know programming
>memejs just werks™
>>
File: developers.jpg (102KB, 700x501px) Image search: [iqdb] [SauceNao] [Google]
developers.jpg
102KB, 700x501px
>thinking up retired excuses not to learn C
It's 2015 and people STILL do it. Why?
>>
>>51489111
LITERALLY COMPLETELY FUCKING RETARDED
>>
File: LOL.png (157KB, 642x834px) Image search: [iqdb] [SauceNao] [Google]
LOL.png
157KB, 642x834px
ITT:
>>
>>51488679
programmers are nerds LOL
medicine is the future
>>
>>51489364
stay pleb
>>
>>51489349
>I'm so mad because my I don't want to actually think
Sucks to be you.
>>
>>51489364
>implying doctors won't be replaced by machines in the next 50 years
>>
File: truetruth.jpg (790KB, 1004x2693px) Image search: [iqdb] [SauceNao] [Google]
truetruth.jpg
790KB, 1004x2693px
>>51489381
>>
>>51489351
Well she's right.
>>
>>51489351
>muh memejs
>muh css
stay pleb
>>
File: 1428432770883.jpg (929KB, 966x3054px) Image search: [iqdb] [SauceNao] [Google]
1428432770883.jpg
929KB, 966x3054px
>>51489401
>studying to become a wageslave
>>
>>51489401

Cheers lad gonna appropriate this image to change medics to science students for banter
>>
>>51489427
Oh NEETs
You think so highly of yourselves
>>
>>51489351
How the hell do people not know how to reverse a binary tree? It's fairly easy.
func reverse(node)
temp = node.left
node.left = node.right
node.right = temp
reverse(node.right)
reverse(node.left)
end
>>
>>51489490
Better yet, why doesn't your binary tree have a reverse iterator?
>>
>>51489427
>be me
>get up at 8 AM
>be at work at 9 AM
>varied and interesting soft and hardware development job
>go home at 5:45 PM
>do whatever until time to sleep (11:45 PM)
>get loads of money

And I'm usually even pretty relaxed too when I get home.
If that's not what you do then you should go reevaluate your life decisions.
>>
>>51489427
Truth is you are probably homeless and gopnik posting from a stolen KGB machine
>>
>>51489490
>not know how to
Because they're too stupid to assign some variables and they're too stupid to walk a tree.
Proof: shitload of retards ITT that can't average 2 numbers.
>>
>>51489545
Add and divide by 2? Or are you going to pull some memey bullshit on me...
>>
>>51489351
She's right though.
>>
>>51489401
underage b& detected
>>
>>51489099
sizeof will return the size of the pointer, not of the string. To get the length of a string, you need to use strlen().

But that isn't a compile-time constant, and array sizes typically need to be compile-time constants (automatic arrays in C99 can have variable length, but there's no way to check for allocation failure; if the array overflows the stack, the program just crashes).

So if you're dealing with a string whose length isn't known at compile time, you need to use malloc() (or some platform-specific allocation function).
>>
>>51488714

(a ^ b) >> 1 + (a & b)
>>
how do i increase an integer inside a value string when writing linear functions in lisp?i need this for artificial forensics
>>
>>51489351
>reversing a binary tree
>unsolvable in 45 mins

holy fuck, are web devs really this retarded?
>>
WHY THE FLIPPITY FUCK IS THIS WRONG ON THE LAST TWO

http://ideone.com/KPqPmh
>>
guys

When I had my .c and .h files -- let's say i'm importing in sdl

should i import the sdl headers via my project's .h file? or should i import it via the .c file?
>>
>>51489584
> Add and divide by 2?
The addition may overflow. For signed integers, that's undefined behaviour.

Similarly, if you use a+(b-a)/2, b-a may overflow.

(a/2)+(b/2) doesn't overflow, but rounding is incorrect (e.g. a=b=3 => 3/2+3/2=1+1=2).

Conversion to "long" doesn't help because "long" isn't guaranteed to be any larger than "int" (e.g. on "64-bit" Windows, both are 32 bit).

Conversion to "double" doesn't help because "double" isn't required to have enough bits in the significand (IEEE double precision has a 53-bit significand including the implied leading one, some platforms have a 64-bit "int").
>>
>>51489533
>gopnik
not a word you hear around here often Vladimir
>>
>>51489694
always keep includes in the header file (but remember about include guards), unless you have circular dependency
>>
Hey, /g/entooman! Does someone have that image with those 100 ideas for projects? I lost it on my archive.

Thanks in advance.
>>
>>51489707
Doesn't matter in Python.
>>
>>51489645
Bitwise operations are undefined on negative integers.
>>
>>51489723
https://better-dpt-roll.github.io/
>>
>>51489707
>Conversion to "long" doesn't help because "long" isn't guaranteed to be any larger than "int" (e.g. on "64-bit" Windows, both are 32 bit).
Using numbers bigger than 32767 in ints is not portable.
Converting to longs is a valid implementation.
>>
>>51489716
what if i want to make a global variable from main.c available in another .c file?
>>
>>51489741
Thank you, friend.
>>
>>51489690
HOLY SHIT C IS AUTISTIC

http://ideone.com/qXPpZQ

OF COURSE I WOULD USE STDINT IN A SERIOUS APPLICATION BUT JEEZ WHY CAN'T WE HAVE 64-BIT LONGS IN 2015
>>
>>51489794
Something.h:
#ifndef SOMETHING_H
#define SOMETHING_H

extern int variable;

#endif


Something.c:
#include "Something.h"

int variable = 5;


Another.c:
#include "Another.h"

fprintf(stdout, "%d", variable);


Another.h:
#ifndef ANOTHER_H
#define ANOTHER_H

#include "Something.h"

#endif
>>
Lisp faggots explain
>>
>>51489809
int64_t
>>
>>51489833
Lisp is the language of the chosen people.
>>
>>51489809
>WHY CAN'T WE HAVE 64-BIT LONGS IN 2015
Shouldn't that be the case if you have a 64-bit machine?
>>
>>51489809
Because long is defined as 32-bit.
If you care about the length use (u)int(n)_t or (even better) (u)int_least(n)_t.
>>
>>51489707
>doesn't even know the C standard

int is only guaranteed to have 16 bits (hence anyone using them should only use values up to 32767), if you want to be autistic, conversion to long is valid and the addition doesn't overflow.
>>
>>51489833
>clisp
>anno domini 2015
bitch please, everyone uses sbcl
>>
>>51489844
>>51489864
>OF COURSE I WOULD USE STDINT
>>
>>51489898
calm down faggot
>>
>>51489898
then what's the fucking problem you double nigger
>>
>>51489890
>int is only guaranteed to have 16 bits
but in practise it will have 32 bits on 32 bit machines?
>>
>>51489828
I am pretty sure the "extern" declaration in the .h file is not necessary, as it the definition in the .c file.
Or I've been using an implementation specific feature all the time.
>>
I'm a fucking retard, can you point me how i can learn programming without shitty curses?
>>
>>51489926
That depends entirely on your compiler. And it might behave differently if you then compile it with a different one.
>>
>>51488914
>Arithmetic mean, like any sane person would conclude. Rounded towards zero.
any sane person would assume that encryption is easy. until they have to program it.
>>
>>51489490
32 minutes, C-ish pseudo code for a tree inversion (leaves become roots and all leaves are kept in a list).
>>
I got you senpai.

int32_t Average(int32_t a, int32_t b)
{
int64_t avg = (a + b) / 2;

return static_cast<int32_t>(
std::max<int64_t>(-2147483647, std::min<int64_t>(avg, 2147483647))
);
}
>>
>>51489926
probably, but your code won't be portable if you use them for values greater than 32767.
>>
>>51489971
fuck me
typedef struct invTree {
value
child
} invTree;

l = new List<invTree>

void invertTree(Node *node, invTree parent) {
invTree invTreeNode = new invTree{node.value, parent}
if node.right == NULL && node.left == NULL {
l.add(invTreeNode)
} else {
match (node.right, node.left) {
(NULL, _) =>
invertTree(node.right, invTreeNode)
(_, NULL) =>
invertTree(node.left, invTreeNode)
(_, _) =>
invertTree(node.left, invTreeNode)
invertTree(node.right, invTreeNode)
}
}
}

invertTree(head, NULL)
>>
>>51489763
>Using numbers bigger than 32767
>Converting to longs is a valid implementation
Spotted the retard.
>>
>>51489890
>the addition doesn't overflow
It does.
>>
>>51489991
You don't appear to know C.
>>
>>51489982
you're literally retarded
>>
>>51490015
I know for a fact you don't know what the fuck you're talking about.
>>
This is why we have arbitrarily precise integers.
>>
>>51489828

worked perfectly.

why the use of include guards?

don't you think that in 2015 that clang/llvm would figure it out?
>>
>>51490016
But he's right though, in both C and C++ int is only guaranteed to have at least 16-bits of width but no more, so technically your code won't be portable if you use ints for values greater than that and as such you should use fixed-width types.
>>
>>51488714
return (a >> 1) + (b >> 1) + (a & b & 0x1);


but you aren't allowed to use that, sinc it's patented: http://www.google.com/patents/US6007232?dq=6007232

ameriburgers really grant patents for trivial shit like that?
>>
>>51490035
MODULES WHEN
>>
>>51489864
longs are actually 'at least 32-bits', but can be defined as longer. Just like ints are 'at least 16-bits', but can be 32-bits like in x86 with gcc.
>>
>>51490035
>don't you think that in 2015 that clang/llvm would figure it out?

Nope. Also you might want to use #pragma once instead of defines, it's a bit cleaner but it's not standard (although all relevant compilers support it).

>>51490051
C++17 for C++ (optimistically), probably never in C
>>
>>51490004
long is guaranteed 32 bit. since when does adding two 16 bit numbers yield a number greater than that?
>>
>>51489991
aren't longs supposed to be at least 32 bits?
>>
how the fuck do I'm suppose to use git in ubuntu.

I was following the tutorial and my folder files just dissapeared.

wtf.
>>
>>51490094
why did you delete the folder?
that wasn't a part of the test
>>
>>51489940
Well, by doing that, you end up with one "variable" defined and statically allocated for each .c file that includes something.h. That's not what we want here, we want a unique "variable" that is declared and initialized in somewhere.c, and whose definition (a symbol) is used in another.c without questioning if it's allocated or not (that's what extern really means). The compiler will then link the symbol of "variable" in another.c with its memory address allocated in something.c.
>>
>>51490073
overflow. A 16-bit + 16-bit number is a 17-bit number. We just normally truncate the result to 16-bits.
>>
>>51490126
>17 bits overflows 32+ bits
>>
>>51490037
You don't know what you're talking about, mate. You actually don't understands what it means to be portable and what the standard guarantees.
This is not portable, because int might not be able to hold 100000.
int x = 100000;

This IS portable, and INT_MAX can be larger than 32767.
int x = INT_MAX;

So the statement that conversion to long is valid because int is less than 32767 is fucking stupid! int is less than INT_MAX, you fucking monkeys and 32767 is the guaranteed minimum for that constant.
>>
>>51490148
alright fucking nasa, not everyone has 32 bits
>>
>>51490047
doesn't matter, it doesn't work anyway
>>
>>51490094
>my folder files just dissapeared
that's lollinux for you. linux can't be used for serious computing. it's merely a toy OS and you should expect to have to reinstall your OS many times per year. meanwhile i've had the same windows 7 installation for 6+ years
>>
>>51490148
long is guaranteed 32 bits, but you are forgetting that ints CAN BE 32 bits. longs are not guaranteed to be larger than ints either.
>>
>>51490073
>>51490079
int can be 32 bits.
>>
>>51490167
>not everyone has 32 bits
In this day and age, how?
>>
>>51490155
>standard compliant code is portable
>>
>>51490167
>Long signed integer type. Capable of containing at least the [−2147483647, +2147483647] range;[3] thus, it is at least 32 bits in size.
https://en.wikipedia.org/wiki/C_data_types#Basic_types
>>
>spent 1 hour debugging a simple makefile with 3 .o files
>i wrote gcc instead of g++

JUST
>>
>>51490192
>yes
>>
>>51490155
don't bother replying, retard
>>
Easiest way to make averaging two numbers in C is
int average(int a, int b)
{
return (a>>1) + (b>>1) + (a&1 + b&1 >> 1);
}

This makes the rounding accurate.
>>
>>51490073
>adding two 16 bit numbers
We're not talking about adding 16 bit numbers, we're talking about adding int numbers.
>>
>>51490215
>I don't know C
>>
>>51490203
overflows on 16 bit systems
>>
>>51490225
Wrong.
>>
>>51490213
>innocence
>>
>>51490047
>it's patented
it's trivial as shit and you should be able to use it just fine. just don't tell anyone about it
>>
File: druUsnp.png (870KB, 794x698px) Image search: [iqdb] [SauceNao] [Google]
druUsnp.png
870KB, 794x698px
>>
>>51489740
only for the left shift, tard.
>>
>>51490037
>technically your code won't be portable if you use ints for values greater
Were you dropped on your head?
>>
>>51490249
what?! on a 16-bit system longs are still at least 32 bits wide
>>
>>51490173
yes it does.
>>
>>51490259
Lol. It's correct. Maybe I should use modulos instead of anding a one. Don't remember how signed numbers work represent even or odd.
int average(int a, int b)
{
return (a/2) + (b/2) + ((a%2 + b%2) / 2)
}
>>
>>51490226
but you should never expect that an int can hold more than 16 bits, anon.
>>
>>51490300
>Don't remember how signed numbers work
And what makes you think you can write C?
>>
>>51490309
I would considering most people use at least a 32-bit machine
>>
File: gdev.jpg (58KB, 590x536px) Image search: [iqdb] [SauceNao] [Google]
gdev.jpg
58KB, 590x536px
>>51490300
int average(int a, int b)
{
div_t div (in, int);
div_t hA = div(a, 2);
div_t hB = div(b, 2);
return hA.quot + hB.quot + (hA.rem + hB.rem) / 2;
}
>>
>>51490311
>not using based sign bit representation
>needing a compliment
>>
>>51490309
you should stop spewing shit you overheard without understanding what you're saying
>>
>>51490299
no, it doesn't.
>>
>>51490179
Just 6 years. Windows 7 came out in 2009, it's 2015 now.
>>
>>51490349
It's 20 what?
>>
>>51490327
Wrong.
>>
>>51488893

On non-shit operating systems you can.

It's called a package manager.
>>
>>51490309
you should never expect that a char can hold more than 8 bits
>>
>>51490300

Why not?
int avg(int a, int b)
{
return (a + b) / 2;
}
>>
File: 1441726208672.png (2MB, 1920x1080px) Image search: [iqdb] [SauceNao] [Google]
1441726208672.png
2MB, 1920x1080px
>not using unsigned long longs
>>
>>51490399
it doesn't work
>>
>>51489266
Not him, but it seems /dpt/ need some educating.

I can't believe there are so many replies in this thread from people who don't even understand something as simple as signed overflow, or 2s compliment binary, or rounding.
>>
>>51490399
That's what we've been discussing. a+b can overflow.

>>51490300
avg of 2 and 4 is 3
avg of 2 and 5 is 3
avg of 1 and 3 is 2
avg of -1 and 5 is 2
avg of -1 and 4 is 2
avg of -2 and 2 is 0
avg of -1 and -5 is -3
avg of -1 and -4 is -2
avg of -4 and -6 is -5

Does this look right? I don't think -1 and 4 is 2, it should be 1, right?
>>
>>51490349
i've been using it since the RC faggot

and it's almost 2016

probably 7 years or like 6 months and 10 months or something like that
>>
>>51490402
>thinking that using unsigned long long makes you good at C
>>
>>51490402
pretty sure he faked his death
>>
>>51490426
My statement wasn't implying anything dickweed
>>
>>51490402
>>51490426
>using anything other than inline assembly
>>
>>51490439
>implying no implications
>>
>>51490420
nvm maybe 6 and a half years
>>
>>51489852
>Shouldn't that be the case if you have a 64-bit machine?

It's implementation defined. Mostly, it's defined by the ABI of the system you're using: take a look at the x32 ABI on Linux if you've never considered this sort of stuff before and want a bit of a mindfuck.
>>
>>51490444
>not machine language
>>
>>51490444
>using anything other than paper and pencil
>>
quality bants lads
>>
>>51490467
>using something easier than inline assembly
>>51490458
>not using defines to turn pseudo machine code into inline assembly into machine code
>>
>>51490458
>machine language

I wish
>>
>>51490487
>it's all over my head
>I don't understand any of this
>how about some anime?
>did you see the desktop thread?
>guise?
>>
>>51490399
A = 5
B = 8

Expected average: 6.5
According to your method: 6
>>
>>51490066
think we'll see a C17?
>>
>>51488714
int average2i(int x, int y)
{
div_t halfX = div(x, 2);
div_t halfY = div(y, 2);

return (halfX.rem + halfY.rem + 4) / 3 - 1 + halfX.quot + halfY.quot;
}
>>
>>51490538
that's how ints get rounded, anon.
>>
>>51490538
>Expected average: 6.5
6.5 is not an integer son.
>>
>>51490559
>rounded
truncated, not rounded.
>>
>>51490344
>>51490299

dpt in a nutshell.
>>
>>51489401
This is a piece of shit b8, m8.
>>
>>51490574
>I don't know C
>>
>>51490570
rounded toward zero.
>>
what is more efficient in C:

- keep everything in a global variable
- pass the variable / reference to inner struct variable around by reference in functions?

this may sound stupid, but i come from an interpretted language where this kind of shit slows things down quickly
>>
>>51488714
int average(int a, int b){
return ((a+b)/2);
}
there nigger
>>
>>51490545
Wrong.
>>
>>51490674
Doesn't work, nigger.
>>
>>51490654
Keeping things in globals is faster to write since you don't have to type out that thing in the argument list of every function that uses it. However, it makes it difficult to change your program later if you ever decide you need more than one of a certain thing.

The runtime difference is negligible.
>>
>>51490674
>average(2147483647, 2147483647);
>>
>>51490570
>truncation is not rounding
>>
>mfw valve used 32 bit ids for items in steam
>the limit was reached two days ago, and no new items can be created
>mfw i have no face
>>
>>51490717
sure it does nigger

>>51490721
AVERAGE OF TWO INTS NIGGER, did you learn how to round in school, oh yeah you never went there.
>>
>>51490721
>assuming int is always 32 bits

can't you read, anon? you clearly don't know C.
>>
>>51490790
Sure doesn't.
>>
int average(int a, int b)
{
return abs(a/2 - b/2) + ((a%2 + b %2) / 2) + min(a, b)
}

lel
>>
>>51490846
Not working.
>>
>>51490875
#define abs(x) ((x) < 0 ? -(x) : (x))
#define min(a, b) ((a) < (b) ? (a) : (b))

Don't know how to define simple macros my man?
>>
>>51490790
>>51490807
retards

average(MAX_INT, MAX_INT)

where is your god now
>>
>>51490908
Don't know C my man? Because that doesn't work.
>>
>>51489351
so basically she's proud of being a fucking code monkey that can't implement any of the libraries she uses and only writes dumbass glue code that utilizes other people's libraries.
And she managed to drag gender politics into it.
>>
>>51488679
A game using libgdx
>>
Hey /g/ im so happy i wrote my first program in python
its a simple rock paper scissors game

Is there anything wrong with the code?
thanks for help
#SIMPLE ROCK PAPER SCISSORS GAME
import random

#computer choice
random_num = random.randrange(0,3)
array = ["rock", "paper", "scissors"]
b = array[random_num]
#Game
def play():
if a =="rock":
if b == "rock":
print "draw"
elif b == "paper":
print "you lose"
else:
print "you win"
elif a == "paper":
if b == "rock":
print "you win"
elif b == "paper":
print "draw"
else:
print "you lose"
elif a == "scissors":
if b == "rock":
print "you lose"
elif b == "paper":
print "you win"
else:
print "draw"
else:
print "You have to choose rock, paper or scissors!"

def restart():
c = raw_input(">> ")
if c == "restart":
play()
#Start of the game
print "Choose rock/paper/scissors"
a = raw_input("> ")
print "1...\n2...\n3..."
play()

print "Game Over"
print "Your opponent had: " + b
print "If you want to play again type restart"
restart()

>>
>>51489351
>software "engineer"
lol more like professional code monkey.
>>
>>51489194
Are you le fuggn ebin 日本語話者
>>
// new averaging system
float pp;
float x;
float xx;
float xxx;
float xxxx;
float xxxxx;
float xxxxxx;
float xxxxxxx;
float xxxxxxxx;
float xxxxxxxxx;
float xxxxxxxxxx;
float xxxxxxxxxxx;
float xxxxxxxxxxxx;
float xxxxxxxxxxxxx;
float xxxxxxxxxxxxxx;
float xxxxxxxxxxxxxxx;
float xxxxxxxxxxxxxxxx;
float xxxxxxxxxxxxxxxxx;
float xxxxxxxxxxxxxxxxxx;
float xxxxxxxxxxxxxxxxxxx;
float xxxxxxxxxxxxxxxxxxxx;
cout<<endl;
cout<<"======="<<endl;
cout<<"Average"<<endl;
cout<<"======="<<endl;
cout<<endl;
cout<<"how many values are you averaging? (max 20)"<<endl;
cin>>pp;
>>
>>51491009
her website is pure gold

>I will only speak at events with a Code of Conduct.
>I learnt to program after taking CS 101 at Harvey Mudd College, because of the department's initiatives to make CS more welcoming to newbies :D
>watch prime-time TV
>I primarily work in Ruby on Rails

why is webdev so cancer, /dpt/?
>>
>>51491078
Because it's so easy.
I feel like I should eat my pride and learn some fucking javascript so I can get my foot in the door.
>>
>>51491078
It's not very technically challenging. In labour is in very high demand, so people who are not very good will still find work.
>>
>>51491078
>why is webdev so cancer, /dpt/?
Because the .com bubble made it look like it was some perfect job to normies and they all were instantly attracted to it. Then they brought in their normie culture, which made all normies somewhat interested in something to do with computers go directly to web dev. Finally, the few intelligent people there started making libraries and frameworks for the more retarded people so that they could gain some more money off of it. Then we were left with a bunch of mentally retarded people who have libraries that can do all the work for them.
>>
>>51491033
the computer choice won't change when you restart
>>
>>51488714
>stupid question with no specification
>people reply to the post, half of the thread dedicated to it

>>51489351
>this pic, for the nth time
>people reply to the post

/g/


>>51489427
>>51489401
haha
>>
>>51491033
technically nothing "wrong", but lots of code that's not very clever, some that's hard-coded in, which you generally don't want. you want code to be able to survive extension without breaking. so if you imagined a fourth item and added it to your array, how much more code would you need to fix?

This doesn't solve that issue, but it shows how much more concise you can be while approaching a solution that's more abstract:

import random

choices = ["rock", "paper", "scissors"]

def play(player_choice,computer_choice):
if player_choice=="rock":
return -1 if computer_choice=="paper" else 1 if computer_choice=="scissors" else 0
elif player_choice=="paper":
return -1 if computer_choice=="scissors" else 1 if computer_choice=="rock" else 0
elif player_choice=="scissors":
return -1 if computer_choice=="rock" else 1 if computer_choice=="paper" else 0
else:
print "You need to choose between rock, paper, and scissors!"
return play(raw_input("choose now: "), computer_choice)

score=0
player_choice=raw_input("Choose between 'rock', 'paper', and 'scissors': ")

while player_choice!="quit":
computer_choice = random.choice(choices)
this_round=play(player_choice, computer_choice)
score+=this_round
print "your opponent had {}\nYou {}!".format(computer_choice,["lost","tied","won"][this_round+1])
player_choice=raw_input("Play again? (or 'quit'): ")
print "you ended with a score of {}".format(score)
>>
>>51491211
>half of the thread dedicated to it
ebin
>>
>>51491211
>it's a stupid question because I don't know the answer
>>
>>51491226
count the posts and tell me if I'm wrong, retard
>>
>>51488714
>i've posted this question before and gave people a hard time about it
>it made me feel smart so i'll post it again!
>>
>>51491247
>count the posts
simply ebin
>>
>>51488714
c = (a + b) / 2
>>
>>51491261
>I didn't know the answer then
>I was too stupid to understand
>I don't know the answer now
>>
>>51491211
>>stupid question with no specification
>>people reply to the post, half of the thread dedicated to it

better than arguing about anime, at least its programming
>>
>>51491286
>look how smart i am
>i'm calling strangers on the internet stupid!!
>>
>>51491270
No workey.
>>
>>51491287
fegit, can you even into traps?
>>
>>51491287
true, but otoh, anime woul not even be a topic if mods did their job
>>
>>51491299
But that's literally how you average things.
>>
>>51491296
>no bully pls
>I'm scared of big dick playas
>>
>>51491222
and as another example, here's something that makes the function not care much about if you do something like change the names of the elements in the array (although the math of returning -1 or 1 if you win/lose breaks if you add more elements to the array)

import random

choices = {"rock":["scissors","paper"], # element: [what it beats, what beats it]
"paper":["rock","scissors"],
"scissors":["paper","rock"]
}

def play(player_choice,computer_choice):
if player_choice==computer_choice:
return 0
else:
return (choices[player_choice].index(computer_choice)*2)-1

score=0
player_choice=raw_input("Choose between 'rock', 'paper', and 'scissors': ")

while player_choice!="quit":
computer_choice = random.choice(choices.keys())
this_round=play(player_choice, computer_choice)
score+=this_round
print "your opponent had {}\nYou {}!".format(computer_choice,["lost","tied","won"][this_round+1])
player_choice=raw_input("Play again? (or 'quit'): ")
print "you ended with a score of {}".format(score)

>>
>>51491319
a+b can overflow
>>
>>51491319
Not in C.
>>
>>51491334
>look mom i told him i have a big dick!
>>
>>51491349
Who the fuck cares.
>>
>>51491349
depends on the language faggot
>>
>>51491351
Yes it is fuckface.
>>
>>51491361
>help mom, the big dick bully is here to rape my ass AGAIN
>>
Rate my strrev implementation.
char *strrev(char *str)
{
int length = strlen(str) - 1; /* omit \0 */
char *tmp = malloc(length);
int a = length;
int b = 0;
while (a >= 0 && b <= length)
{
strncpy(tmp+b, str+a, sizeof(char));
a--;
b++;
}
return tmp;
}
>>
>>51491398
You're wrong and you clearly don't know C. Please stay with js or return to your homescreen thread.
>>
>designing a language with fixed number of bits for integer values
>avoids defining what happens on overflow
>avoids defining how many bits it's supposed to be

C sucks
>>
>>51491374
well you will if you want to find the average of 1000 and INT_MAX and get the wrong answer :^)

>>51491380
are you a retard the question was "do you know how to average 2 ints in C"
>>
>>51491417
undefined-behavior/10
>>
>>51491425
who cares about that obsolete garbage of a language that C is?
>>
>>51491436
spotted the javascript cuk
web "developer" ahhahahahahah
>>
>>51491436
i do
>>
>>51491424
>waaaaaah, why am I stupid
>waaaah, I like css better
>mooooom, make them stop with their serious, big-boy, big-dick-playa languages!
>>
>>51491425
>"do you know how to average 2 ints in C"
double average = (((double) a) + ((double) b)) / 2.0;


It literally doesn't say that the result needs to be an int.
>>
File: pilki.jpg (20KB, 300x399px) Image search: [iqdb] [SauceNao] [Google]
pilki.jpg
20KB, 300x399px
So /dpt/, are you still a NEET who doesn't know a single functional language?
>>
>>51491491
>I'm a full-stack CSS developer
we know, kid, we know
>>
I finally have an idea.
How do I learn to write a program that does it?
>>
>>51491481
>finding language limitations retarded = being stupid
I program in C for a living, faggot. I just find the whole approach of avoiding defining things to be extremely limiting in 2015 when it made sense in the 70s.
>>
>>51491491
>the result can't be int because I don't know how to do it
>muh damage control
>muh backpedaling
>muh getting slapped over the face with a big dick
>>
>>51488714
int average(int x, int y)
{
return ( x & y)+( (x ^ y) >> 1);
}
>>
>>51491516
>>51491481
Are you mad you don't know how to average two integers or something?
>>
>>51491527
>claim to work with C
>I use C for no reason
>I don't understand performance implications
well memed, web fag
>>
>>51491520
Look up guide on how to make program do 'it'.
>>
NEW THREAD

>>51491564
>>
>>51491533
>the result can't be int because I don't know how to do it
Do you know what an average is?

If truncating the average, it's not the average...
>>
>>51491417
>int length = strlen(str) - 1;
>malloc(length)

what if strlen(str) is 0

>sizeof(char)

guaranteed to be 1, don't do this

>strncpy(<a>, <b>, 1)

the fuck are you doing man, do
*a = *b;


3/10
>>
>>51491540
Wrong.
>>
>>51491575
>C for no reason
I have a good reason

>performance implications
Wrong, anon. The concept of undefined behaviour is there because of PORTABILITY implications, not for performance.
>>
>>51491582
>muh CSS development
>>
>>51491605
>because of PORTABILITY implications, not for performance
Wrong.
>>
>>51491593
how so?
>>
>>51491627
If C was intended for high performance by design, it would be closely dependent on the underlying architecture.

However, it is not. It is by design a "portable assembly", meaning the reason why C is so popular is because it's so simple it is the first language that is implemented/ported to a new architecture.

Learn some history. Just because C compilers today churn your broken C into highly efficient byte code, doesn't mean it was like this.
>>
>>51491720
You're stupid, please stop posting if you can't think.
>>
>>51488345
Those who have and need the hardware don't give a fuck.
>mfw employer spends big bucks for a new cluster with hundreds of machines each with at least two cpus with 12 cores
>only C++ and Fortran
>the code squeezes everything out of each core with SIMD
>no functional programming in sight

Consumers also don't give a fuck. They want a good enough processor with long batterylife that doesn't get too hot when under load. This may change with VR but not today.

Web developers also don't give fucks otherwise they wouldn't be using php, ruby or python.

If you think the benefit of functional progamming is parallelism then you're just one of those reddit hipsters that bought into their meme conferences.
>>
>>51491424
>designing a language with fixed number of bits for integer values
You know, there's a reason for this.
>>
>>51491222
Even better. Make the computer decide whether the player wins, loses, or ties and use the look-up table to print the message instead of dealing all that string comparison bullshit.
local rps = {"rock", "paper", "scissors"}
local win = {"win", "tie", "lose"}

while true do
print("Please enter a number to represent your play, or 0 to quit")
for i = 1, #rps do
print(i..": "..rps[i])
end

local input
while true do
input = io.read("*n")
if not input or input < 0 or input > 3 then
print("bad input")
elseif input == 0 then
os.exit()
else
break
end
end
local play = math.random(3)

print(string.format("Computer played %s, you %s\n", rps[((input+play) % 3) + 1], win[play]))
end
>>
>>51491902
>You know
He doesn't. He's a fucking idiot that doesn't know what C is all about. He's most likely a "full-stack developer", aka javascript and css monkey.
>>
new thread?
>>
RETARDED FAGGOT DIDN'T LINK TO THE NEW THREAD

>>51491564
>>
>>51491937
>>51491222
>>51491341
>>51491167
wow cool
thanks for your responds
>>
>51489249
>0x2

you're such a faggot
>>
>>51489194
>This_Fucking_Style
trash
Thread posts: 324
Thread images: 18


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]
Please support this website by donating Bitcoins to 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
If a post contains copyrighted or illegal content, please click on that post's [Report] button and fill out a post removal request
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.
This is a 4chan archive - all of the content originated from that site. This means that 4Archive shows an archive of their content. If you need information for a Poster - contact them.