|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On January 08 2014 03:17 Arnstein wrote:I was bored, so I just did some random shit in C, but why doesn't it print the sum? + Show Spoiler + #include <stdio.h> #include <time.h>
int main() { int sum = 0; while ( 1) { srand(time(NULL)); sum += rand() % 6 + 1; printf( "Summen er: \n", sum ); } }
It shows "Summen er" when I run it, but not the int!
Missing the %d to indicate where in the string to print sum actually should go.
+ Show Spoiler +printf( "Summen er: %d\n", sum );
|
|
|
Why an endless loop? Put some brakes on it with return or break...
|
CTRL+C does the trick I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop.
|
You may want to switch that to a for loop to avoid perma-while looping your program.
|
On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop.
Then while (true) may be better to write instead. It's more readable than while (1)
Or even better:
bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import
#include <stdbool.h>
if you use the boolean approach.
#############################
I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general?
|
On January 08 2014 07:55 darkness wrote:Show nested quote +On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach.
There's nothing wrong with a while(1) if you don't care or don't want your program to terminate.
However, it is bad juju to "break;" out of an infinite loop, as it may hide the condition to branch from the compiler, and can prevent some of the loop optimizations. I'm not saying never use "break;", just letting ya'll know the ramifications.
I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general?
It's definately the way I would do it, but I'm not sure I would advise that way if you're trying to do everything the Java (tm) way.
If you're copying Smalltalk's idea that every noun is an object, then try thinking of the data, both unsorted and sorted, as an object. Create a class that wraps that data array, and a method that mutates the wrapped data.
Java also pulls from the C/C++ land of OOP. Alternatively, if the data array is supposed to be a static element, then make a wrapper object, with a static method to sort the object passed as a parameter. By just passing in the data array without making a wrapper object, you're putting in a shortcut.
Or, a more savvy approach would be to just use the Arrays.sort method, and make sure your data array is Sortable.
It's been a while since I've done any real projects using Java. The promised land is Plain Old Data and speed.
|
1 looks like a typical case of a magic number to me. It's just sloppy/lazy coding imho. 'true' should be more readable for most people. The guy seems to be a beginner, so he shouldn't pick up bad habits like magic numbers.
|
On January 08 2014 08:18 darkness wrote: 1 looks like a typical case of a magic number to me. It's just sloppy/lazy coding imho. 'true' should be more readable for most people. The guy seems to be a beginner, so he shouldn't pick up bad habits like magic numbers.
If you see a while(1) loop, you can instantly recognize what it is doing the same as while(true).
"Magic" numbers are magic because there is no way to tell what it is they represent, and in this case, he's programming using C, where any non-zero number represents a true value.
http://en.wikipedia.org/wiki/Magic_number_(programming)#Accepted_limited_use_of_magic_numbers
"The constants 1 and 0 are sometimes used to represent the boolean values True and False in programming languages without a boolean type such as older versions of C."
|
don't really wanna make a new thread for this simple question but: as someone new to programming and wanting to try it to see if it is for me, what website/resources should I try? I've used codecademy/code.org a bit but they seem far too simple? surely this is not what programming is really like? Also I am not really sure if programming is something I want to pursue as a career/at university. Anyone got any general advice from their own experience?
|
On January 08 2014 08:50 Scorpion77 wrote: don't really wanna make a new thread for this simple question but: as someone new to programming and wanting to try it to see if it is for me, what website/resources should I try? I've used codecademy/code.org a bit but they seem far too simple? surely this is not what programming is really like? Also I am not really sure if programming is something I want to pursue as a career/at university. Anyone got any general advice from their own experience?
Both me and my girlfriend learned to program from the Non-Programmer's Guide to Python which I found incredibly good in grade eleven, it presents everything really readably and simply... just nice to use in general. I plug that guide a lot whenever anyone asks me how to learn programming.
|
On January 08 2014 08:50 Scorpion77 wrote: don't really wanna make a new thread for this simple question but: as someone new to programming and wanting to try it to see if it is for me, what website/resources should I try? I've used codecademy/code.org a bit but they seem far too simple? surely this is not what programming is really like? Also I am not really sure if programming is something I want to pursue as a career/at university. Anyone got any general advice from their own experience?
It depends what you want out of it. If you just want to check stuff out without anything really advanced then coding in ANSI C or C++ for console input/output only is the way to go in my opinion. This is relatively easy for simple stuff and gives you a very solid background if you'd like to move to something more complex or even entirely different (OOP, GUIs etc.).
Another way to approach this would be to grab something that doesn't require a lot of setup (assuming you're using Windows as your OS) and won't require manual compilation, debugging and all that crap. For that you could simply install Eclipse for Java as you get everything you need there and can run your programs any time you want to see how they work.
Other people will most likely suggest other things, but that's what worked for me in the beginning. I wouldn't get into PHP early on for example since it requires a ton of setup most of the time (PHP itself, virtual servers etc. etc.), at least on Windows I remember I used to get some headaches from that.
|
On January 08 2014 07:55 darkness wrote:Show nested quote +On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach. ############################# I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general?
loopShouldContinue clearly does express the intention of the programmer precisely enough, it's even less expressive than while(1)!
try: while(sinceThisProgramIsJustForMyOwnExperimentationThisLoopWillJustRunUntilITerminatedAtMyDiscretinonWithCtrlC)
|
On January 08 2014 09:31 Manit0u wrote:Show nested quote +On January 08 2014 08:50 Scorpion77 wrote: don't really wanna make a new thread for this simple question but: as someone new to programming and wanting to try it to see if it is for me, what website/resources should I try? I've used codecademy/code.org a bit but they seem far too simple? surely this is not what programming is really like? Also I am not really sure if programming is something I want to pursue as a career/at university. Anyone got any general advice from their own experience? It depends what you want out of it. If you just want to check stuff out without anything really advanced then coding in ANSI C or C++ for console input/output only is the way to go in my opinion. This is relatively easy for simple stuff and gives you a very solid background if you'd like to move to something more complex or even entirely different (OOP, GUIs etc.). Another way to approach this would be to grab something that doesn't require a lot of setup (assuming you're using Windows as your OS) and won't require manual compilation, debugging and all that crap. For that you could simply install Eclipse for Java as you get everything you need there and can run your programs any time you want to see how they work. Other people will most likely suggest other things, but that's what worked for me in the beginning. I wouldn't get into PHP early on for example since it requires a ton of setup most of the time (PHP itself, virtual servers etc. etc.), at least on Windows I remember I used to get some headaches from that. I mean, if you really want to not have fun when you start coding, C/C++ seems like a viable option. C++11 is a lot better in that regard (at least for me), but I don't think the changes from C++11 make the language easier to learn. Python is one of the best languages for beginners, I think. The syntax is so simple and lacks the cruft that C-style languages have (semicolons, curly braces and so on, basically just things that make life easier for the compiler) which makes learning a lot less frustrating. The most important part of learning programming is actually doing programming and trying stuff out and you won't do that if it's not fun. Programming in Python is fun for a beginner. Programming in C/C++, not so much.
By the way, don't take this the wrong way, C/C++ are both very important languages, but they serve a fairly specialized purpose nowadays. (systems programming or performance critical software like OS, games, computer graphics, realtime stuff and so on)
|
Why do you hate curly braces so much? They are awesome, especially in the beginning, when they make it so much easier to identify blocks of code. That is, of course, if someone doing the programming actually uses some consistent formatting. It's a great way of learning that at start to put curly braces where they belong (not just around functions but all if statements, loops etc.)
Personally, I think that for someone just starting out this:
int main(void) { for (int i = 0; i < 5; i += 1) { if (condition) { do_stuff(); } } return 0; }
might be more readable and easier to break into parts than this:
int main() for (int i = 0; i < 5; i += 1) if (condition) do_stuff(); return 0;
The above examples are incredibly simple, but as your programs grow and acquire more methods it might be a bit hard to follow for the beginner. When I was starting out I even put /* comments */ after the closing bracket for a method with its name in it, made moving around much easier.
|
Hey all - I'm trying to get an idea of how an XMLHttpRequest is implemented in browsers. I mean I know what they are, I'm just trying to view the source that implements them. This doesn't serve any practical purpose, I was just curious.
From what I understand, I should look at the source code for v8 or Spidermonkey? I pulled the v8 source code with git from https://code.google.com/p/v8/wiki/UsingGit and did a $ grep -rin "XMLHttpRequest" in v8/, but I can't seem to see where it's implemented. I guess I'm looking in the wrong place?
Any ideas?
|
On January 08 2014 09:50 nunez wrote:Show nested quote +On January 08 2014 07:55 darkness wrote:On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach. ############################# I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general? loopShouldContinue clearly does express the intention of the programmer precisely enough, it's even less expressive than while(1)! try: while(sinceThisProgramIsJustForMyOwnExperimentationThisLoopWillJustRunUntilITerminatedAtMyDiscretinonWithCtrlC)
Don't be so grumpy. The name of the boolean variable can be easily renamed, and it was just an example. You're just acting like an ass at the moment with your sarcasm.
|
On January 08 2014 07:55 darkness wrote:Then while (true) may be better to write instead. It's more readable than while (1) Maybe if you've only worked with more modern programming languages. I think any person who knows enough C to know what while(1) means will read it just as well as while(true).
While I personally would have used true over 1 if it was part of the standard syntax, I feel it's overkill to a big degree to include a header or write macros etc yourself just to make something so simple as while(1) clearer. Especially when I don't intend for anyone else to work with my code.
|
On January 08 2014 11:30 Manit0u wrote:Why do you hate curly braces so much? They are awesome, especially in the beginning, when they make it so much easier to identify blocks of code. That is, of course, if someone doing the programming actually uses some consistent formatting. It's a great way of learning that at start to put curly braces where they belong (not just around functions but all if statements, loops etc.) Personally, I think that for someone just starting out this: int main(void) { for (int i = 0; i < 5; i += 1) { if (condition) { do_stuff(); } } return 0; }
might be more readable and easier to break into parts than this: int main() for (int i = 0; i < 5; i += 1) if (condition) do_stuff(); return 0;
The above examples are incredibly simple, but as your programs grow and acquire more methods it might be a bit hard to follow for the beginner. When I was starting out I even put /* comments */ after the closing bracket for a method with its name in it, made moving around much easier. I don't hate curly braces, I actually code most of my stuff in languages that have them (Scala, Java, C#), but I don't think they make code easier to read, especially for beginners. Python's syntax is essentially pseudocode as sourcecode. Your example would look something like this:
def main(): for i in range(0, 5): if condition: do_stuff()
The thing is also that C and C++ are rather verbose languages. A lot of higher-level things like file IO, networking, parsing XML or whatever else you want to do are pretty complicated or require libraries. Python has all of the above (and more) built in and it's very easy to use as well.
|
On January 08 2014 20:05 darkness wrote:Show nested quote +On January 08 2014 09:50 nunez wrote:On January 08 2014 07:55 darkness wrote:On January 08 2014 04:36 Arnstein wrote:CTRL+C does the trick  I just wanted to look at how/if the number would go to 3.5 if I would take the average of X dice rolls, and then I could just CTRL+C when I wanted to stop. Then while (true) may be better to write instead. It's more readable than while (1) Or even better: bool loopShouldContinue = true; while (loopShouldContinue) { // do your work // when you're finished loopShouldContinue = false; // or you could write break; // but then that may make the point of the boolean variable irrelevant // it just depends on what you want to do exactly }
Don't forget to import #include <stdbool.h>
if you use the boolean approach. ############################# I've got a question. When you don't need a class instance to do something (e.g. to get an unsorted array and return a sorted one), is it advisable to make that method static in general? loopShouldContinue clearly does express the intention of the programmer precisely enough, it's even less expressive than while(1)! try: while(sinceThisProgramIsJustForMyOwnExperimentationThisLoopWillJustRunUntilITerminatedAtMyDiscretinonWithCtrlC) Don't be so grumpy. The name of the boolean variable can be easily renamed, and it was just an example. You're just acting like an ass at the moment with your sarcasm. your post was well-intentioned, but misguided and misplaced nitpicking, borderline overbearing... it persists!
|
|
|
|
|
|