C30N9

Members
  • Content count

    1331
  • Joined

  • Last visited

5 Followers

About C30N9

  • Rank
    Senior Member
  1. In an assignment, I created a simple memory game.

    Memory Game.

    Also, I have a question. Which is better to use to close the program? System.exit(0)? Or dispose() to destroy the window, which already terminates the program on close?

    1. scifista42

      scifista42

      There is a glitch: If you press "New Game" multiple times in succession, and then try to select a card, a random different card will be selected instead.

  2. Here is a code for a calculator using GUI and event driven programming. One annoying problem is that the numbered buttons call the action method twice. Suggestions to improve the program is much appreciated.

    EDIT: An error found. Putting 0s in front of numbers might manipulate the calculation a bit.

    code:
    Array

    1. Maes

      Maes

      Regarding the registering each action twice, that's because you're just listening for all events without filtering them out by type (e.g. button pressed, button released, etc.), and even buttons can generate many kinds of events, not just "pressed". Whenever you click on a button, you generate at least a "button pressed" and "button released" event, while I think there's also a "button clicked" event which only triggers upon a complete press-release cycle.

    2. C30N9

      C30N9

      It should invoke ActionPerformed(ActionEvent e) on press and release. The +, -, * and / buttons work fine.

    3. C30N9

      C30N9

      It turns out that I added the listener two times, that's why it happened. Still though the program does not work perfectly, as I said, when I write 0s in front of numbers, values are manipulated. There must be something faulty in the library I imported. Any suggestions?

  3. I've been asked to write a method which is public static Object max(Object[] a). It should return the highest Object in an Object array using compareTo method. However, with this method's parameter, since Object doesn't have compareTo method, I'm supposed to put several if statements to determine if the object is an instanceof Account, or Employee, or other classes that use compareTo:

    code:
    Array
    So I changed the function to this:
    code:
    Array
    Which works fine, and I think it's similar to java.util.Arrays.max(anArray). But I heard that it's not a very good idea to do that. Is it healthy to do this?

    1. TwinBeast

      TwinBeast

      Maybe it's slow? I did something like that to find the closest color from a 256 palette as a glsl shader and it made the game run 1 to 5 fps.

    2. AndrewB

      Your solution is better. Tell your friend that he's stupid.

    3. Cacatou

      Cacatou

      If your class doesn't have the compareTo function then your method won't work but assuming that all your classes have compareTo then your method will work perfectly.

  4. I'm supposed to write a method that acts like java.lang.String.split, except that this new method returns a String array including the delimeters. For example:

    split("Java#HTML#Cpp", "#") would return "Java", "#", "HTML", "#" and "Cpp" in an array. I already done the hard part, which returns the array correctly. However, I don't how to make it work with regular expressions, such as:

    split("Java#HTML$Cpp%Python", "[#$%]"). It should return:

    "Java", "#", "HTML", "$", "Cpp", "%", "Python".

    Any ideas?

    Here is my code BTW:

    code:
    Array

    1. fraggle

      fraggle

      Instant fail because "String regex" is not interpreted as a regex. You need to either compile that argument to a java.util.regex.Pattern, or (better, if possible), change the type of that argument from String to java.util.regex.Pattern.

      Read up on what a regular expression is if you don't know already. You're supposed to split on a pattern, not a fixed string. Don't try to write your own regex parser.

      Final point, you should never need to call new String(). Strings are immutable in Java.

    2. C30N9

      C30N9

      Done it without using Pattern class. Not actually a regex, but it involves using the square brackets for multiple delimeters. I know it is useless, but I tried to make it without using Pattern. And yes, messy code is messy:

      code:
      Array

    3. fraggle

      fraggle

      C30N9 said:

      Done it without using Pattern class.

      Assuming the assignment requires the second argument to be a regexp, if I was running the class you'd get a fail mark. What you have implemented is not a regexp.

    4. C30N9

      C30N9

      fraggle said:

      Assuming the assignment requires the second argument to be a regexp, if I was running the class you'd get a fail mark. What you have implemented is not a regexp.


      This problem is put very early in the book, probably a mistake, as it didn't discuss regular expressions at all in the chapter. My instructor limited me to making this, not an actual regex. I kept the method naming the same though, that's why I didn't understand "regex" at first.

    5. fraggle

      fraggle

      Fair enough then. Kind of weird though.

      My one criticism would be that isPartOfArray doesn't need to exist. Take regexChars as a substring and just check if regexChars.indexOf(whatever) >= 0. You should be able to get rid of indexOfRegexChar as well.

  5. So I want the output to be this:

    ____5 (four spaces)
    ___45 (three spaces)
    __345 (and so on...)
    _2345
    12345

    Using without more than 2 loops. Doesn't matter any language, but C or Java works.

    1. Cacatou

      Cacatou

      int k = 5;
      int l = 5;
      int j = 5;
      for(int i = 1; i <= k; i++)
      {

      if(i==l) //Do not need to print any more spaces
      {
      while(l<6) //Print all the numbers from l to 5
      {
      System.out.print(l);
      l++;
      }
      j--;
      l=j; //Reduce the value of l to 1 less than its value before the loop
      i=0; //Reset i
      k--; //Reduce the number of spaces required on the next line
      System.out.println();
      }
      else
      {
      System.out.print(" "); // Print a spacebar
      }
      }

      This will be the body of your main function. What the code does is it loops until it prints out 6 things per line. The moment i equals l it stops printing spaces and instead starts printing the numbers. k denotes the number of spaces to print per line. When the value of k becomes too small the loop exits.

    2. YukiRaven

      YukiRaven

      This uses a single loop, and you can pass any number (and any output stream) you want into the function. The key is constructing the formatting string on the fly.

      code:
      Array
      This is a simplified version that only write one line so you can more easily see the format string.
      code:
      Array
      EDIT:
      Faster version. Common Lisp is slow when working with lists (which I always found funny since "LISP" stands for "LISt Processor"), so I replaced that with just strings, which are vectors internally. Also, I forgot about the implicit loop in my FORMAT statement above, so I took that out. This is truly one loop now.
      code:
      Array
      The number 9999 now takes 4.243 seconds on my Core i7, 0.080 of which went to the garbage collector.

    3. Ribbiks

      Ribbiks

      here's a python one-liner for ya:

      code:
      Array
      yay.

    4. Memfis

      Memfis

      lol being able to multiply strings like that is pretty neat

    5. andrewj

      andrewj

      C version using printf formatting:

      code:
      Array

      and C version using a char array:
      code:
      Array

  6. Yesterday, in my C programming lab, I was solving problems with recursions, the hardest part was that some of them required using variables inside recursive functions for better solutions. I tried to avoid declaring variables inside them as that would harm the memory, so I ended up declaring global variables, which ended in a mess. Here is an example of my functions:

    code:
    Array
    The number "10" in the above code is the size of the array I declared later in the main function. I could use a constant value of 10 in SIZE, but a better way is to put another integer variable in the function to store length value like the comment I wrote, but this will make it change its value to the new length, and it will keep declaring integer type variable.
    code:
    Array
    This messy function is supposed to check if a string is a palindrom string. length's value is the length of the character array "LeooeL", same issue as the first code (putting the length as a variable). z (didn't bother to pick a name for it) should be used to indicate whenever it should stop checking, but I didn't finish the function, so I put a comment for each statement concerning it, as it's the not the issue here. Notice where I declared the variables, not favorable.

    1. YukiRaven

      YukiRaven

      I'm not an expert on recursion by any means, but as long as you call pali() in the return statement, and your compiler supports tail recursion, inside variables shouldn't harm memory at all. The stack frame will just be replaced.

      I'm not going to try to write this out in C code, so here's my implementation in Common Lisp:

      code:
      Array
      Or how we Lispers would actually do it...
      code:
      Array
      EDIT:

      For fun...
      code:
      Array
      And some timing...
      code:
      Array

    2. fraggle

      fraggle

      C30N9 said:

      Yesterday, in my C programming lab, I was solving problems with recursions, the hardest part was that some of them required using variables inside recursive functions for better solutions. I tried to avoid declaring variables inside them as that would harm the memory, so I ended up declaring global variables, which ended in a mess.

      Yeah, that sounds like a terrible mistake.

      Don't worry about "harming the memory" - unless you're allocating some huge thing on the stack with every recursion, or recursing to some incredible depth, it's not going to matter.

      If you're using global variables with recursion then you're almost certainly doing recursion wrong. Actually, the entire point of having stack variables is to support recursion / reentrancy in a safe way.

      In general, it's best avoid using global variables. They cause a whole bunch of headaches. Instead, try to structure your code to be as stack-based as possible.

      I think you wanted something like this:

      code:
      Array

    3. scifista42

      scifista42

      In short: Each instance of a recursively called function will keep its own local variables for itself, which is perhaps the main advantage of recursion and the reason why it's being used to solve tasks.

    4. C30N9

      C30N9

      @fraggle:

      code:
      Array

    5. Jonathan

      Jonathan

      C30N9, that extra else if clause you inserted isn't necessary. The && logical operator is short-circuiting in C, meaning that if the left side evaluates to false, the right side recursive call will not be executed, and the result will be 0, which will be returned.

    6. C30N9

      C30N9

      That's new. Neat.

      Any solutions for the first function?

    7. fraggle

      fraggle

      Jonathan said:

      C30N9, that extra else if clause you inserted isn't necessary. The && logical operator is short-circuiting in C, meaning that if the left side evaluates to false, the right side recursive call will not be executed, and the result will be 0, which will be returned.

      And even if C didn't have short-circuiting, the c[0] == c[len - 1] part in the third return statement would be redundant.

      Actually you can simplify further to just:

      code:
      Array

  7. EDIT: Nevermind, I misunderstood it at first. I thought that when I add "6", it would add to array[5] (or the sixth element of the array). Problem solved.

    This is for an assignment. I'm not asking for the solution, but I'm a bit confused:

    Write a C program to create and manipulate a one dimensional array (list) of up to a hundred sorted (ascending order) numbers by using functions to insert, remove, or print elements as follows:
    - Function insert: will search for the appropriate position of a given element and if the element is already in the list will display an error message. If not, the function will shift all the elements starting from the position of the element to the right of the array and then insert the element into that position.
    - Function remove: will search for the element to be removed and if not found will display an error message. If the element exists, the function will remove it and shift all the elements that follow it to the left of the array.
    - Function print: will print the elements that exist in the array at that point.
    Your program should display a menu to the user that allows him/her to insert, remove, or print as many elements as they want until they want to stop. If an element is inserted into an already full list, an error message should be displayed. The array (list) at the beginning should be empty.

    Example of a sample run:
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    2
    List is empty, No change
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    1
    Enter element to insert
    6
    Element 6 is inserted
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    1
    Enter element to insert
    9
    Element 9 is inserted
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    1
    Enter element to insert
    7
    Element 7 is inserted
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    2
    Enter element to remove
    112
    Element 112 does not exist, No change
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    2
    Enter element to remove
    6
    Element 6 is removed
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    3
    Elements in list are:
    7
    9
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    1
    Enter element to insert
    7
    Element 7 already exists, No change
    Enter your choice: 1) insert 2) remove 3) print 4) exit
    4
    Goodbye


    It seems to me that the example is wrong, shouldn't element 7 become 6? And what if the 100th element (99) is shifted to the right? They didn't say anything about the limits.

  8. Today, in my Calc II exam, I've came across the question of finding the derivative of an inverse function when x = 2. The function was as I remember:

    f(x) = e ^ 2x + 4x + 1

    I didn't know how to solve for x algebraically after I substituted f(x) with 2 (or using ln or exp functions). So I put automatically x = 0 as it will satisfy the equation and then I proceeded. Luckily, it was a multiple choice question, so the method of solving isn't important.

    How do I solve this?

    1. Pure Hellspawn

      Pure Hellspawn

      okay my calculus is a little rusty but...

      the 4x + 1 is easy: 4.

      e^2x is 2e^2x

      http://www.tomsmath.com/derivative-of-e-to-the-2x.html

      so the derivative is 2e^2x +4.

      f'(0) = 2e + 4.
      f'(2) = 2e^4 + 4.

      if you mean to find the derivative AFTER you plug in x: you don't have any variables and the derivative of a constant is always 0.

    2. C30N9

      C30N9

      It seems you didn't understand the question. I need to find the derivative of the inverse when x = 2 in f(x) inverse (y = 2 in normal f(x)). I need to get the x value in f(x) to solve the known equation for finding the inverse derivative:

      (df inverse/dx) "when x = 2" = 1 / (df/dx) "when x = f(2) inverse"

    3. fraggle
    4. C30N9

      C30N9

      fraggle said:


      You mean what I just said? :P

    5. Ribbiks

      Ribbiks

      code:
      Array
      Somehow I doubt that's how they expected you to solve it. Unless they actually teach kids the Lambert W function in Calc II nowadays? There's probably some simpler trick I'm not aware of, been ages since I touched this stuff.

    6. C30N9

      C30N9

      That's correct, I got 1/6 during the exam.

      They do teach complex numbers in Calc II, just not before transcendental functions. I don't know if it includes Lambert W function though.

      It's not fair to include such question in this exam if this is the only solution.

    7. Fredrik

      Fredrik

      You don't really need to know about the Lambert W function unless you're solving a more general version of the problem. Here you just need the inverse at one specific value, y = 2, and since it's an exam problem, this point was naturally chosen so that the input x = 0 can be found by just inspecting the equation.

      This is a typical feature of exam problems, less common in real world problems.

  9. This code is supposed to calculate e to the power x. It works fine at lower numbers, but then at x = 3.6, the output starts to decrease as x goes up. What's wrong?

    C language.

    EDIT: The problem was that "factorial" function was an int, so at high values, it won't work.

    code:
    Array

    1. Phml

      EDIT: Nevermind, solved it.


      There will be a day when you will curse yourself for uttering those very words.

    2. C30N9

      C30N9

      Phml said:

      There will be a day when you will curse yourself for uttering those very words.


      Nope.

      It was an assignment, and I'm already done with it. I'm not going to touch this program again.

      I eat programming for breakfast.

    3. Phml

      This is not about your assignment or your programming abilities. No programmer should need this simple logic to be spelled out, but here goes.

      Inevitably, one day you will be looking for the answer to a specific problem, and the only result will bring you to a five years old topic with the final answer being "EDIT: Nevermind, solved it".

      There's no point in editing a post out. It accomplishes nothing and deprives other people of the solution.

      On the other hand, even an egocentric can benefit from altruistic behavior due to reciprocity. When scratching someone's else back costs literally nothing and when not doing so takes actual effort (minimal as it may be, in blanking out a post), the second option is objectively worse. Why do it?

    4. AndrewB

      I'm pretty sure this forum can't be Google searched anyway.

    5. GreyGhost

      GreyGhost

      Looks like it can't, but you can Google search member blogs, so at least the OP can be read without logging into the forums.

    6. Memfis

      Memfis

      When you ask a question like this and then notice your own mistake, it can make you feel embarassed, especially if it's something that seems totally obvious now that you've spotted it. Naturally, you don't want other people to laugh at you so you hide the evidence of your stupidity. I can understand that even though Phml is probably right.

    7. C30N9

      C30N9

      ... wat???

      I just deleted the code without thinking out of speed. No, I wasn't embarrassed.

      I also might didn't get Phml's joke/post at first (if he was referring to leaving the post for others).

      Regardless, I edited the first post.

      (BTW, how could people reach this thread normally?)

    8. GreyGhost

      GreyGhost

      C30N9 said:

      (BTW, how could people reach this thread normally?)

      They need to login to the forums. The Google bot probably hasn't a Doomworld account so it can't index past the OP.

    9. scifista42

      scifista42

      C30N9 said:

      (BTW, how could people reach this thread normally?)

      Do you mean without manually typing /blogs in the forum url?

      1. Click the button "Blog" below any of your posts on the forum.
      2. Click "Comments".

      Step 1 can be done by anyone. Step 2 requires being logged on a forum account.

  10. That's not a dragon, that's a baby dragon. :P Yep, nearly everyone mistake it for a dinosaur. The real reason I called it a dragon because the picture I used to model on was called a dragon. :P
  11. Don't let their cute faces fool you. (I made the model)
  12. I'm majoring in computer science (first year), and I'm thinking of taking minor in math. The reason is because of its connection to computer science (since it's a sub-field of math), and I like it a lot. The question is, are these a good pair?

    For CS degrees, math courses include Calculus I and II, Discrete Mathematics, Statistics, Linear Algebra and Numerical Methods. As for the mathematics minor, it includes Calculus III, Foundations of Mathematics, Normal Differential Equations, Mathematical Analysis I, Abstract Algebra I and other math courses in the choice of the student.

    I feel getting a minor in math is a waste of time. It's because where I live, computer scientists and engineers usually end up in programming jobs, or maintaining computers. So it's very likely that I won't work in the "theoretical" part of computer science, and I "assume" that math is applied more on the theoretical part than programming (or software engineering).

    Shall I forget it and pursue other minors, or am I being ignorant about how good it is to study such pair?

    1. Maes

      Maes

      Pretty much all oldschool CS profs I know started out as Math majors, with a specialization in CS acquired on top of that. That was before most universities introduced a separate/more technical CS curriculum, as well as the various "computer engineering" ones.

      So the two are definitively compatible, but the only reason for pursuing them together today would be if you're interested in an academic career or working in a specialized field such as cryptoanalysis or numerical computing (which usually point to an academic/research career anyway).

    2. YukiRaven

      YukiRaven

      When I was a CompSci major, I was required to minor in math (with a 2nd minor of my own choice; I chose Japanese). At first it didn't seem necessary, but as I got into tougher classes, such as Computer Security, I saw the reason. Unfortunately I can't math to save my life, and thus I switched to a BA in Applied Computing, which didn't require the math.

      However, the "BS in Computer Science" they offered didn't actually cover the things I'd need to be a programmer in-the-large. They said it did, but not really. They instead expected people in the BS program to think about things in a more academic way, then go off to do research or something. Sure, you learned some good programming skills (C/C++, C#, some basic x86 assembly in Intel syntax, Prolog, and how to construct certain data structures like lists and queues), but their focus was more on making you a computer scientist. This is why I switched to the BA, which was more along the lines of "we're teaching you to be a programmer". But that was my specific school, which didn't exactly have a stellar CompSci department.

      On the other hand, a good foundation in at least some of the science underlying programming is good to know. Plus, certain fields will require you to know some of the higher maths. 3d programming and linear algebra comes to mind.

      Disclaimer: I never finished college.

  13. That would be better, give it to someone else. I don't want to delay the project because of my laziness.
  14. I'm sorry, but I read the message recently. Here is E1M6: http://speedy.sh/tj4d3/dm16-c30n9.wad
  15. I'm currently majoring in Computer Science, but it is possible to switch to Computer Systems Engineering. In general, CS should focus more on math, theories and software-related stuff than CSE, but after looking through my university's guide for BA degree, it seems that CS is a bit "contained" in CSE. For example, CSE students take several courses that are essential to get into software engineering (programming, data structure and management, and software engineering courses), with another courses related to Electrical Engineering and hardware stuff, but some share the same name of other CS courses (AI, Operating Systems, Algorithms). Plus CSE students can take some courses in CS as elective courses.

    You have to be good in Physics and Mathematics to switch or to do well, which I am good at both. I just keep feeling that I'm wasting a chance to study engineering and become good at both software and hardware rather than only focusing on Computer Science. At the end of the day, either degrees will likely get me the same job, but an engineer is usually generally known as being smarter (because he is an "engineer", plus the requirements to get engineering are harder than the ones to get into CS).

    Should I stop listening to people who keep telling me that I've took the wrong choice choosing CS over CSE even though I can make it through in the latter major?