Author Topic: For C programmers...  (Read 11911 times)

Offline Matt

  • Polski American
  • VIP
  • Sr. Member
  • **
  • Posts: 336
  • Rank: Transformer
  • Score: 1564
  • An IT enthusiast and gadget geek
    • MattMski
Re: For C programmers...
« Reply #15 on: November 16, 2016, 12:15:08 am »
As a Java beginner, thanks for posting your version Mia. Studying code, like that, to understand it better and better helps :)

RD og, Ex Admin. Been playing PTP since 2011 (Tenshi's PTP, not Jonne's)

Offline Spectre

  • VIP
  • Hero Member
  • **
  • Posts: 622
  • Rank: Unknown
  • Score: Unknown
  • Grossly incandescent
    • Team: No Terrorism
Re: For C programmers...
« Reply #16 on: November 16, 2016, 01:02:19 am »
or like I did (find the divisors of every number, in except for the number itself, and check if their addition equals the number, definition of perfect number).

Yep, that's how I found it to be the easiest - complete the logic of finding a perfect number (using for and if), then just put it into while loop

As a Java beginner, thanks for posting your version Mia. Studying code, like that, to understand it better and better helps :)

Java is one of the objective languages, right? I've heard they can be a real biatch xD

Offline YoMama

  • VIP
  • Hero Member
  • **
  • Posts: 638
  • Rank: Hoodsta
  • Score: 24630
Re: For C programmers...
« Reply #17 on: November 16, 2016, 01:17:53 am »
Like Tree and YoMama, I solved it using Mersenne primes...
I don't claim credit for that- I just modified TreePuncher's code to try to illustrate what inline does.

This is interesting, but this knowledge is kinda pure academic as you will be rarely messing with inline functions within your real projects.
I think this depends entirely on what you're doing- for any high-performance computing, you have to be aware of where you can cut out inefficiency, especially since Moore's law appears to be basically dead.

If you've got any more suggestions for "amateurs" in C (which I am lol), feel free to post them, or even examples of different interesting codes.
Project Euler is kind of cool, and you can almost always find the solutions to the problems on Stack Overflow and usually learn a lot from the different solutions people have. It's pretty heavy on theory, but still useful. I don't actually have that much experience with C; I've spent more time in the C++/.NET world. Take a computer architecture or compiler class if you want to learn about optimization. I also really like Computerphile for learning little odd things that can come in handy and overviews of how certain things work (here's their video on recursion, if you're interested). Computerphile is made by the same people that made Quido's videos.

Yep, that's how I found it to be the easiest - complete the logic of finding a perfect number (using for and if), then just put it into while loop
The shitty part about computer science is that often the easiest solutions to understand are the least efficient in practice.

Offline SoLoD

  • Hero Member
  • *****
  • Posts: 563
  • Rank: Unknown
  • Score: Unknown
    • Youtube stunting channel
Re: For C programmers...
« Reply #18 on: November 16, 2016, 06:38:42 am »
What i really cant get, do u all studying programming or just learning it for fun.
You know the nice thing about the Bronx Zoo, Charlie? There's bars between you and the monkeys.

Offline Matt

  • Polski American
  • VIP
  • Sr. Member
  • **
  • Posts: 336
  • Rank: Transformer
  • Score: 1564
  • An IT enthusiast and gadget geek
    • MattMski
Re: For C programmers...
« Reply #19 on: November 16, 2016, 08:13:35 am »
As a Java beginner, thanks for posting your version Mia. Studying code, like that, to understand it better and better helps :)

Java is one of the objective languages, right? I've heard they can be a real biatch xD
Yea Java is object oriented like PHP and C++. But it's one of the easiest to learn so I would say C++ looks harder.

What i really cant get, do u all studying programming or just learning it for fun.
Studying

RD og, Ex Admin. Been playing PTP since 2011 (Tenshi's PTP, not Jonne's)

Offline SoLoD

  • Hero Member
  • *****
  • Posts: 563
  • Rank: Unknown
  • Score: Unknown
    • Youtube stunting channel
Re: For C programmers...
« Reply #20 on: November 16, 2016, 09:27:27 am »
Well, Matt, if to talk about Mia`s code, it is bad. Do not write it on that way - making an array, putting in it all variables, so every time u program will go to RAM, it will call not a single int or long or whatever ur date type is, but it will go to an array, to the 4 bytes of its location, then it will move to the array`s numbers, change it... You just increasing time of prog. And well, that is kinda bm for someone who will read ur code.
And even more. In java you "do not give a fuck whats going on in ur RAM". But you must know how it works. If you take arrays, there is some unexpected stuff, which could lead you to overloading ur RAM or losing ur data. So, do not work with arrays THAT way.
And ofc you should care to minimize RAM using and time. If we talk about Mia`s code, you can simply divide by 2 RAM u are using and prog time by adding 2-3 symbols to the code. So, try to figure it out.
« Last Edit: November 16, 2016, 09:29:22 am by SoLoD[PPLV] »
You know the nice thing about the Bronx Zoo, Charlie? There's bars between you and the monkeys.

Offline Spectre

  • VIP
  • Hero Member
  • **
  • Posts: 622
  • Rank: Unknown
  • Score: Unknown
  • Grossly incandescent
    • Team: No Terrorism
Re: For C programmers...
« Reply #21 on: November 16, 2016, 02:49:01 pm »
If you take arrays, there is some unexpected stuff, which could lead you to overloading ur RAM or losing ur data.

I don't get what you're trying to say. As far as I got it, when you try to use something out of array range, the program will then fetch some unknown value from RAM. It can lead to some crazy output values, sure, but I don't know about losing data (correct me if I'm wrong and/or provide references for this statement).
Anyways, shouldn't be a problem for me, since I'm rarely using arrays that are over 20 in element length, and I'm being careful not to "step into the unknown" so to speak. RAM usage is always small since I'm not an expert programmer (yet) and my programs aren't that big.

Offline Miau

  • VIP
  • Hero Member
  • **
  • Posts: 565
  • Rank: Jacker
  • Score: 46079
Re: For C programmers...
« Reply #22 on: November 16, 2016, 03:35:03 pm »
Well, Matt, if to talk about Mia`s code, it is bad. Do not write it on that way - making an array, putting in it all variables, so every time u program will go to RAM, it will call not a single int or long or whatever ur date type is, but it will go to an array, to the 4 bytes of its location, then it will move to the array`s numbers, change it... You just increasing time of prog. And well, that is kinda bm for someone who will read ur code.
And even more. In java you "do not give a fuck whats going on in ur RAM". But you must know how it works. If you take arrays, there is some unexpected stuff, which could lead you to overloading ur RAM or losing ur data. So, do not work with arrays THAT way.
And ofc you should care to minimize RAM using and time. If we talk about Mia`s code, you can simply divide by 2 RAM u are using and prog time by adding 2-3 symbols to the code. So, try to figure it out.

I made it that way because Spectre wanted a code with a while loop and an [only one?] array.
Oh! I don't want to fight you, Jorah the Andal. What do I have to gain? If I win, I'm the shit who killed an old man. If I lose, I'm the shit who was killed by an old man.

~ Daario Naharis

Offline Matt

  • Polski American
  • VIP
  • Sr. Member
  • **
  • Posts: 336
  • Rank: Transformer
  • Score: 1564
  • An IT enthusiast and gadget geek
    • MattMski
Re: For C programmers...
« Reply #23 on: November 16, 2016, 10:12:08 pm »
Well, Matt, if to talk about Mia`s code, it is bad. Do not write it on that way - making an array, putting in it all variables, so every time u program will go to RAM, it will call not a single int or long or whatever ur date type is, but it will go to an array, to the 4 bytes of its location, then it will move to the array`s numbers, change it... You just increasing time of prog. And well, that is kinda bm for someone who will read ur code.
And even more. In java you "do not give a fuck whats going on in ur RAM". But you must know how it works. If you take arrays, there is some unexpected stuff, which could lead you to overloading ur RAM or losing ur data. So, do not work with arrays THAT way.
And ofc you should care to minimize RAM using and time. If we talk about Mia`s code, you can simply divide by 2 RAM u are using and prog time by adding 2-3 symbols to the code. So, try to figure it out.
I never said her code was the best either, I was interested in studying the loops mainly. That's one area I lack on and reading more of them helps. I understand it can be done in an advanced way but I'm not there yet. This baby beginner code helps out alot. Memory usage is a factor when writing an actual program, not something beginners write :P Regardless, thanks for ur opinion from an advanced perspective.

RD og, Ex Admin. Been playing PTP since 2011 (Tenshi's PTP, not Jonne's)

Offline Altus_Demens

  • Admin
  • Hero Member
  • ****
  • Posts: 1119
  • Rank: Hoodsta
  • Score: 23951
Re: For C programmers...
« Reply #24 on: November 17, 2016, 03:35:20 am »
Like Tree and YoMama, I solved it using Mersenne primes...
I don't claim credit for that- I just modified TreePuncher's code to try to illustrate what inline does.
Apologies, I wrote that purely automatically, seeing your contribution.

This is interesting, but this knowledge is kinda pure academic as you will be rarely messing with inline functions within your real projects.
I think this depends entirely on what you're doing- for any high-performance computing, you have to be aware of where you can cut out inefficiency, especially since Moore's law appears to be basically dead.
Of course, but what I mean is that you would rarely need to do it yourself: the compiler will interpret functions as inline when it's really needed. It's the same as overusing inline assembly: most of the modern compilers generate better assembly code than a programmer would, so you'd likely use it at the most crucial points of your program. But, of course, being capable of writing in assembly will definitely help you in your programmer's life. Besides, don't forget that inlining functions aren't just a magic wand, it can turn out to be negative, especially when you're working with classes in C++.

Well, Matt, if to talk about Mia`s code, it is bad. Do not write it on that way - making an array, putting in it all variables, so every time u program will go to RAM, it will call not a single int or long or whatever ur date type is, but it will go to an array, to the 4 bytes of its location, then it will move to the array`s numbers, change it... You just increasing time of prog. And well, that is kinda bm for someone who will read ur code.
Obviously Mia (and me) used an array instead of standard variables only because Spectre's task formulation suggested that. Aside of it, her code (as much as Tree's and mine) is pretty nice.
Of course, nobody would use arrays like we did, they exist for other purposes. But in fact you are not right saying that it will always spend more processor time. It is true for, say, Java, as the arrays are actually objects in Java. One bytecode instruction is used to access a "normal" variable (STORE var_idx or LOAD var_idx) and at least three to access an array value (ALOAD arr, PUSH idx, ALOAD or ASTORE). But it doesn't work like this in C: every variable, including an array element, is accessed by shifting from the base pointer (mov $value, -var_idx(%ebp) or mov -var_idx(%ebp), %eax).

And even more. In java you "do not give a fuck whats going on in ur RAM". But you must know how it works. If you take arrays, there is some unexpected stuff, which could lead you to overloading ur RAM or losing ur data. So, do not work with arrays THAT way.
And ofc you should care to minimize RAM using and time. If we talk about Mia`s code, you can simply divide by 2 RAM u are using and prog time by adding 2-3 symbols to the code. So, try to figure it out.
Well, the GC works very well nowadays, yet you should care about the memory. My experience tells that memory leaks happen more rarely in Java projects, but they are sometimes harder to locate and eliminate. As for the arrays, what unexpected stuff are you talking about? (sorry for picking on your words, but I just love Java and I'm always interested to discuss it ;D)

Yea Java is object oriented like PHP and C++. But it's one of the easiest to learn so I would say C++ looks harder.
Well, PHP's object orientation still looks like a diesel engine on a horse carriage... ;D Though, talking seriously, it is often handy.
Once you understand the main programming concept, you can easily start to write in any language. As for tricky particularities... Every language has a lot of them, you're just starting to feel and understand them with the experience.

I don't get what you're trying to say. As far as I got it, when you try to use something out of array range, the program will then fetch some unknown value from RAM. It can lead to some crazy output values, sure, but I don't know about losing data (correct me if I'm wrong and/or provide references for this statement).
You are right, it works this way in C and C++, but it isn't always like that. For example, an exception will be thrown in Java if you try to use an incorrect index.
A paltry man and poor of mind
At all things ever mocks;
For never he knows, what he ought to know,
That he is not free from faults.

Offline Ege

  • VIP
  • Hero Member
  • **
  • Posts: 856
  • Rank: Loc
  • Score: 39828
  • |m|
Re: For C programmers...
« Reply #25 on: November 18, 2016, 09:11:08 am »
Altough I don't understand a single shit this topic is fun to read lol

Offline Spectre

  • VIP
  • Hero Member
  • **
  • Posts: 622
  • Rank: Unknown
  • Score: Unknown
  • Grossly incandescent
    • Team: No Terrorism
Re: For C programmers...
« Reply #26 on: November 18, 2016, 12:10:33 pm »
Altough I don't understand a single shit this topic is fun to read lol

It's better that you don't, trust me :P

Offline Storm

  • VIP
  • Sr. Member
  • **
  • Posts: 278
  • Rank: Snitch
  • Score: 1263
  • Respect if you want to be respected
Re: For C programmers...
« Reply #27 on: November 18, 2016, 12:38:54 pm »
As much as I know C, C++ and Java, I still prefer web development in PHP

Offline Spectre

  • VIP
  • Hero Member
  • **
  • Posts: 622
  • Rank: Unknown
  • Score: Unknown
  • Grossly incandescent
    • Team: No Terrorism
Re: For C programmers...
« Reply #28 on: November 18, 2016, 01:07:19 pm »
As much as I know C, C++ and Java, I still prefer web development in PHP

I like front-end better tbh (HTML, CSS, JS), but I'll start learning PHP this year as well. Should be fun. Although, I heard people are starting to use Node.js more than PHP nowadays

Offline Storm

  • VIP
  • Sr. Member
  • **
  • Posts: 278
  • Rank: Snitch
  • Score: 1263
  • Respect if you want to be respected
Re: For C programmers...
« Reply #29 on: November 18, 2016, 02:54:27 pm »
As much as I know C, C++ and Java, I still prefer web development in PHP

I like front-end better tbh (HTML, CSS, JS), but I'll start learning PHP this year as well. Should be fun. Although, I heard people are starting to use Node.js more than PHP nowadays

Actually, PHP is more on-demand right now. Especially if you know the framework Laravel