[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
/dpt/ - Daily Programming Thread
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 255
Thread images: 30
File: hime ACNL.png (778 KB, 400x720) Image search: [Google]
hime ACNL.png
778 KB, 400x720
old thread: >>54724576

What are you working on, /g/?
>>
>>54726833
killing myself.
>>
How do I pass values by reference in javascript?
>>
File: sucideKillYourself.jpg (314 KB, 1239x795) Image search: [Google]
sucideKillYourself.jpg
314 KB, 1239x795
>>54726864
I can help with that.
>>
File: 1463604502077.jpg (60 KB, 670x503) Image search: [Google]
1463604502077.jpg
60 KB, 670x503
>>54726864

Man only knows true peace after he has shuffle'd off this mortal coil.
>>
can the retarded java is pass by reference faggot please stay in the old thread thanks
>>
>>54726876
Consumer & disposable helium tanks are no longer 100% helium and won't work with this method, just FYI
>>
Can somebody help me understand this haskell code?
primes = 2 : primes'
where isPrime (p:ps) n = p*p > n || n `rem` p /= 0 && isPrime ps n
primes' = 3 : filter (isPrime primes') [5, 7 ..]

I don't understand how one can use primes' inside primes' definition, since it is an infinite list.
>>
Repost bc maybe someone has a clue why this prints 0 AND 1
#include <cmath>
#include <iostream>

int main()
{
double sineSignal;
double my_pi = 3.141592653589793;

sineSignal = std::sin(my_pi/2);
std::cout << "std::sin(my_pi/2):\t" << sineSignal << "\n";

sineSignal = std::sin(my_pi*(1/2));
std::cout << "std::sin(my_pi*(1/2):\t" << sineSignal << "\n";

return 0;
}


C++ sine is like Schrodingers Cat
>>
>>54726871
by not using javascript
>>
>>54726977

IEEE-754 b64.
>>
>>54726977
shouldn't the integers be doubles
>>
>>54726977
(1/2) returns an int due to them both being ints. Do (1/2.0), or (1/2.f) if you were working with floats.

>>54727016
Useless post. Filtered.
>>
>>54726977
I'm guessing 1/2 is integer division, so the result is 0. Try 1.0f/2.0f, for example.
>>
>>54727043
>i'm a retarded and i don't follow the standard IEEE convention

hello reddít
>>
>>54726870
>o != o[0].
>>54726967
>pass by reference syntax
using System;

class Program
{
static void foo(ref object o)
{
o = null;
}

static void Main()
{
object o = new object();
foo(ref o);
if (o == null) {
Console.WriteLine("passed by reference");
} else {
Console.WriteLine("passed by value");
}
}
}

inb4
>that's not pass by reference in C# because ref o != o
>why don't you compare "ref o" against "null" in Main
Please don't be this stupid.
>>
>>54727109
>strawman out of nowhere
Put your tripcode back on.

>>54727121
Fuck off with this shit already.
>>
>>54726936
Look at the C# "equivalent"
>>54726964
>>
>>54727134
>DELETE THIS
Out of arguments, my mentally impaired friend?
>>
>>54726880
Would you happen to have an un-fucked (compressed to hell) version of that picture; or be able to point me to the article?
>>
>>54726977
>double my_pi = 3.141592653589793;
double my_pi = acos (-1.0);
>>
>>54727134
>>strawman out of nowhere
>Put your tripcode back on.

I don't take it off to post niggers on /pol/, /s/, and /hc/, so I definitely wouldn't take it off here.

Get ahold of yourself.
>>
File: XamlFile.png (256 B, 16x16) Image search: [Google]
XamlFile.png
256 B, 16x16
How do I write an array of arrays in C?

>I don't want multidimensional array
>>
>>54727154

Reverse image search will get you to it.
>>
>>54727151
No, the shitposting should just stop with the last thread.

>>54727198
An array of arrays is a multidimensional array, a multidimensional array is not an array of arrays.
>>
>>54727198
they are the same thing
>>
>>54727222
>shitposting should just stop
Got told hard, eh?
>multidimensional array is not an array of arrays
Prepare your anus again!
>>
>>54727222
>No, the shitposting should just stop with the last thread.

Pass an array to a function, then. That's a reasonable simple thing to do, right?
>>
mercilessly rate my sort
import re

splitter = re.compile( r"\d+|\D+" )

def split_compare( a, b ):

longer, shorter, mult = ( a, b, 1 ) if len( a ) >= len( b ) else ( b, a, -1 )

for i in range( len( longer ) ):
if i >= len( shorter ):
return 1 * mult

try:
l_part = int( longer[ i ] )
s_part = int( shorter[ i ] )
except ValueError:
l_part = longer[ i ]
s_part = shorter[ i ]

if l_part > s_part:
return 1 * mult
elif l_part < s_part:
return -1 * mult

return 0

def smart_sort( *strings ):

presplit = [ ( s, splitter.findall( s ) ) for s in strings ]

return [ p[ 0 ] for p in sorted(
presplit,
cmp = lambda a, b : split_compare( a[ 1 ], b[ 1 ] )
) ]
>>
>>54727142
typedef struct trip {
Date date;
char destiny[MAX];
int seats_available, max_seats;
} Trip;

I got this struct, why can't I simply do destiny = "Paris"; ?
>>
>>54727044
>>54727043
>>54727027
>>54727016
Thanks, didn't knew that.

Interestingly, both equations return 1 when I change the compile settings in QtCreator to debug.
>>
>>54727222
>multidimensional array is not an array of arrays
why would you contradict the C standard? is your mouth itching for a big dick?
>>
>>54727258
>return 1 * mult
>return -1 * mult
>>
>>54727285
Because destiny is an array and not a pointer.
>>
>>54727305
inb4
>b-but arrays are pointers
>>
>>54727320
Not exactly.
>>
>>54727299
>[][][][][][][] == [][]
Go home.
>>
I swear, C is the only language that can offer so much entertainment with this crowd of retards! Keep it up!
>>
>>54727305
So, how can I make it work? Sorry for wrong quote I forgot to delete the quick reply
>>
>>54727333
not exactly inb4? how do you know?
>>
>>> from datetime import datetime
>>> t = datetime.utcnow()
>>> print t
>>> datetime.strptime(t, "%d-%m-%Y %H:%M:%S")
ValueError: unconverted data remains: .86400


Help
>>
>>54727349
You must do a for loop (or a mem copy) to copy the value of your static string to your array.
>>
>>54727341
nah, I'll stay and rape your fat stupid face :^)
>>
>>54727304
Yes I am lazy
>>
>>54727341
>dodging the questions
anon...
>>
>>54727352
arrays in C decade to pointers every time they get passed to a function.
>>
>>54727397
how do you know it's "not exactly inb4"?
>>
>>54727397
Or used as a pointer.
>>
>>54727333
>>54727397
That's what the >inb4 meant, dumbass
>>
>>54727397
>decade to pointers
the fuck is that?
>>
>>54727355
help
>>
>>54727397
>arrays
>every time they get passed to a function
you can't pass arrays to a function
>>
>>54727121
pass-by-reference cucks BTFO!
>>
>>54727043
>Useless post. Filtered.

That's true, I'm retarded and didn't read gud. To be fair, though, IEEE is the problem in 99% of "Why doesn't this float/double calculation do what I expect?" posts.
>>
>>54727433
>you can't pass arrays to a function

What a shitty language.
>>
>>54727359
Jesus christ all I want to do when creating a trip is
Trip paris;
paris.destiny = "Paris"';

And I apparently can't do that
>>
>>54727481
Is this C or C++? If it's C++, use std::string. If it's C, choose another language or resign yourself to strcpy_s()
>>
>>54727481
strncpy (paris.destiny, "Paris", MAX);

Maybe "Paris" must be allocated to a static variable.
>>
>>54727475
>I'm surprised by this
standard webshit, as usual
>>
>>54727475

I recall that Ritchie said the entire thing was a fucking mistake.
>>
>>54727481
try javascript, it was literally made for you
>>
>>54727506
I can't.
This C assignment I'm supposed to make a "travel agency" with linked lists, queues, reading from files etc...
It's supposed to be able to choose a trip, cancel it, if full then go to a queue, list all trips to a destiny list all trips by a client, etc etc...
>>
>>54727519

>C programmer
>can't take criticism

Enjoy your shrinking programming niche
>>
>>54727343
>what is c++
>>
File: Selection_025.png (2 KB, 497x126) Image search: [Google]
Selection_025.png
2 KB, 497x126
>>54726833
I was told by /g/ that my project was shit so now I am working on a CLI anime folder organizer written in C; pls do not judge
>>
>>54727551
>criticism
>from webshits
you're out of your element, "html programmer"
>>
>>54727570
But all this does is organize my anime folder?
>>
>>54727475
you didn't understand: YOU can't pass arrays to a function; competent programmers can
>>
>>54727544
Use strncpy http://en.cppreference.com/w/c/string/byte/strncpy the page is C++ but the function is available to C99 and later.
>>
>>54726833
>covert hime thread

I like this.

>>54726871
You don't.
There's literally no possible way to pass a reference to a variable.
>>
>>54727557
its shit
>>
>>54727509
Why the MAX?
>>
>>54727598
you shouldn't give advice if you don't know what the fuck you're talking about
>>
>>54727609
Its not ready yet, soon all you neckbeards will be organizing your anime folders automatically with my command line application
>>
>>54727570
>you're out of your element, "html programmer"

And you STILL can't pass an array to a function.
>>
>>54727622

What if you don't have any anime?
>>
>>54727613
lel k what's your solution?
I mean if all destinations are known at link time sure you can just reference them.
>>
>>54727622
What about my hentai? Does it look for tags?
>>
>>54727630
of course I can, see >>54727596 for more information
>>
>>54727596
>you didn't understand: YOU can't pass arrays to a function; competent programmers can

From Dennis Ritchie himself

" The rule, which survives in today's C, is that values of array type are converted, when they appear in expressions, into pointers to the first of the objects making up the array."

DENNIS FUCKING RITCHIE.
>>
>>54727622
Organinzing? Can you explain how it works? I'm actually kinda excited.
>>
>>54727640
memcpy
>>
>>54727622
What language is it in? C? Why not Python?
>>
>>54727610
To protect from writing outside of the array.
>>
>>54727649
yes, and you're not competent enough to figure out how to work around that
>>
>>54727557
Do "vec4(f_color, fade);"
>>
>>54727433
void PrintArray(int arrah[], int arrayElements) {
for(int i=0; i<arrayElements; i++) cout<<i<<"="<<arrah[i]<<"\n";
}

int main() {
int arrr[5]={1,2,3,4,5};

PrintArray(arrr, 5);
return 0;
}


Output:
1
2
3
4
5
>>
>>54727649
>DENNIS FUCKING RITCHIE.
he's a fag, err was*
>>
>>54727622
Do you have this on Github yet?
>>
>>54727660
How do you justify that above strncpy()?
>>
>>54727695
that's a pointer, donkey
>>
>>54727670
>yes, and you're not competent enough to figure out how to work around that

BTFO BY THE LANGUAGE DESIGNER HIMSELF.

work around to pass an array to a function, my fucking sides.

As I said you can't pass an array to a function in C, and Dennis Ritchie agrees with me.
>>
>>54727707
it doesn't do stupid shit
>>
What are you guys going to do when blo-lang (aka Jai) revolutionizes programming?

Stroustrup, Gosling, Rossum, Hejlsberg, and Alexandrescu are planning a group suicide for its release day.
>>
>>54727258
bampu~
>>
>>54727724
>agrees with me
he agrees that stupid shits should stick to css
>>
File: the end of code.png (133 KB, 600x786) Image search: [Google]
the end of code.png
133 KB, 600x786
>programming
>2016

Programming is being made obsolete under your feet and you crusty C autists are too stupid to realize it.

Nobody will be programming in 20 years from now, software development is over.
>>
File: feels.jpg (14 KB, 498x302) Image search: [Google]
feels.jpg
14 KB, 498x302
>>54727633
>>54727647
>>54727651
>>54727668
>>54727678
>>54727705
I am sorry guys this was a joke, I said that /g/ would not make fun of my project if it were a "command line application written in C" and the people I was talking to said /g/ was not this way. So while I am sorry I got you guys excited for nothing, I know have evidence that /g/ would not make fun of my project if it were an anime organizer and for that I thank you.
>>
>>54727695
You passed a pointer and a length. That's differente tha an array, and it's retarded.
You should be able to do something like this.

void PrintArray(int arrah[]) {
for(int i=0; i< length(arrah) ; i++) {
printf ("This would the element %d of a true array, if C were a sane language", arrah[i]);
}
}
>>
>>54727779
(¯―¯٥)
>>
>>54727733
>revolutionizes programming?
sure, just like Go :^)
>>
>>54727779
fag
>>
File: button.png (666 KB, 960x540) Image search: [Google]
button.png
666 KB, 960x540
>>54727838
>>54727852
I am sorry comrades.
>>
>>54727835
>void PrintArray(int arrah[]) {
>int arrah[]
that's a pointer
>length(arrah)
length of pointer?
>>
>>54727760
So butthurt dunning kruger programmer.
>>
>>54727861
Nothing to be sorry for, I don't even care for your initial post, you just sound like a colossal faggot, that's all.
>>
>>54727864
>length of pointer?

This is a fictional variant of C, in which you could pass arrays as arrays, and not as pointers.
>>
>>54727886
It is okay, let out your anger that your anime folder will not be organized
>>
>>54727899
Jokes on you, I've no anime folder :)
>>
>>54726927

As long as they don't contain oxygen they will work fine. Easier to just use a nitrogen tank anyway.
>>
>>54727892
implement it
>>
>>54727909
We are the only two on /g/ who fall under this category
>>
>>54726871
Using arrays, or by declaring the variable outside the function.
>>
File: 1438911932341.gif (433 KB, 500x275) Image search: [Google]
1438911932341.gif
433 KB, 500x275
>>54727779
You had me so excited for the chance to organize my anime. My life had meaning again.
What am I supposed to do now?
>>
>>54727934
That and the people who have bookmarks instead of downloading it all. ;D
>>
>>54727953
I mean I guess we could start one if others are interested
>>
>>54727869
>butthurt
I'm not the one mad about arrays :^)
>>
>>54727912
They're purposefully doped with carbon dioxide (CO2), oxygen (O2), or a denaturing agent like Benzene, to discourage use as an exit bag.

Try it today with any tank.
You will feel like shit right away and have the instinctive urge to breathe or get fresh air, just like using a closed O2 bag.
>>
File: akari bleh.jpg (43 KB, 690x460) Image search: [Google]
akari bleh.jpg
43 KB, 690x460
function multiplier(factor) {
return function(number) {
return number * factor;
};
}
var twice = multiplier(2);
console.log(twice(5));


Why do closures exist?
Why not just
var multiply = function(number, factor) {
return number * factor;
};
console.log(multiply(5, 2));
>>
>>54727983
>Why
masturbation purposes
>>
>>54727975

Lab grade nitrogen is easy enough to get hold of though and that contained no oxygen.
>>
>>54727923
I think it's called Pascal
>>
>>54728006
>Pascal
>variant of C
I think you're retarded
>>
>>54727969
>I'm not the one mad about arrays :^)

I think I'm the one who is right. Prove me wrong.
Pass an array to a function, I'll wait.
>>
>>54727983
For when you need a function that multiples by two and a function that multiplies by five but don't want to define two nearly identical functions.
>>
>>54728016
Semantically wise it's a sane C (the non OOP variants).
>>
>>54728028
>so mad
>>
>>54728050
>it's a sane C
yeah, that's why everyone uses it
>>
>>54728043
javascript already supports anonymous function literals so you can write any one-off local inline functions you could want, why would you want to mix functions and data by creating functions that return functions?

It just seems completely backwards to me.
>>
>>54728059
>so provably wrong

Stay mad C cuck
>>
>>54728050

It's the other way around, C is an insane Pascal.
>>
File: 1436897789045.png (287 KB, 723x664) Image search: [Google]
1436897789045.png
287 KB, 723x664
>>54727983
> mfw I have some coworkers that love to have functions that return functions

I'm okay with jabascript, but thing kills me a little everytime I see it.
>>
File: wow, that's nice.png (32 KB, 1056x183) Image search: [Google]
wow, that's nice.png
32 KB, 1056x183
You guys didn't tell me how nice object-oriented programming is in Lua. I was struggling with the idea that "best practice" in C++ is creating a separate header file.

This language is intuitive.
>>
>>54728120
What's wrong with it? It seems like a cool idea to me. I'm sure they have good reasons to use it, right?
>>
>>54728081
>yeah, that's why everyone uses it

C is pleb
Pascal is patrician
>>
>>54727983
it called currying, can be pretty useful if used right.
>>
>>54728110
>It's the other way around, C is an insane Pascal.

That's a better way to put it, actually
>>
>>54728083
To avoid repeating yourself.
>>
>>54728127
You're not even using Lua OOP at all, just a simple table. To achieve actual OOP you have to use meta tables.
>>
>>54728083
>why would you want to mix functions and data by creating functions that return functions?

>object-oriented programmers actually believe this
>>
>>54728107
>muh html
>>
>>54728188
Lisp, masterrace you cuck.
If you don't know metaprogramming you'll be dead in 10 years.
>>
>>54728171
Oh, I didn't realize that. I'm just looking at the Lua documentation, which is paginated; I didn't realize there was more to it!
>>
>>54728130
yeah, that's why everyone uses it
>>
>>54728206
>List
>masterrace
>dead since the 70s
top cuck
>>
>>54728260
>yeah, that's why everyone uses it
>He doesn't know he's a sitting duck

Have fun debugging legacy applications, because that's your future.
>>
File: Asobi Seksu - Citrus.jpg (44 KB, 640x480) Image search: [Google]
Asobi Seksu - Citrus.jpg
44 KB, 640x480
Hey /dpt/, post yfw he uses a C array in C++
>>
>>54728273
You are right, it's dead :^)
>>
File: Gotta_go_fast.png (129 KB, 678x678) Image search: [Google]
Gotta_go_fast.png
129 KB, 678x678
>>54728289
>>
Which is the superior power function?
99% of programmers will get this wrong!

function power(base, exp) {
if (!exp)
return 1;
else
return base * power(base, exp - 1);
}

function pow(base, exp) {
var result = 1
while (exp--)
result *= base;
return result;
}
>>
>>54728279
>Pascal
>debugging legacy applications
what?
>>
>>54728157

It's also more specific because Pascal predates C and it's [spoiler]better[/spoiler]
>>
>>54728292
we know
>>
>>54728330
>better
yeah, that's why everyone's using it
>>
>>54728325
I'd say 2 but statistically that's probably wrong.
>the real answer is whatever's in your language's standard library
>>
>>54728325
both of these are shit and O(n)
exponentiation should be O(logn)
read your fucking SICP
>>
>>54728325
Directly from the bible

(define (fast-expt b n)
(cond ((= n 0) 1)
((even? n) (square (fast-expt b (/ n 2))))
(else (* b (fast-expt b (- n 1))))))
>>
>>54728346
>yeah, that's why everyone's using it

Not an argument
>>
>>54728346

The fact that people still use C is a historical accident.
>>
>>54728349
>>54728325
Wait a sec.
while (exp--)

You got me.
>>
>>54728371
>totally better
sure
>>
>>54726871
you have to wrap it in an object
function whatever(kys)
{
kys.val += 10;
}
var num = { val: 104 };
whatever(num);
console.log(num.val); //114


welcome to the wonderful world of javascript
>>
>>54728380
yeah, so sad, they should be using javascript
>>
>>54728387
C is what makes the botnet possible.
You are just a CIA nigger.
>>
ITT: cucks rekt on C topics blame the grapes for being sour tho
>>
>>54728402
They should be using non retarded systems languages, m8.
>>
>>54728409
yeah, javascript is the tits tho, amirite, webshit?
>>
>>54728419
You mean C cucks Rekt by Dennis Ritchie himself?
>>
>>54728428
yeah, Go is great, isn't it, cssboy?
>>
>>54728385
What's wrong with while(exp--)?
I just tested it and it seems to work fine.
>>
>>54728440
>so sour
I know, right? dat pascal tho...
>>
File: 1460198219741.jpg (12 KB, 292x282) Image search: [Google]
1460198219741.jpg
12 KB, 292x282
why does everything on /g/ devolve into a shitfest? Are people really that defensive/autistic about their preferred language?
>>
>>54728458
pow(5, 0) = ?
>>
>>54728402

Clipper was the Jesus of programming languages and we never even knew it until it was too late, and it was gone.
>>
>>54728448
>yeah, Go is great, isn't it, cssboy?

Go is not a systems language, despite some claims, you fucking CIA nigger.
>>
>>54726833
How do I scale images in C/OpenGL?
>>
>>54728485
should return 1
>>
Why does haskell, a purely function language that shuns mutability, store programs in mutable character arrays?
>>
>>54728526
Haskell is not pure.
>>
>>54728469
BTFO by Ritchie himself

>Dat damage control
>>
>>54728510
You're right, I'm dumb. Still, the real answer is
import math
etc.
>>
>>54728492
>Go is not a systems language
no way!
>>
>>54728551
>muh html tho
>>
>>54728562
Marketing, amirite?
>>
>>54726876
there's easier way, Just try to find a logic behind PHP
>>54727258
>python
absolutely savage
>>
File: i hate this job.jpg (83 KB, 691x621) Image search: [Google]
i hate this job.jpg
83 KB, 691x621
This javascript book is kinda shit.
I'm only 10 pages in, and it's asking how I would write an "isEven" function recursively.

Are all javascript books like this?
It's self-masturbatory as fuck.
>>
>>54728547
It tries to be
>>
How would I make a list from 1 to a sqrt of a number in haskell? I've tried but I always end up with a type error.
listSqrt x = [1..(round . sqrt . fromIntegral x)]

is what I tried, but throws werid type errors
>>
google image search "sicp"
>>
>>54728762
>cp
how bout no
>>
>>54728485
>asking how I would write an "isEven" function recursively.
Top kek
>>
is this a valid use of javascript?
it sorta seems to use C strings, but at the same time, it sorta does't.
function strlen(str)
{
var len = 0;
while (str[len++]);
return len - 1;
}
>>
>>54726876
>tfw you need $101 just to have a peaceful death
>>
string_length = None
def strlen( string ):
global string_length
if string_length == None:
string_length = 0
try:
string[ string_length ]
string_length += 1
except IndexError:
string_length -= 1
string_length_to_return = string_length
string_length = None
return string_length_to_return
return strlen( string )
>>
>come back to /dpt/
>javascript everywhere
What the fucking hell happened?

>>54728734
What errors?
>>
Holy fuck, you guys are fucking retarded.
Just please STOP talking about C. You have absolutely no fucking idea what you are talking about, do NOT talk about languages that you don't know.

Please just go back to talking about Java and C# on reddit.
>>
>>54728932
Are the Java shitters at least gone?
>>
>>54728942
>implying to know C
anon...
>>
>>54728885
It's a lot easier if you have a car.
>>
File: anime girl reading sicp.png (655 KB, 777x1028) Image search: [Google]
anime girl reading sicp.png
655 KB, 777x1028
r8 oc
>>
>>54728653
isEven 0 = true
isEven x = not . isEven $ x-1
>>
>>54728971
This is not the right thread for this.
Just because she has a book, doesn't mean it's ok.
>>
>>54728971
Terrible editing 6/10
>>
I'm trying to learn C, but I'm finding the K&R book to be increasingly difficult (and maybe old?), what books do you recommend for me (that's my first programming language, and I don't want to learn something else instead)?
>>
>>54728942
>You have absolutely no fucking idea what you are talking about,

Maybe it's you the one who has no idea.

> I know better than the language designer himself

Of course, anon, of course you do.
>>
>>54728942
>Just please STOP talking about C. You have absolutely no fucking idea what you are talking about, do NOT talk about languages that you don't know.

I hope you have the spec memorized.
>>
File: php masterrace.png (2 MB, 1598x1585) Image search: [Google]
php masterrace.png
2 MB, 1598x1585
>>54729006
>>
>>54728971
10/10 new /dpt/ OP image confirmed
>>
>>54729009
There's only one book anyone needs to read about C

the standard, dumbass

we will fucking test you on it too
>>
>>54729009
>he fell for the meme
at least learn python or something first geez
>>
>>54728971
It's shit.
>>
>>54729009
>I'm trying to learn C, but I'm finding the K&R book to be increasingly difficult (and maybe old?),what books do you recommend for me (that's my first programming language, and I don't want to learn something else instead)?

C programming a modern approach is better.
But if you are learning programiming for the first time, you should learn the concepts first, and then the language.
I don't know if starting with C is a good idea though.
>>
>>54729009
K&R is written for programmers who already know how to program, not kids who don't know what a for loop is.
Go read something else then come back.
>>
>>54729042
It's not even freely available, and it's meant as a reference for those who are already experienced, and just want to... idk, make a C compiler or something

>>54729048
No can do.
>>
>>54729067
I'm not an absolute beginner, I know what a for loop is.
>>
(define (iter x) (begin (display x) (display " ") (cond (< x 10) (iter (+ x 1)))))

what am i doing wrong? it just prints 1 and 10 when i do (iter 1) but i want it to print every number between 1 and 10
>>
File: klinefelter frog feels.jpg (26 KB, 450x500) Image search: [Google]
klinefelter frog feels.jpg
26 KB, 450x500
>>54729051
that criticism isn't very constructive
>>
>>54729088
Then you should have no problem with K&R.
Where are you having trouble, anon?
>>
>>54729100
>what am i doing wrong?
using a dead language
>>
>>54729110
nothing good can be constructed from what you did
>>
>>54729117
I found myself unable to do the exercices in the book. I did the ones on the first few chapters, but then I couldn't.
>>
File: indian inspiration.jpg (21 KB, 508x440) Image search: [Google]
indian inspiration.jpg
21 KB, 508x440
>>54729121
>>
>>54728853
Since Javascript permits that syntax for accessing individual characters in Javascript strings, why wouldn't be okay? It has nothing to do with C strings.
It's a bit weird not to just to str.length, though.
>>
>>54729144
>a student of life
>>
>>54729143
Post the exercise.
>>
>>54729100
this works

(define (iter x)
(if (<= x 10)
(cons x (iter (+ x 1)))
'()))
>>
>>54729179
it's to make a program in scheme that counts from 1 to 10. this is what i have so far >>54729100
>>
>>54729182
i want it to print it though. i want output to be 1 2 3 4 5 6 7 8 9 10 if you do (iter 1)
>>
>>54729190
I thought you were doing K&R?
>>
So /dpt/, what's your current project?
>>
>>54729179
Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number
of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns.
Should n be a variable or a symbolic parameter?
Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum
number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab .
When either a tab or a single blank would suffice to reach a tab stop, which should be given
preference?
Exercise 1-22. Write a program to ``fold'
'long input lines into two or more shorter lines after
the last non-blank character that occurs before the n-th column of input. Make sure your
program does something intelligent with very long lines, and if there are no blanks or tabs
before the specified column.
Exercise 1-23. Write a program to remove all comments from a C program. Don'
t forget to
handle quoted strings and character constants properly. C comments don'
t nest.
Exercise 1-24. Write a program to check a C program for rudimentary syntax errors like
unmatched parentheses, brackets and braces. Don'
t forget about quotes, both single and
double, escape sequences, and comments. (This program is hard if you do it in full
generality.)
>>
>>54729211
it does on my REPL
otherwise I think you could do a (display (iter 1))
>>
>>54729100
You're using a begin, for one thing. It's not necessary when you're creating a new procedure.

Also, the problem is the cond. A cond form looks like
(cond ((predicate0?) ...)
...)

You need all those parentheses, which you were missing. Consider using when if there's only one condition you're testing for.
>>
>>54729245
really?
this is just playing with C strings
if (str[i] == '\t') fprintf(stdout, "     ");
>>
>>54729262
thanks. i keep fucking up cond and i keep trying to memorize the right form and think i get it
>>54729247
idk i'm just doing fib now, i know its creating the recursion its just not printing all the info
>>
>>54729242
Android app for erowid.org
>>
>>54728400
manigga
> I salute you comrade SR JS dev
>>
>>54729211
This is what you want I think

(define (iter x)
(if(<= x 10)
(begin
(display x)
(display " ")
(iter (+ x 1)))
(newline)))

But now you have to generalize it.
Write a function (count-iter start end) that counts from start to end.

(count 2 7) should print

2 3 4 5 6 7
>>
>>54729268
fprintf hasn't been discussed up to this point.
for (i = 0; i < TABSTOP; i++) {
printf(" ");
}
>>
File: 1454286304783.jpg (31 KB, 680x365) Image search: [Google]
1454286304783.jpg
31 KB, 680x365
>>54726999
>>54727606
>>54727945
Pahjeet, Sandeep and Kishore
>>
>>54729242
SDL/Qt "competitor"
>>
>>54729291
>idk i'm just doing fib now, i know its creating the recursion its just not printing all the info

You are doing the SICP exercises by any chance? Don't worry about the display function right now, if you cons a list it automatically appears on the REPL.
>>
Is R a meme or is it actually useful for data profiling/mining/stats?
>>
>>54729367
i'm not doing an exercise right now. right now i'm just trying to do some simple programs in scheme to get the hang of it
>>
 (define (fibIter sum current target) (if (< current sum) (fibIter (+ sum current) (+ current 1) target) (sum)))

(define (fib x) (fibIter 1 1 x))


what am i doing wrong? it tells me

application: not a procedure;
expected a procedure that can be applied to arguments
given: 1
arguments...: [none]
>>
>>54729458
That last (sum) is actually the number 1. And 1 is a literal, not a procedure.
>>
>>54729458
You are invoking sum, sum it's not a proceure, drop the parentheses
>>
>>54729505
what do i do instead then? i want it to return it
>>
>>54729505
>>54729517
removing the parenthesis fixed it (still got a problem where its return 1 always but im sure thats just me being stupid) but i'm not sure why
>>
File: code goblin 4.png (18 KB, 898x410) Image search: [Google]
code goblin 4.png
18 KB, 898x410
i don't get what's happening
>>
>>54729684
how is fibiter iteration if it's called within itself?
>>
>>54729715
how isn't it?
>>
>>54729684
I think you need memoization for iterative fibonacci. Iterative fibonacci is less straightforward than the recursive one. Have you tried writing the recursive before?
>>
File: 1424846396553.jpg (65 KB, 665x662) Image search: [Google]
1424846396553.jpg
65 KB, 665x662
>code logistics
>>
>>54729715
what
>>
>>54729722
because it's recursion?
Thread replies: 255
Thread images: 30

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

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.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.