Coding: Fleeting Thoughts

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Coding: Fleeting Thoughts

Postby phlip » Tue Jul 15, 2014 10:27 am UTC

... Huh. And here I thought M_PI was a standard part of <math.h>, but looking at the C99 spec, it's not in there. I guess it's just a common nonstandard addition.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
Jplus
Posts: 1721
Joined: Wed Apr 21, 2010 12:29 pm UTC
Location: Netherlands

Re: Coding: Fleeting Thoughts

Postby Jplus » Tue Jul 15, 2014 10:36 am UTC

Yes. Sometimes you may need to define _USE_MATH_DEFINES before including <cmath>.
"There are only two hard problems in computer science: cache coherence, naming things, and off-by-one errors." (Phil Karlton and Leon Bambrick)

coding and xkcd combined

(Julian/Julian's)

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: Coding: Fleeting Thoughts

Postby PM 2Ring » Tue Jul 15, 2014 12:38 pm UTC

EvanED wrote:I do pretty strongly think that *(x + y) notation instead of x[y] is rarely appropriate.

What he said.
Fancy wrote:
Yakk wrote:Write code better than your project deserves, so you become ready for a better project. :)

You saying this makes me feel bad, but if I change now all of code will have to be changed for consistency.

We're not trying to make you feel bad, we just want you to develop good coding habits now, while you're still fresh to the art of computer programming, both for your own sake, and for the sake of other people who read your code. Trying to break bad coding habits later down the track can be a lot harder.


Fancy wrote:A fleeting thought addendum
Trying to learn C is really frustrating me and making me feel like an absolute idiot, and I don't want to clog up these forums with my requests for help, but I have no where else to go...

I mean, I feel like an absolute imbecile for this. This is such basic stuff, and yet it doesn't work. The book teaches us with these nice, slow examples that make sense, and then BAM the end of chapter problems are virtually impossible.


Don't feel like an imbecile, what you're going through is pretty normal. Some of us here have been programming for decades, but we remember the confusion & frustration of our early days. And we get a "friendly" reminder of that whenever we embark on learning a new language. :) Don't be afraid to ask for help here, or of asking "dumb" questions: we're here to help.

Sure, part of the reason we hang around here is that we might pick up some neat programming trick, or read an amusing programming-related anecdote, but a big motivation is that we like to share what we know, and we enjoy giving others the benefit of our skills and experience. It's very rewarding to see new programmers gradually begin to understand what programming is all about and to see them break through the confusion & frustration and start turning out succinct, efficient code. But don't expect to become a top-flight programmer overnight. Like any worthwhile skill it takes time to develop.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 15, 2014 1:30 pm UTC

Diadem wrote:Some heroic previous developer apparently figured out that pi is equal to 4.0 * atan(1.0). This happy fact of course had to be shared with the world.

Yeah, that's annoying.
The corrrect way is obviously pi = atan2(-1, 0).
Image

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 1:52 pm UTC

PM 2Ring wrote:
EvanED wrote:I do pretty strongly think that *(x + y) notation instead of x[y] is rarely appropriate.

What he said.
Fancy wrote:

We're not trying to make you feel bad, we just want you to develop good coding habits now, while you're still fresh to the art of computer programming, both for your own sake, and for the sake of other people who read your code. Trying to break bad coding habits later down the track can be a lot harder.


Fancy wrote:A fleeting thought addendum
Trying to learn C is really frustrating me and making me feel like an absolute idiot, and I don't want to clog up these forums with my requests for help, but I have no where else to go...


Sure, part of the reason we hang around here is that we might pick up some neat programming trick, or read an amusing programming-related anecdote, but a big motivation is that we like to share what we know, and we enjoy giving others the benefit of our skills and experience. It's very rewarding to see new programmers gradually begin to understand what programming is all about and to see them break through the confusion & frustration and start turning out succinct, efficient code. But don't expect to become a top-flight programmer overnight. Like any worthwhile skill it takes time to develop.
[/quote]
I'm demotivateed by others sometimes. You see these news stories about people who are age 12 and rocking it out with the programming.
Fun fact:Some of the Arch Linux devs were born in the early nineties. That points them in their early twenties, I'm 17. These guys write arch and I'm just sitting around confused over how some of the most basic things work.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
Sizik
Posts: 1251
Joined: Wed Aug 27, 2008 3:48 am UTC

Re: Coding: Fleeting Thoughts

Postby Sizik » Tue Jul 15, 2014 2:09 pm UTC

Xenomortis wrote:
Fancy wrote:"Write a function that finds the first word in an input line, ingoring all preceding whitespace, and have it store it in an array."
What the hell is this?

I can guarantee that if I tried to implement this in C, I'd fuck up in some way and get some mysterious error.
I can do it in Python trivially (although I had to look up how to read from stdin).

Edit:
Yeah, I corrupted something and my code printed some random ASCII character...


Took a stab at it.

Spoiler:
Only thing that didn't work first try was assigning the output of realloc to a char array.

Code: Select all

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    // char buffer
    //char arr[8];
    char* arr = (char*) malloc(* sizeof(char));
    int len = 8;
    char c;
    
    
// skip whitespace
    for(= getchar(); isspace(c); c = getchar()){}
    
    
// first non-ws char.
    arr[0] = c;
    int i = 1;
    
    
// get chars until first ws char.
    for(= getchar(); !isspace(c); c = getchar(), i++){
        // resize array if needed
        if(>= len)
        {
            arr = (char*) realloc(arr, (len + 8) * sizeof(char));
            len += 8;            
        
}
        arr[i] = c;
    }
    
    
// terminate string
    arr[i] = '\0';
    printf("%s", arr);
    return 0;    
} 
she/they
gmalivuk wrote:
King Author wrote:If space (rather, distance) is an illusion, it'd be possible for one meta-me to experience both body's sensory inputs.
Yes. And if wishes were horses, wishing wells would fill up very quickly with drowned horses.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 15, 2014 2:25 pm UTC

Fancy wrote:I'm demotivateed by others sometimes. You see these news stories about people who are age 12 and rocking it out with the programming.
Fun fact:Some of the Arch Linux devs were born in the early nineties. That points them in their early twenties, I'm 17. These guys write arch and I'm just sitting around confused over how some of the most basic things work.

Those 12 year olds probably have some guidance and almost certainly aren't writing in C.

Look, programming is hard.
And C is a hard language; unless you're practiced or have incredible patience, even the most basic of tasks can be intensely frustrating - it does very little to help you, and without some know how, it can be infuriating to debug.

Those 20-odd year old Arch Linux devs?
These are people that have been programming for years; they love it, it's their life.
You're 17 and I'm guessing you've not been programming for long - you cannot compete with those guys yet.

But hey, you're 17 now - if you enjoy this and keep at it (and you should only continue if you do enjoy programming), by the time you're my age (23), you'd have been programming for 6 years!
For one thing; that's three times the amount of programming experience I have, and if it's in a language like C, you could find yourself working on something like Arch.

Above all, do not let others rob you of any feelings of success or joy when you solve something, even if it's trivial.
Everyone makes mistakes, or gets stuck on something that "feels obvious", and we all feel satisfaction when we finally solve it.
Just because someone spots the solution instantly when you recount the tale; do not feel bad for getting stuck.



Sizik wrote:Took a stab at it.

Spoiler:
Only thing that didn't work first try was assigning the output of realloc to a char array.

Code: Select all

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
    // char buffer
    //char arr[8];
    char* arr = (char*) malloc(* sizeof(char));
    int len = 8;
    char c;
    
    
// skip whitespace
    for(= getchar(); isspace(c); c = getchar()){}
    
    
// first non-ws char.
    arr[0] = c;
    int i = 1;
    
    
// get chars until first ws char.
    for(= getchar(); !isspace(c); c = getchar(), i++){
        // resize array if needed
        if(>= len)
        {
            arr = (char*) realloc(arr, (len + 8) * sizeof(char));
            len += 8;            
        
}
        arr[i] = c;
    }
    
    
// terminate string
    arr[i] = '\0';
    printf("%s", arr);
    return 0;    
}  


Spoiler:

Code: Select all

#include <stdio.h>

void getFirstWord( int length, char* in, char* out )
{
   int i = 0;
   while ( i++ < length && *in && *in == ' ' ) in++;
   while ( (*in) != ' ' && *in ) {
      *out++ = *in++;
   }
   return;
}

int main()
{
   char in[128];
   char out[128];
   printf( "Enter input string: " );
   gets( in );
   getFirstWord( 128, in, out );
   printf( "%s\n", out );
   return 0;
}
 

The mistake I made earlier?
Used scanf( "%s", in ); instead of gets( in );
Seriously, fuck C.
Ok, I made a pointer error in getFirstWord, but I realised it shortly after compiling (incremented "in" inside the test, cutting off the first letter of the word to be retrieved).

Spoiler:
Yes, I use gets() to fill a stack allocated buffer, yes I'm an evil person.
Image

User avatar
Diadem
Posts: 5654
Joined: Wed Jun 11, 2008 11:03 am UTC
Location: The Netherlands

Re: Coding: Fleeting Thoughts

Postby Diadem » Tue Jul 15, 2014 2:34 pm UTC

Xenomortis wrote:
Diadem wrote:Some heroic previous developer apparently figured out that pi is equal to 4.0 * atan(1.0). This happy fact of course had to be shared with the world.

Yeah, that's annoying.
The corrrect way is obviously pi = atan2(-1, 0).

Assuming you mean 4*atan2(-1,0), then don't worry, that one occurs as well. And we shouldn't forget 4*atan2(1,1).

And did I mention the endless variants of pi? There is two_pi and twopi and Twopi and TwoPi and Two_pi. I'm not kidding, all of those happen, some several times. Then there are there really weird ones where pi2 is defined as pi*pi in one file and 2*pi in another. Of course for pi*pi there are 3 other constants as well (pi_pi, pi_sqr and pi_2).

There are literally (and I mean literally literally, not figuratively) hundreds of lines of codes dedicated to defining pi or variants of pi. It's impressive.
It's one of those irregular verbs, isn't it? I have an independent mind, you are an eccentric, he is round the twist
- Bernard Woolley in Yes, Prime Minister

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 15, 2014 2:41 pm UTC

Actually I meant atan2(0, -1).
It's "preferable" because you don't need to multiply by 4.

atan2(-1, 0) = - pi / 2
Hey, that could be a useful #define for your team!
Image

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 15, 2014 2:45 pm UTC

Code: Select all

#ifndef _ASTROPHYSICS_H
#define _ASTROPHYSICS_H

//snip

#define PI 1

//snip

#endif
Summum ius, summa iniuria.

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 4:35 pm UTC

Xenomortis wrote:
Fancy wrote:I'm demotivateed by others sometimes.

Those 12 year olds probably have some guidance and almost certainly aren't writing in C.

Look, programming is hard.
And C is a hard language; unless you're practiced or have incredible patience, even the most basic of tasks can be intensely frustrating - it does very little to help you, and without some know how, it can be infuriating to debug.

Those 20-odd year old Arch Linux devs?
These are people that have been programming for years; they love it, it's their life.
You're 17 and I'm guessing you've not been programming for long - you cannot compete with those guys yet.

But hey, you're 17 now - if you enjoy this and keep at it (and you should only continue if you do enjoy programming), by the time you're my age (23), you'd have been programming for 6 years!
For one thing; that's three times the amount of programming experience I have, and if it's in a language like C, you could find yourself working on something like Arch.

Above all, do not let others rob you of any feelings of success or joy when you solve something, even if it's trivial.
Everyone makes mistakes, or gets stuck on something that "feels obvious", and we all feel satisfaction when we finally solve it.
Just because someone spots the solution instantly when you recount the tale; do not feel bad for getting stuck.


Another huge problem I find is I don't know where to start. I can't think of any cool porjects to do, and every time I start a book, I hop to another book halfway through. I've tried K&R, Learn C The Hard Way, C: How to Progrem, C Programming: A Modern Approach...the list is infinite. I've seen some bad reviews for many of these, too. "K&R is outdate" "LCTHW isn't teaching right" along with the accompanying "K&R is god" "LCTHW is excellent"

Right now I'm starting Ivor Horton's "Beginning C" because it looks good.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 4:40 pm UTC

Is there a reason you want to learn C in particular?
He/Him/His/Alex
God damn these electric sex pants!

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 4:54 pm UTC

ahammel wrote:Is there a reason you want to learn C in particular?

I want to know the lower-level things, but they are more challenging than say just learning Python. I did start a little Python a while ago and it wasn't bad, along with taking a couple of silly classes last year that covered the most basic of the basics.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 15, 2014 4:58 pm UTC

Is there any open source software you use? Preferably not something too complex. Figure out a feature you want to add to it, and try to do it. Go to the bug reports and see if you can figure out how to fix bugs. Reading is all well and good, but at some point you just need to dive into it. Just don't get frustrated because you can't figure it out, just move on to something a bit easier and come back to it.

Personally, I wouldn't be too concerned about low level until you are comfortable with the high level.
Summum ius, summa iniuria.

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 5:00 pm UTC

Thesh wrote:Is there any open source software you use? Preferably not something too complex. Figure out a feature you want to add to it, and try to do it. Go to the bug reports and see if you can figure out how to fix bugs. Reading is all well and good, but at some point you just need to dive into it. Just don't get frustrated because you can't figure it out, just move on to something a bit easier and come back to it.

Personallyz I wouldn't be too concerned about low level until you are comfortable with the high level.

I'm a Linux user, but don't really know of any open source things I could help with.

I feel that I am comfortable, to an extent, with the high level, but it bores me. I think the class I took in high school, a very basic primer using Visual Basic, may have turned me away from some of the higher level things. I absolutely hated VB.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 15, 2014 5:02 pm UTC

You don't even have to cobtribute; just pick something and see if you can figure it out.

Also, I suppose I should mention https://projecteuler.net/
Summum ius, summa iniuria.

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 5:08 pm UTC

Thesh wrote:You don't even have to cobtribute; just pick something and see if you can figure it out.

Also, I suppose I should mention https://projecteuler.net/

I've considered PE, but am terrible at math and 1) it seems to be very mathy 2) it seems to improve math skills more than it does programming.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 5:11 pm UTC

Implement your favourite data structures?
He/Him/His/Alex
God damn these electric sex pants!

User avatar
Xenomortis
Not actually a special flower.
Posts: 1448
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: Coding: Fleeting Thoughts

Postby Xenomortis » Tue Jul 15, 2014 5:19 pm UTC

Fancy wrote:every time I start a book, I hop to another book halfway through. I've tried K&R, Learn C The Hard Way, C: How to Progrem, C Programming: A Modern Approach...the list is infinite. I've seen some bad reviews for many of these, too. "K&R is outdate" "LCTHW isn't teaching right" along with the accompanying "K&R is god" "LCTHW is excellent"

Right now I'm starting Ivor Horton's "Beginning C" because it looks good.

Books don't seem to be helping you.
I have "read"* precisely two "learning to program" books.

(Spoiler relates to my personal experience - not exactly relevant)
Spoiler:
The first was a Pascal book (can't remember the name) we followed in my AS Computing class**. It wasn't used for long; it basically covered the very basics (primitive types, arrays, loop constructs, etc). That class was basically "here's how to do very basic programming, here's how to make a GUI using Delphi (and Borland's IDE, whatever it was called), now write a program to do something" - the coding part of it lasted about 4 months and I didn't continue into the second year.

The second was a VB.NET book I briefly followed when I started my first job (21, straight out of University, with no programming experience since that Computing class at 16/17).
This was a bigger book, but I didn't follow it closely (I remembered the basics). This did cover the basics of Object Oriented Programming, but I didn't read that bit much, because I didn't get the point of OOP (book examples are always so contrived that OO struck me as overly complicated). Of course my first project at that job was a mess - still didn't get the point of OOP and so arrays of things flew everywhere. It was ok; nobody seemed to care what I did for the first 6 months there.
That VB.NET book was still used for a bit though, as reference, just not for progression.
**(AS Level is the first year of A Levels; an academic qualification taken at 16-18)

*Maybe I got half way through one of them.


FancyHat wrote:Another huge problem I find is I don't know where to start. I can't think of any cool porjects to do

This is a problem I share.
There have been a few things that have helped me, Project Euler being a big one (shame the functionality of that's been reduced for the moment...), there's the Red Spider Project on these forums, which are basically a collection of cute programs / scripts, that are bundled together - my first real C++ effort went towards that (and I swear I'll finish it someday!).
There's also Programming Wars on these forums (viewtopic.php?f=14&t=103385 and viewtopic.php?f=14&t=107922) - kinda on a break because I'm supposed to be hosting the next round and motivation and me go together like vampires and werewolves; there's no known reason why they can't get along, it just never seems to work out.

I will be honest though; the only reason I program as a hobby was because the initial barriers were removed by getting a job as a developer.
The fact you seem to be actively seeking this out is promising.

Fancy wrote:I absolutely hated VB.

I think everybody hates VB.

ahammel wrote:Implement your favourite data structures?

How dull.
Program a simple game - it's what the cool kids do!
(And I do mean, really simple; terminal stuff - and let it build up from there).

Fancy wrote:I've considered PE, but am terrible at math and 1) it seems to be very mathy 2) it seems to improve math skills more than it does programming.

They are, but they're great for getting familiar with the basics of a language.
Image

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 5:22 pm UTC

Xenomortis wrote:
ahammel wrote:Implement your favourite data structures?

How dull.

Suit yourself. I like a good binomial heap :P
He/Him/His/Alex
God damn these electric sex pants!

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 5:26 pm UTC

Xenomortis wrote:...

I don't even know what a data structure is, but I'll look into PE. Finsihed problem 1, problem 2 seems reasonably easy too.
I don't know where I would even start with a game. I don't have that sort of creativity, I'm more of an analytical problem-solver.

On seeking it out
I do it for three reasons. The first is that I enjoy it, the second is that is seems like an excellent career oppurtunity, and the third is that it produces something useful. Sometimes. Other times it just adds up all the multiples of 3 and 5 less than 1000.

FT: Is it considered bad on these forums to do what I do with omitting a large amount of quoted text? It seems like a huge waste of screen space.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 5:31 pm UTC

Fancy wrote:I don't even know what a data structure is
It's a thing you put data in. Arrays and structs are the ones C gives you for free. If you want something fancy like a hash table, you have to make it yourself (or use a library, but where's the fun in that?)

FT: Is it considered bad on these forums to do what I do with omitting a lrage amount of quotes? It seems like a huge waste of screen space.

It's fine.
He/Him/His/Alex
God damn these electric sex pants!

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 5:38 pm UTC

See, I'm horrible at this programming thing. I can't even do Project Euler problem 2. I'm trying to debug it with gdb, and that isn't going so well.

Spoiler:

Code: Select all

#include <stdio.h>
int main(void)
{
   int num = 2, prev_num = 1, sum = 0;
   while (num < 4000000) {
      if (num % 2 == 0)
         sum += num;
      prev_num = num;
      num += prev_num;
   }
   printf("%d", sum);

   return 0;
}
Last edited by Fancy on Tue Jul 15, 2014 5:46 pm UTC, edited 1 time in total.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 15, 2014 5:45 pm UTC

Everyone was horrible when they started. Figuring out where you went wrong is how you get better.
Summum ius, summa iniuria.

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 5:48 pm UTC

Thesh wrote:Everyone was horrible when they started. Figuring out where you went wrong is how you get better.

I posted the code in my spoiler. It seems perfect, but I'm just retarded.
And then I got it immediately after my post. Very embarassing.
With many little strokes a large tree is felled.
-Japanese Proverb

speising
Posts: 2353
Joined: Mon Sep 03, 2012 4:54 pm UTC
Location: wien

Re: Coding: Fleeting Thoughts

Postby speising » Tue Jul 15, 2014 5:55 pm UTC

Fancy wrote:See, I'm horrible at this programming thing. I can't even do Project Euler problem 2. I'm trying to debug it with gdb, and that isn't going so well.

Spoiler:

Code: Select all

#include <stdio.h>
int main(void)
{
   int num = 2, prev_num = 1, sum = 0;
   while (num < 4000000) {
      if (num % 2 == 0)
         sum += num;
      prev_num = num;
      num += prev_num;
   }
   printf("%d", sum);

   return 0;
}

after the first iteration, prev_num will be 2 and num will be ... 4.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: Coding: Fleeting Thoughts

Postby EvanED » Tue Jul 15, 2014 5:57 pm UTC

Two thoughts:

  • There's a religious war thread on this, but I'm pretty down on C for a first language for most people. "If you really want to learn C" is pretty much the exception to that, so you may be someone for whom C is right. But my position is that there are multiple aspects that are hard about programming, the hardest of which is learning how to "think computationally" -- break down a problem into intermediate goals that you can implement, think through edge cases and details, etc. And C throws too many difficulties that are, IMO, completely unrelated to that problem at you at the same time you're trying to learn the hardest thing about programming -- you have to deal with buffer overruns and stray pointers, you have to deal with manual memory management, you have to deal with an incredibly anemic standard library, and you have to deal with a language that prevents any reasonable implementation of generic containers. I suspect much better is to start with something where the language isn't set against your progress, work in that for a while, and then learn C after you have some reasonable experience programming. It should soften the learning curve. Python is the in-vogue language for this, and I don't think I have suggestions that are clearly better, so that's what I'd recommend even though I like types.
  • You might look for open class materials for classes based around the language you're interested in. The benefit of that over most books would be that you may be able to get assignments and projects and such that are designed for the right level. (This is another place where Python would help -- you'd have resources like MIT OCW at your disposal. Another weird option would be to find a class, like Berkeley's, that is in Scheme using "SICP". It should be easy to find resources for Python and for Java; it'll be harder to find a class in C that assumes no prior programming experience.)

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 5:59 pm UTC

EvanED wrote:Two thoughts:

  • There's a religious war thread on this, but I'm pretty down on C for a first language for most people. "If you really want to learn C" is pretty much the exception to that, so you may be someone for whom C is right. But my position is that there are multiple aspects that are hard about programming, the hardest of which is learning how to "think computationally" -- break down a problem into intermediate goals that you can implement, think through edge cases and details, etc. And C throws too many difficulties that are, IMO, completely unrelated to that problem at you at the same time you're trying to learn the hardest thing about programming -- you have to deal with buffer overruns and stray pointers, you have to deal with manual memory management, you have to deal with an incredibly anemic standard library, and you have to deal with a language that prevents any reasonable implementation of generic containers. I suspect much better is to start with something where the language isn't set against your progress, work in that for a while, and then learn C after you have some reasonable experience programming. It should soften the learning curve. Python is the in-vogue language for this, and I don't think I have suggestions that are clearly better, so that's what I'd recommend even though I like types.
  • You might look for open class materials for classes based around the language you're interested in. The benefit of that over most books would be that you may be able to get assignments and projects and such that are designed for the right level. (This is another place where Python would help -- you've got resources like MIT OCW at your disposal. Another weird option would be to find a class, like Berkeley's, that is in Scheme using "SICP".)

I don't like Python because "ha indents actually matter" and this makes it a pain to edit. I don't know of a scheme interpreter (compiler?) for Linux. I really want to learn C, because C is what is used for the things that interest me, but it's so unbelievably hard.
With many little strokes a large tree is felled.
-Japanese Proverb

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Tue Jul 15, 2014 6:01 pm UTC

Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 6:02 pm UTC

Fancy wrote: I don't know of a scheme interpreter (compiler?) for Linux

Racket, Chicken, and MIT Scheme. That's off the top of my head, I'm sure there are half a dozen others. I recommend Racket (decent module system).
He/Him/His/Alex
God damn these electric sex pants!

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 6:03 pm UTC

korona wrote:Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

That's a whole lot of extra coding to be done though.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 6:07 pm UTC

Fancy wrote:
korona wrote:Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

That's a whole lot of extra coding to be done though.

No? Just printf some relevant variables in functions where you think there are bugs.

That's a common debugging strategy in pretty much every language.
He/Him/His/Alex
God damn these electric sex pants!

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 6:11 pm UTC

ahammel wrote:
Fancy wrote:
korona wrote:Just a quick thought:
When debugging C/C++ programs I rarely use GDB at all. The best debugging tool is printf. GDB is nice for checking where a segfault happens or why some code is stuck in an infinite loop but I don't use it to inspect variables or look at the control flow. printf is faster for that stuff.

That's a whole lot of extra coding to be done though.

No? Just printf some relevant variables in functions where you think there are bugs.

That's a common debugging strategy in pretty much every language.

I know that it's a relevant strategy, but then my screen won't just be filled with my failing code, now it'll be "AT LINE X VARIABLE Y HAS VALUE Z"
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
Thesh
Made to Fuck Dinosaurs
Posts: 6579
Joined: Tue Jan 12, 2010 1:55 am UTC
Location: Colorado

Re: Coding: Fleeting Thoughts

Postby Thesh » Tue Jul 15, 2014 6:13 pm UTC

I'm not a fan of printf in general for debugging, unless it's a small program - it probably suits your needs here. However, you may consider using an IDE like KDevelop which doesn't have nearly as steep a learning curve as GDB.

Also, there are plenty of languages out there. Some easier to learn languages:
Java
C# if you don't mind windows (despite being from Microsoft, it's a great language, and Visual Studio is one a great toolset)
Ruby

There are also some newer langusges like Go and Rust if you don't mind being out on the fringe.
Summum ius, summa iniuria.

User avatar
Dason
Posts: 1311
Joined: Wed Dec 02, 2009 7:06 am UTC
Location: ~/

Re: Coding: Fleeting Thoughts

Postby Dason » Tue Jul 15, 2014 6:19 pm UTC

Fancy wrote:I don't like Python because "ha indents actually matter" and this makes it a pain to edit.


Well you should be indenting properly anyways. I understand some people are against this aspect of python but honestly if you're just starting off it might not be bad to learn some decent indentation conventions.
double epsilon = -.0000001;

korona
Posts: 495
Joined: Sun Jul 04, 2010 8:40 pm UTC

Re: Coding: Fleeting Thoughts

Postby korona » Tue Jul 15, 2014 6:25 pm UTC

Thesh wrote:I'm not a fan of printf in general for debugging, unless it's a small program - it probably suits your needs here. However, you may consider using an IDE like KDevelop which doesn't have nearly as steep a learning curve as GDB.

I use mostly printf debugging, even for large projects. In my experience (which is mostly low level stuff, I'm almost never involved with end-user applications) the bugs that are hard to find all happen in corner cases with heavy branching. Fixing bugs in linear code is easy. Adding a temporary "if(in corner case) printf(relevent vars)" to the code is easy - setting conditional breakpoints and complex watch expressions in debuggers is annoying and time consuming.

Ubik
Posts: 1016
Joined: Thu Oct 18, 2007 3:43 pm UTC

Re: Coding: Fleeting Thoughts

Postby Ubik » Tue Jul 15, 2014 7:33 pm UTC

Not related to the current discussion, just a fleeting thought: If V8 supported asm.js, you could run C code pretty much natively on node.js.

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 8:51 pm UTC

Dason wrote:
Fancy wrote:I don't like Python because "ha indents actually matter" and this makes it a pain to edit.


Well you should be indenting properly anyways. I understand some people are against this aspect of python but honestly if you're just starting off it might not be bad to learn some decent indentation conventions.

I do follow a strinct indent and formatting rule, it's just not "python compliant" For my C, which is my main focus, it's linux kernel style.
With many little strokes a large tree is felled.
-Japanese Proverb

User avatar
ahammel
My Little Cabbage
Posts: 2135
Joined: Mon Jan 30, 2012 12:46 am UTC
Location: Vancouver BC
Contact:

Re: Coding: Fleeting Thoughts

Postby ahammel » Tue Jul 15, 2014 9:05 pm UTC

In what way is kernel style not Python compliant?
He/Him/His/Alex
God damn these electric sex pants!

Fancy
Posts: 57
Joined: Sun Jul 13, 2014 7:23 pm UTC

Re: Coding: Fleeting Thoughts

Postby Fancy » Tue Jul 15, 2014 9:38 pm UTC

ahammel wrote:In what way is kernel style not Python compliant?

Tabs are 8 wide. I tried it, and it didn't work. I'll go find some example code and try again.
With many little strokes a large tree is felled.
-Japanese Proverb


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 16 guests