Author Topic: Ds Dude's Programming Corner :D  (Read 4264 times)

0 Members and 1 Guest are viewing this topic.

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Ds Dude's Programming Corner :D
« on: March 25, 2009, 01:52:21 pm »
Okay, so, I wanted to make a topic discussing the programing language called C. It was created by Dennis Ritchie in 1972 at Bell Telephone Laboratories, and is used to create the UNIX operating system. C was meant to create software.

So now you know who it was created by, where, and why.
This going to be like a semi-blog/help corner/anything you want to say about C.

Alright, I started reading The C Programming Language by K&R (Kernighan, Ritchie) that jrgp has been nice enough to lend me, and I'm on Word Counting. So they give me this program that I should type myself to get the feel. I type it. It ends up like this:

Code: [Select]
#include <stdio.h>

#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */

/* count lines, words, and characters in input */
main()
{
int c, nl, nw, nc, state;

state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d\n", nl, nw, nc);
}
Pretty good, it's not too complicated, I was able to break it down and understand what it means. After all that, I figure out that they tell you in the book which states "Every time the program encounters the first character of a word, it counts one more word." Then they tell you what each piece of code does. I honestly have to reread each section to fully understand, but that's cool, because it a programming language right? Takes time.

So I read through, and I compile it using Code::Blocks. I run the exe, and it just like the last two programs?(The last two sections taught me line counting and character counting). I wonder if I did it wrong. It was long 'till I figured out that the code on the book says:

if (c == ' ' || c == '\n' || c = '\t')

I look at it and try to read this code. It says, "if c is a blank or c is a newline or c is a tab. Then it hits me, "==" means is, but "=" means to. So for example, "c = 0" means "c to 0", and "c == 0" means "c is o." I thought the book made a mistake and I believe I was correct, for then when I compiled it gave me no errors.

When I click on the executable file, the command prompt opens and allows be to type whatever I want. I started getting skeptical, because it is exactly like the other programs, I might not know it, so you help me out there.

So far, I'm going over the section again, to make sure I understand what this program is suppose to do. If you guys would like to help me, you can.

I'll be updating this topic on all the stuff I'm doing right now, and keep you guys up-to-date (Thank god for the Edit button).



« Last Edit: March 25, 2009, 02:56:00 pm by ds dude »
This signature was borked. Now it is not.

Offline -Skykanden-

  • Flamebow Warrior
  • *****
  • Posts: 3065
  • Hallowed be my name
Re: Ds Dude's Programming Corner :D
« Reply #1 on: March 25, 2009, 02:22:47 pm »
Would be great to learn some basic programming, so why not :]

maybe you should put those codes with the
Code: [Select]
the code there

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #2 on: March 25, 2009, 02:23:43 pm »
What do you mean?
This signature was borked. Now it is not.

Offline -Skykanden-

  • Flamebow Warrior
  • *****
  • Posts: 3065
  • Hallowed be my name
Re: Ds Dude's Programming Corner :D
« Reply #3 on: March 25, 2009, 02:24:24 pm »
I mean, I don't really know where it starts, because you explained it a bit, so I would like to see where the code starts and ends

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Ds Dude's Programming Corner :D
« Reply #4 on: March 25, 2009, 02:48:11 pm »
Code: [Select]
#include <stdio.h>

#define IN   1 /* inside a word */
#define OUT   0 /* outside a word */

/* count lines, words, and characters in input */
main()
{
   int c, nl, nw, nc, state;

   state = OUT;
   nl = nw = nc = 0;
   while ((c = getchar()) != EOF) {
      ++nc;
      if (c == '\n')
         ++nl;
      if (c == ' ' || c == '\n' || c == '\t')
         state = OUT;
      else if (state == OUT) {
         state = IN;
         ++nw;
      }
   }
   printf("%d %d %d\n", nl, nw, nc);
}

I've done a bit reading, so thanks for helping up my C again.
The getchar() function reads from stdin. That's a part of the memory where the input stream is stored, usually the source is the keyboard. So as far as I can tell, the program is running, but I don't think you can send an EOF (end of file) through the keyboard. That means the while loop will never end properly and the program doesn't have chance to show you the numbers.
In the code:
Code: [Select]
while ((c = getchar()) != EOF)This while loop will go on as long as the input character is not an EOF.

Now according to this you should be able to input a file in stdin by using the command:
Code: [Select]
myapplication < example.txtWrite some text in the textfile and you should see the numbers, I hope it works :D
Come join: EliteCTF
Listen to: My Music

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #5 on: March 25, 2009, 02:50:31 pm »
Alright, I'll go try that. :)
This signature was borked. Now it is not.

Offline Mangled*

  • Flagrunner
  • ****
  • Posts: 925
  • Never Wrong
Re: Ds Dude's Programming Corner :D
« Reply #6 on: March 25, 2009, 02:50:58 pm »
What do you mean?

He means use code tags in your post for code.
"There she lusted after her lovers, whose genitals were like those of donkeys and whose emission was like that of horses." - Ezekiel 23:20

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #7 on: March 25, 2009, 02:52:13 pm »
Oh ok, thanks mangled.

JFK, where should I put myapplication < example.txt?

Can I put it in the middle of the code?
« Last Edit: March 25, 2009, 02:55:35 pm by ds dude »
This signature was borked. Now it is not.

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: Ds Dude's Programming Corner :D
« Reply #8 on: March 25, 2009, 03:36:04 pm »
oh no, sorry :)

It's a command-line, that means you'll have to 'run' it like that. You can do that by going to the start menu and select run and then filling in the complete path to your own .exe plus the extra command '< example.txt'.
Another way is using Command Prompt, you'll have to read how to work in it (it's old DOS), but if you want to learn how to program on Windows, that's not a bad idea.

A bit easier way would be to make a small batch-file. Batch-files will run command-lines for you and are easily made. Just create a new text-file and fill in:
Code: [Select]
myapplication < example.txt
replace 'myapplication' with the name of your .exe file. Then save this file in the same directory as your application. The name of this file doesn't matter, but make sure it ends with .bat and not .txt! If you double-click that file it will run the command line and your program will be loaded together with the example.txt file. Of course you also have to make example.txt
Come join: EliteCTF
Listen to: My Music

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Ds Dude's Programming Corner :D
« Reply #9 on: March 25, 2009, 05:03:37 pm »
ds dude: if you want to program in C seriously, get linux. It is far easier.

I'd also suggest not using an IDE when learning it. It may seem easier but learning to write makefiles (easy to do) and link stuff by hand and such are very useful skills. And way easier on linux.

if (c == ' ' || c == '\n' || c = '\t')
That is a really common error (among new people, when you get better you get to start messing up with pointers yay!) because it doesn't give an error when you compile it and run it. This is partly why I like Java as a first programming language, you save a LOT of time that would normally be spent debugging, and you can learn the programming concepts easier.

If you want a good C book btw, C for Dummies by Dan Gookin is actually really good.

Offline jettlarue

  • Flagrunner
  • ****
  • Posts: 724
Re: Ds Dude's Programming Corner :D
« Reply #10 on: March 25, 2009, 05:09:16 pm »
ds dude: if you want to program in C seriously, get linux. It is far easier.
Highly recommend Linux.

I think it's cool your pursuing C. I have never gotten around to be competent with it, but out of all languages it is probably to learn because the concepts is has are similar to ones in many other languages. Good luck bud.

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #11 on: March 25, 2009, 05:37:47 pm »
oh no, sorry :)

It's a command-line, that means you'll have to 'run' it like that. You can do that by going to the start menu and select run and then filling in the complete path to your own .exe plus the extra command '< example.txt'.
Another way is using Command Prompt, you'll have to read how to work in it (it's old DOS), but if you want to learn how to program on Windows, that's not a bad idea.

A bit easier way would be to make a small batch-file. Batch-files will run command-lines for you and are easily made. Just create a new text-file and fill in:
Code: [Select]
myapplication < example.txt
replace 'myapplication' with the name of your .exe file. Then save this file in the same directory as your application. The name of this file doesn't matter, but make sure it ends with .bat and not .txt! If you double-click that file it will run the command line and your program will be loaded together with the example.txt file. Of course you also have to make example.txt
I understand perfectly, I did the batch file, but when I click it the command prompt goes away instantly. I can't even recognize what was on the command prompt. I might have done something wrong, I'll check on it right now.

ds dude: if you want to program in C seriously, get linux. It is far easier.

I'd also suggest not using an IDE when learning it. It may seem easier but learning to write makefiles (easy to do) and link stuff by hand and such are very useful skills. And way easier on linux.

if (c == ' ' || c == '\n' || c = '\t')
That is a really common error (among new people, when you get better you get to start messing up with pointers yay!) because it doesn't give an error when you compile it and run it. This is partly why I like Java as a first programming language, you save a LOT of time that would normally be spent debugging, and you can learn the programming concepts easier.


If you want a good C book btw, C for Dummies by Dan Gookin is actually really good.

Yes, I understand, jrgp told me about Linux and programming. I've been studying Linux along with C, and I'm learning tons. The only problem is I can't have Linux on my desktop (which I don't use for programming anyway), and I want to get it on my laptop.

I need to find a good and easy way to put a persistent ubuntu on a usb and program from there. I'll have to figure out how to compile my program to what Linux can execute from, because EXE is out of the question. Any help with this is greatly appreciated. Btw, I'll look into that book. Thanks. ;)

ds dude: if you want to program in C seriously, get linux. It is far easier.
Highly recommend Linux.

I think it's cool your pursuing C. I have never gotten around to be competent with it, but out of all languages it is probably to learn because the concepts is has are similar to ones in many other languages. Good luck bud.
Yes, jrgp showed me the light and help me with getting a compiler running and using Visual C++ (which I save all my code in C source file), which reminds me, what does Linux use to code with?

Thank you everyone, I'll keeping this topic alive as much as possible. Still working on Word count before I get into Arrays (which is the next section). I am greatly committed to this. Thank you.

« Last Edit: March 25, 2009, 05:53:39 pm by ds dude »
This signature was borked. Now it is not.

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Ds Dude's Programming Corner :D
« Reply #12 on: March 25, 2009, 07:01:08 pm »
I need to find a good and easy way to put a persistent ubuntu on a usb and program from there. I'll have to figure out how to compile my program to what Linux can execute from, because EXE is out of the question. Any help with this is greatly appreciated. Btw, I'll look into that book. Thanks. ;)
Linux doesn't use .exe (unless going through wine) instead it's got its own fancy executable files. To compile it to that just type in "gcc blah.c" (read up on further options if you want) and it compiles blah.c and makes an executable for you.
Read up on gcc for more info.

About the usb thing, why not just partition your HD?

Yes, jrgp showed me the light and help me with getting a compiler running and using Visual C++ (which I save all my code in C source file), which reminds me, what does Linux use to code with?
Whatever you want. I just use gedit which is the default gnome text editor. gcc to compile my stuff, gdb when I'm completely stuck with a bug (still haven't learned to use it very well).
Just 'cause I'm bored I'll post a pic of what I work with, as well as the program I'm working on right now ^.^

Pic 1: the environment I program in. It's simple and easy to use. That's my pms reader in the background btw :)
Pic 2: the program I'm writing. I like it.

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #13 on: March 25, 2009, 07:52:46 pm »
Hm, interesting. I don't need to download anything, it's all there in convenience.

The reason I can't partition, well, it's because my netgear wireless doesn't seem compatible with Ubuntu. I thought I was able to connect instantly, but it didn't. So I need some help with that before I can install it. I have a usb with linux on it, so I can use that to test and see how I can make my wireless work, then put Ubuntu on a partition permanently. I'm going to have to clear up some space now.

I'll go search up on gcc.
This signature was borked. Now it is not.

Offline Xxypher

  • Veteran
  • *****
  • Posts: 1319
  • Soldat Veteran.
Re: Ds Dude's Programming Corner :D
« Reply #14 on: March 25, 2009, 08:16:12 pm »
Wanna see some of the codes of my AI?
This is only the targeting system, nothing else.


Code: [Select]
//Sets where the bot looks and moves to.
var ID;
ID=0

if bot_check_ground(x+image_xscale*25,y,x+image_xscale*25,y+600)=false
{

    if place_free(tar_x,tar_y)
    {
    tar_x=x+lengthdir_x(50,point_direction(x,y,room_width/2,y))
    tar_y=y
    aim_tar_x = tar_x;
    aim_tar_y = tar_y;
    }
   
if image_xscale=1 { move_dir="left" }
if image_xscale=-1 { move_dir="right" }

}
else
{
    if instance_exists(o_w_healing) { ID=instance_nearest(x,y,o_w_healing) }
   
    var xx, yy;
    xx=room_width/2
    yy=-2000
   
    if instance_exists(ID)
    {
    xx=ID.x
    yy=ID.y
    }
   
    if (life<20 and distance_to_point(xx,yy)<900) or (life<100 and !instance_exists(tar)) and instance_exists(o_w_healing) and distance_to_object(ID)<800
    {
        bot_find_object(instance_nearest(x,y,o_w_healing))
    }
    else
    {
   
        if distance_to_object(o_bomb)<100 //Avoid grenades
        {
        ID=instance_nearest(x,y,o_bomb)
            if distance_to_object(ID)<200
            {
            tar_x=x+lengthdir_x(-230,point_direction(x,y,ID.x+ID.hspeed*10,ID.y+ID.vspeed*10))
            tar_y=y+lengthdir_y(-230,point_direction(x,y,ID.x+ID.hspeed*10,ID.y+ID.vspeed*10))
            aim_tar_x=x+lengthdir_x(-230,point_direction(x,y,ID.x+ID.hspeed*10,ID.y+ID.vspeed*10))
            aim_tar_y=y+lengthdir_y(-230,point_direction(x,y,ID.x+ID.hspeed*10,ID.y+ID.vspeed*10))
            }
        }
        else
        {



           
                                   

                        {
                        if instance_exists(lkl)
                                {
                                ID=instance_nearest_ext(x,y,lkl,1)
                                if distance_to_point(ID.x,ID.y)<1000 and collision_line(x,y,ID.x,ID.y,o_solidparent,0,true)<0
                                    {
                                    tar_x=x+lengthdir_x(-300,point_direction(x,y,ID.x,ID.y))
                                    tar_y=y-40+lengthdir_y(-50,point_direction(x,y,ID.x,ID.y))
                                    aim_tar_x = x+lengthdir_x(-300,point_direction(x,y,ID.x,ID.y))
                                    aim_tar_y = y-40+lengthdir_y(-50,point_direction(x,y,ID.x,ID.y))
                                    if !(place_free(tar_x,tar_y)) { tar_y-=100 }                                   
                                    }
                                    else
                                    {
                                    bot_idle()
                                    }


               
               
                if global.mode=0{bot_chase_2()}                                   
               
                }

   

}}}}

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #15 on: March 25, 2009, 08:35:30 pm »
Hmm.. interesting, I think I know a little of it, can't say anything for the whole thing though.

I still am having problems with my word counting program crashing. It seems that there might be something wrong with the code, I'll check it out right now. To know more about this problem, read my previous post.
This signature was borked. Now it is not.

Offline Xxypher

  • Veteran
  • *****
  • Posts: 1319
  • Soldat Veteran.
Re: Ds Dude's Programming Corner :D
« Reply #16 on: March 25, 2009, 08:54:10 pm »
Does it keep track of everything? Do you keep/use a DS list?

Offline ds dude

  • Soldat Beta Team
  • Flagrunner
  • ******
  • Posts: 631
  • Lolicon Forever.
Re: Ds Dude's Programming Corner :D
« Reply #17 on: March 25, 2009, 10:49:17 pm »
Hey, could you explain what a DS list is? By keeping track, do you mean your target system code?
This signature was borked. Now it is not.

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Ds Dude's Programming Corner :D
« Reply #18 on: March 25, 2009, 11:08:32 pm »
About a word counting program, here's how I would do it (and did it in a pascal script thing).

Make the count start at 1.
Find first character in the string that is a space. Add 1 to count. Then cut the string up to that point, cutting out the space. Repeat until there are no more spaces.

Offline Farah

  • Major
  • *
  • Posts: 86
Re: Ds Dude's Programming Corner :D
« Reply #19 on: March 26, 2009, 03:19:23 am »
xxypher that is in GML am i correct?
<EnEsCe> you challenge me I will make your Soldat life a living hell.