Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 71 72 [73] 74 75 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95671 times)

olemars

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1080 on: March 18, 2011, 04:22:50 am »

You generally don't need the extern keyword for anything in C++, just FYI.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1081 on: March 18, 2011, 07:25:09 am »

a makefile usually (but doesn't have to) list each .o file as a target with its corresponding .c file as a requirement. if the .c has been modified since the .o was last built, it runs commands (usually just gcc, with a flag to stop before linking) to update it. the default target is generally the executable, which depends on all of the .o files

I think that mostly covers it... makefiles can be used for a number of other things too
A Makefile consists of "rules" which are a series of shell commands to build your application. Instead of manually running gcc a couple of times, you can use make to do it for you.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1082 on: March 18, 2011, 07:26:53 am »

You generally don't need the extern keyword for anything in C++, just FYI.

you do for global variables, and that's about it.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1083 on: March 18, 2011, 08:01:54 am »

And you shouldn't be using any global variables in C++.
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1084 on: March 18, 2011, 08:03:28 am »

And you shouldn't be using any global variables in C++.

I know, I'm sorry.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1085 on: March 18, 2011, 08:25:37 am »

And you shouldn't be using any global variables in C++.
I know, I'm sorry.
Yeah, you better be!
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1086 on: March 18, 2011, 08:58:22 am »

And you shouldn't be using any global variables in C++.

I know, I'm sorry.
Give me 10 hail Stroustrup's for penance.
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1087 on: March 19, 2011, 12:15:03 pm »

More linked list fun. My linked list breaks when setting it to circulate (i.e. linking the first and last items together).
Code: [Select]
#include <stdio.h>
#define LIST  _Node

typedef struct __Node {
     int data;
     struct __Node *next;
} _Node;

LIST navigate (LIST *l, int i)
{
     LIST ret = *l;
     while(i-- > 0)
         if (ret.next != NULL) ret = *(ret.next);
     return ret;
}

void link (LIST *l, LIST *m) /* insert 'm' after 'l' */
{
     m->next = (l->next == NULL) ? NULL : l->next;
     l->next = m;
}


int main ()
{
     LIST a, b, c;
     a.data = (int)'a'; b.data = (int)'b'; c.data = (int)'c';
     link(&a, &b); link(&b, &c); link(&c, &a);

     printf("%d %d %d\n",
        a.data,
        navigate(&a, 1).data,
        navigate(&a, 2).data
      );
     return 0;
}
The correct output should be "97 98 99", but for some reason I get "97 1330556 1330556".
« Last Edit: March 19, 2011, 12:24:04 pm by ILikePie »
Logged

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1088 on: March 19, 2011, 12:28:59 pm »

And you shouldn't be using any global variables in C++.

I know, I'm sorry.
Give me 10 hail Stroustrup's for penance.
Have you read the (thick) book by him on C++?
...
I'm still reading  :P
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Shades

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1089 on: March 19, 2011, 02:06:36 pm »

Code: [Select]
void link (LIST *l, LIST *m) /* insert 'm' after 'l' */
{
     m->next = (l->next == NULL) ? NULL : l->next;
     l->next = m;
}

I haven't run this code so I might be missing something but it seems to me this will mean the third link statement will actually modified a's next as well to point at whatever c's next was set to.
If you running in debug c's next pointer will have been defaulted to NULL (depending on your compiler) but if your not it would have been random memory as you don't initialise any of the next values.

Also m->next = (l->next == NULL) ? NULL : l->next; is the same as typing m->next = l->next;
if (a = null) then b = null else b = a either way.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1090 on: March 19, 2011, 02:16:51 pm »

What Shades said. *next is uninitialised, and therefore not-NULL. Make a constructor.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1091 on: March 19, 2011, 03:22:02 pm »

The struct naming is a bit odd. Judging by the typedef, I would guess that that is C, and in C, "struct foo" and "foo" can be entirely different types (bad idea, though. It would be extremely confusing). typedef struct a {...} a; is valid, and would result in both a "struct a" and a "a" being types that mean the same thing.

Furthermore, link() inserts m between l and the node following l, so before you do any of the links, setting a.next to &a means that the list will automatically remain circular as nodes are inserted. Alternatively, directly setting the last node to have the first one as .next would also work. However, link() is not designed to re-insert a node, so it can't be used to link the last node to the first node properly, instead it would basically move the first node to the end of the list, and leave the list non-circular, or, if used on a node mid-list, you get something like a backwards tree.
Logged
Eh?
Eh!

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1092 on: March 20, 2011, 06:29:15 am »

The struct naming is a bit odd. Judging by the typedef, I would guess that that is C, and in C, "struct foo" and "foo" can be entirely different types (bad idea, though. It would be extremely confusing). typedef struct a {...} a; is valid, and would result in both a "struct a" and a "a" being types that mean the same thing.
I didn't know that, though, does it really matter? I mean, I wont be using struct __Node or struct _Node anywhere anyway.

e, Creating a constructor and modifying link did it.
Code: [Select]
...

LIST node () /* list node constructor */
{
     LIST l;
     l.next = NULL;
     return l;
}
...
void link (LIST *l, LIST *m) /* insert 'm' after 'l' */
{
     if (l->next != NULL) m->next = l->next;
     l->next = m;
}

int main ()
{
     LIST a = node(), b = node(), c = node();
     a.data = (int)'a'; b.data = (int)'b'; c.data = (int)'c';
     link(&a, &c);
     link(&a, &b);
     link(&c, &a);
     
     printf("%c->%c->%c->%c\n",
    a.data,
    navigate(&a, 1).data,
    navigate(&a, 2).data,
    navigate(&a, 3).data
  );
     return 0;
}

------

$ ./list
a->b->c->a
« Last Edit: March 20, 2011, 07:36:05 am by ILikePie »
Logged

bowdown2q

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1093 on: March 31, 2011, 11:31:44 pm »

Damn, shame I found this thread late at night >_o. I've been trying to code a game in VB and keep getting hung up. I'll read through this thread tomorrow, but for now here's a link to my current project http://www.bay12forums.com/smf/index.php?topic=81060.0 and I'll post to watch and remind myself this exists.

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1094 on: April 01, 2011, 03:35:54 pm »

And you shouldn't be using any global variables in C++.

I know, I'm sorry.
Give me 10 hail Stroustrup's for penance.
Have you read the (thick) book by him on C++?
...
I'm still reading  :P

Mmm, same here.

I'm finding it really useful, though. Good purchase.
Logged
Pages: 1 ... 71 72 [73] 74 75 ... 78