Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

MeruFM posted:

Also python owns
dynamic typingby default is a deadly sin

coffeetable fucked around with this message at 20:12 on Jan 18, 2014

Adbot
ADBOT LOVES YOU

MeruFM
Jul 27, 2010
List comprehension is neater than map reduce for anyone new

RICHUNCLEPENNYBAGS posted:

how are you tutoring people dude

iunno, please tell me the difference so I won't spread my misknowledge.

RICHUNCLEPENNYBAGS
Dec 21, 2010

MeruFM posted:

List comprehension is neater than map reduce for anyone new


iunno, please tell me the difference so I won't spread my misknowledge.

like a variable can be scoped to a block and not be accessible from outside that block or it can be globally scoped and be accessible from everywhere. idk you're the tutor you explain it.

FamDav
Mar 29, 2008
coffee table do you have a job yet

MeruFM
Jul 27, 2010

RICHUNCLEPENNYBAGS posted:

like a variable can be scoped to a block and not be accessible from outside that block or it can be globally scoped and be accessible from everywhere. idk you're the tutor you explain it.

Yeah that's scope. But what about visibility?

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

FamDav posted:

coffee table do you have a job yet
nope. finished my project work over christmas though, so will be throwin cvs to the wind when i get back from edinburgh at the end of the month

e: as much as it's lonely and goony as gently caress to be livin at home again as a grad, i kinda wish it could last longer because ive never been this productive in my life. six months straight of learning, day in day out. owns.

coffeetable fucked around with this message at 20:25 on Jan 18, 2014

RICHUNCLEPENNYBAGS
Dec 21, 2010

MeruFM posted:

Yeah that's scope. But what about visibility?

public, private, protected

MeruFM
Jul 27, 2010
I get it now. I thought scope covered both of those ideas and just called them all scope. Private scope, global scope, block scope.

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man
poo poo piss I thought my dumb bad code wasn't working and then I talked to atmel and their micro turned out to have a known bug that isn't documented unless you email them and they like you that won't be fixed until the changes go into the fab in like April


Now I have to do awful bullshit instead of just using the hardware uggghhh

Doc Block
Apr 15, 2003
Fun Shoe
it's OK man, i spent hours yesterday trying to get a dumb hd44780 LCD working with an ATmega32U4 and still haven't gotten the loving thing to work. i suspect it's my code, but hell if i could tell you why.

JewKiller 3000
Nov 28, 2006

by Lowtax

MeruFM posted:

I get it now. I thought scope covered both of those ideas and just called them all scope. Private scope, global scope, block scope.

the concepts are similar enough that your mistake is perfectly reasonable. imo the fact that you noticed how they're both parts of name resolution indicates a deeper understanding than simply being able to repeat the terminology that RICHUNCLEPENNYBAGS wanted

RICHUNCLEPENNYBAGS
Dec 21, 2010

JewKiller 3000 posted:

the concepts are similar enough that your mistake is perfectly reasonable. imo the fact that you noticed how they're both parts of name resolution indicates a deeper understanding than simply being able to repeat the terminology that RICHUNCLEPENNYBAGS wanted

it would be pretty wack if python had "no scoping"

MeruFM
Jul 27, 2010

JewKiller 3000 posted:

the concepts are similar enough that your mistake is perfectly reasonable. imo the fact that you noticed how they're both parts of name resolution indicates a deeper understanding than simply being able to repeat the terminology that RICHUNCLEPENNYBAGS wanted

thanks bro, i feel tingly and fulfilled now

Bloody
Mar 3, 2013

gucci void main posted:

or better yet, don't use python at all

Bloody
Mar 3, 2013

Doc Block posted:

it's OK man, i spent hours yesterday trying to get a dumb hd44780 LCD working with an ATmega32U4 and still haven't gotten the loving thing to work. i suspect it's my code, but hell if i could tell you why.

b/c its crap code

Doc Block
Apr 15, 2003
Fun Shoe

Bloody posted:

b/c its crap code

tested it with a different MCU and it worked fine (see my sig). the code may be crap, but at least it's working crap. bummer that the ATmega32U4 is probably trashed, though.

----------------
This thread brought to you by a tremendous dickhead!

~Coxy
Dec 9, 2003

R.I.P. Inter-OS Sass - b.2000AD d.2003AD

Mr SuperAwesome posted:

well yeah ofc we'd all use c# all the time if we could but sometimes u gotta fix someones lovely python project

or interface some ancient mining software with a scripting language that is literally perl to your nice WCF service

gonadic io
Feb 16, 2011

>>=
dumb c question #2: how does free manage to free whole arrays without the length being specified? i thought the point of, say, null terminators in strings was that you couldn't tell the length of the array??

i've been passing around the array length as a param along with the array itself, do i not need to do this?

Posting Principle
Dec 10, 2011

by Ralp
the allocator maintains its own metadata about allocations, because what it actually allocates may not always be exactly what you asked for

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison

AlsoD posted:

dumb c question #2: how does free manage to free whole arrays without the length being specified? i thought the point of, say, null terminators in strings was that you couldn't tell the length of the array??

i've been passing around the array length as a param along with the array itself, do i not need to do this?

terrible programmer answer - everything in c is essentially an implicit pointer, free removes the pointer to the array but not the data inside it.

you don't really need to pass the array length unless you need to know it for some reason

gonadic io
Feb 16, 2011

>>=
well here's a sample of what i've got:
code:
struct solution knapsack (int cap, int n, int* values, int* weights) {
	// assuming that len(values) == len (weights) == n
	int i = 0;

	// find the gcd of all of the weights: foldl' gcd cap weights
	int gcdWeights = cap;
	for (i = 0; i < n; i++) {
		// gcdWeights gcd= weights[i]; sadly doesn't work :(
		gcdWeights = gcd (gcdWeights, weights[i]);
	}

	// scale the weights by their gcd: map (/ gcdWeights) weights
	cap /= gcdWeights;
	for (i = 0; i < n; i++) {
		weights[i] /= gcdWeights;
	}
	[bunch more stuff here to actually compute a solution]
}
is this kinda the right thing to do? completely terrible? both? as you can see i'm basically coding my haskell solution in c which is probably not the most idiomatic way to go about it

Zombywuf
Mar 29, 2008

AlsoD posted:

well here's a sample of what i've got:
code:
struct solution knapsack (int cap, int n, int* values, int* weights) {
	// assuming that len(values) == len (weights) == n
	int i = 0;

	// find the gcd of all of the weights: foldl' gcd cap weights
	int gcdWeights = cap;
	for (i = 0; i < n; i++) {
		// gcdWeights gcd= weights[i]; sadly doesn't work :(
		gcdWeights = gcd (gcdWeights, weights[i]);
	}

	// scale the weights by their gcd: map (/ gcdWeights) weights
	cap /= gcdWeights;
	for (i = 0; i < n; i++) {
		weights[i] /= gcdWeights;
	}
	[bunch more stuff here to actually compute a solution]
}
is this kinda the right thing to do? completely terrible? both? as you can see i'm basically coding my haskell solution in c which is probably not the most idiomatic way to go about it

This is basically the right way to go about it. You might want to make a function called something like "scale_by_gcd" to break it up a bit but otherwise yeah, that's C.

You may also want to stick some asserts in to ensure the pointers are not null and n is > 0, going wild with asserts is never bad. If I'm being really picky n should be a size_t.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Zombywuf posted:

This is basically the right way to go about it. You might want to make a function called something like "scale_by_gcd" to break it up a bit but otherwise yeah, that's C.

You may also want to stick some asserts in to ensure the pointers are not null and n is > 0, going wild with asserts is never bad. If I'm being really picky n should be a size_t.

slap some restricts on those pointers and/or use c99 style
code:
knapsack(int cap, size_t n, int weights[restrict static n], int values[restrict static n]);
knapsack(int cap, size_t n, int *restrict weights, int  *restrict values);

Malcolm XML fucked around with this message at 20:03 on Jan 23, 2014

Pie Colony
Dec 8, 2006
I AM SUCH A FUCKUP THAT I CAN'T EVEN POST IN AN E/N THREAD I STARTED

AlsoD posted:

dumb c question #2: how does free manage to free whole arrays without the length being specified? i thought the point of, say, null terminators in strings was that you couldn't tell the length of the array??

i've been passing around the array length as a param along with the array itself, do i not need to do this?

well, when you requested space for the array with malloc or calloc, you requested N bytes total. malloc actually tries to find a block of size N + M, where M is the size of the malloc's header, which contains info about the size of the chunk and other stuff. malloc returns the address of whatever block it found + M, which to you looks like N bytes, and whenever you pass that block back to free, it can just subtract M from the address, read the header, and free however many bytes. at least thats how most allocators work

Zaxxon
Feb 14, 2004

Wir Tanzen Mekanik

Phobeste posted:

poo poo piss I thought my dumb bad code wasn't working and then I talked to atmel and their micro turned out to have a known bug that isn't documented unless you email them and they like you that won't be fixed until the changes go into the fab in like April


Now I have to do awful bullshit instead of just using the hardware uggghhh

which micro and what is the bug?

gonadic io
Feb 16, 2011

>>=
ahh why can't i just return a 4-tuple without having to mess around with defining a strut that'll only ever get used once

code:
populate_params(FILE_NAME, &cap, &n, &values, &weights); // in main

// defn:
void populate_params (char* file_name, int* pCap, int* pN, int** pValues, int** pWeights) {
this code was just inline in main and reads from the file to assign cap, n and the arrays. but i thought i'd be nice and abstract it a little but now it's turned into this shitheap where i have stuff like

code:
for (i = 0; i < *pN; i++) {
	fscanf(pFile, "%i %i\n", *pValues + i, *pWeights + i);
	assert (*pValues[i] >= 0 && *pWeights[i] > 0);
}
having previously malloc'd pValue and pWeights a few lines above. Does dereferencing those pointers a bunch make a difference or can the compiler notice that the pointers aren't changing and inline that poo poo for me?

also do i declare that string const?

gonadic io fucked around with this message at 02:18 on Jan 24, 2014

kitten emergency
Jan 13, 2008

get meow this wack-ass crystal prison
const all strings

FamDav
Mar 29, 2008
you either got const strings or const bitches

or something like that

FamDav
Mar 29, 2008
also always postpend const

char const * const supremacy

gonadic io
Feb 16, 2011

>>=
presumably this is a 20 year old argument but 'char* file_name' seems to make so much more sense than anything else to me. you have the type (pointer to char) and then the name. then you have stuff like 'int *p1, *p2' as opposed to 'int* p1, p2' as it should be imo. being a pointer is a property of the type, not the name!

but then, as established, i am a terrible and novice c programmer

Nomnom Cookie
Aug 30, 2009



more like a 40 year old argument

FamDav
Mar 29, 2008

AlsoD posted:

presumably this is a 20 year old argument but 'char* file_name' seems to make so much more sense than anything else to me. you have the type (pointer to char) and then the name. then you have stuff like 'int *p1, *p2' as opposed to 'int* p1, p2' as it should be imo. being a pointer is a property of the type, not the name!

but then, as established, i am a terrible and novice c programmer

AlsoD was right

coffeetable
Feb 5, 2006

TELL ME AGAIN HOW GREAT BRITAIN WOULD BE IF IT WAS RULED BY THE MERCILESS JACKBOOT OF PRINCE CHARLES

YES I DO TALK TO PLANTS ACTUALLY

AlsoD posted:

presumably this is a 20 year old argument but 'char* file_name' seems to make so much more sense than anything else to me. you have the type (pointer to char) and then the name. then you have stuff like 'int *p1, *p2' as opposed to 'int* p1, p2' as it should be imo. being a pointer is a property of the type, not the name!

int *p: p when dereferenced is an int
int* p: p is pointer to an int

i guess the former interpretation makes better sense than the latter if you learnt to program before types started showing up

FamDav
Mar 29, 2008
c was designed by a guy who wrote ed

Nomnom Cookie
Aug 30, 2009



tbd back then they thought a text editor came from the typing pool

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

AlsoD posted:

'int* p1, p2' as it should be

you may already know this but [given the thread we're in] that is actually legal c, it just won't result in what you think it will, which is why *p1 is the "standard"

if you write "int* p1, p2" p1 is an int* and p2 is an int

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
also maybe they made that less insane in c99 i haven't written c since 1995

JewKiller 3000
Nov 28, 2006

by Lowtax
of course they didn't, that would break backward compatibility

Innocent Bystander
May 8, 2007
Born in the LOLbarn.

Dessert Rose posted:

also maybe they made that less insane in c99 i haven't written c since 1995

Lets be honest, nobody has

Adbot
ADBOT LOVES YOU

Zaxxon
Feb 14, 2004

Wir Tanzen Mekanik

Dessert Rose posted:

also maybe they made that less insane in c99 i haven't written c since 1995

nope, you can still do everything the stupid way.

  • Locked thread