Yeah it looks like it would be just a left shift, then I have to add in the next hex to that one. I think.
For reference:
0000 0000 0000 0000
The furthest bit to the right is bit 0, right? If so, that means that has to be read first. Damn this will be harder than I thought. I may just turn in my single program without this one... but 10 points out of 50 (-5 already for a day late) is a lot of points.
public int groupSize(int row, int col)
{
if(row < 0 || col < 0 || row >= matrix.length || col >= matrix[0].length)
return 0;
if(matrix[row][col] != SET)
return 0;
matrix[row][col] = VISITED;
return 1 + groupSize(row + 1, col) + groupSize(row - 1, col) + groupSize(row, col + 1) + groupSize(row, col - 1);
}
If you think about this, how bad can this be in the worst case? I'll ignore the fact that only 20% of the squares are filled for now.
There is a possibility that every point on the grid is SET, in which case you need to check every square. Because of the nature of your algorithm, there are at most 4 squares from which you can access any given square, meaning that groupSize is called at most 5 times (if you start with the square you might hit it 5 times) for each square in the grid.
This means that the total running time is proportional (some number of comparisons and additions and things are used) to 5(size of grid) = 5n^2 if n is the side-length.
Which means it's O(n^2), or quadratic.
You can see that only having 20% or fewer of the squares filled would only make a constant difference to this running time.
The best case is obviously constant (if a SET point is surrounded by all EMPTYs).
A more interesting question is the average case. That would require some more thought though and it's late.
That is a gnarly recursion to solve, so I think this approach is probably much easier.
public int numberOfGroups()
{
int ctr = 0;
for(int i = 0; i < matrix.length; i++)
for(int j = 0; j < matrix[0].length; j++)
{
if(matrix[i][j] == SET)
{
groupSize(i, j);
ctr++;
}
}
return ctr;
}
Approach this one in the same way as the above one. Think about, on a per-square bases, how many times groupSize can be called in the worst case. As in the above example, it is 5. Once it and all its surrounding squares are set to VISITED or EMPTY, it will never be visited again. Thus we see that despite calling a quadratic time function a few times, this function is still just quadratic in the worst case!
Sorry to stack yet another question on top of the two above, but I have a question that might go unanswered. Basically I have to create a program (in assembly) that will take 16 bits and allow me to store two chars into it.
Basically, lets say someone enters the letters "CD" into the program. I want to be able to store it in memory as: x4344 instead of two separate: x0043 and x0044. I just can't think of a logical way of doing it. If the user enters C, then R0 gets x0043... But how do I move that to the beginning? Converting it to x4300 seems a little difficult in assembly.
Heh, just looked at the LC-3 instruction set. Very minimalistic, I have to say. Yeah, you do appear to be lacking a bit shift instruction... but you do have an alternative.
Do you remember in higher level programming languages that you might do multiplications by bit shifting?
e.g. nNewValue = nOldValue << 1; // Multiply nOldValue by 2
?
You can also go the other way - do bit shifts by multiplying. Now, you don't have an multiply instruction... but you do have an addition instruction...
I was thinking that as well. I am mentally exhausted right now so I'll look into that in the morning. But that is a very good idea... I just wonder if I can implement it well enough. That's the problem. (That and even if I make x0044 into x4400 how do I add the next char?)
I was thinking that as well. I am mentally exhausted right now so I'll look into that in the morning. But that is a very good idea... I just wonder if I can implement it well enough. That's the problem. (That and even if I make x0044 into x4400 how do I add the next char?)
Logical or.
You've got logical "and" and logical "not", and every single logic instruction can be made up out of "nand"s.
another question
this is part of the code in the test class our teacher wrote for us:
// Start timing the groupsSize method
startGroupSizeTime = System.currentTimeMillis();
int size = g.groupSize(c.getRow(), c.getCol());
stopGroupSizeTime = System.currentTimeMillis();
totalGroupSizeTime += (stopGroupSizeTime - startGroupSizeTime);
// Reset the matrix to be used again
g.resetMatrix(c.getRow(), c.getCol());
// Start timing the numberOfGroups method
startNumberOfGroupsTime = System.currentTimeMillis();
int numberOfGroups = g.numberOfGroups();
stopNumberOfGroupsTime = System.currentTimeMillis();
totalNumberOfGroupsTime += (stopNumberOfGroupsTime - startNumberOfGroupsTime);
// Reset the matrix to be used again
g.resetMatrix();
// Start timing the listGroups method
startListGroupTime = System.currentTimeMillis();
g.listGroups(false);
stopListGroupTime = System.currentTimeMillis();
startListGroupTime += (stopListGroupTime - startListGroupTime);
// Reset the matrix to be used again
g.resetMatrix();
System.out.println("Grid size is " + theSize + " x " + theSize);
System.out.println(pctSet + "% elements set in grid");
System.out.println("groupSize time = " + totalGroupSizeTime);
System.out.println("numberOfGroups time = " + totalNumberOfGroupsTime);
System.out.println("listOfGroups time = " + totalListGroupTime);
System.out.println();
It's in the middle of a couple for loops that change the size of the matrix and the percent of the matrix that is filled; it calls a few of the methods I wrote and times them, then prints out the times. The problem is that no matter what the size of the matrix, and no matter what the percent filled (up to 87%, the highest I can go without getting a stack overflow), it always returns 0.0 for groupSize time (this is in milliseconds). Is there a flaw in my teacher's code that isn't obvious to me, or is my groupSize method really that fast?
Have you tested your methods to make sure they actually do the right thing and don't just return 0 all the time or something silly like that? They looked okay to me, but I might have missed something.
Also, it might just be that fast. Try setting up a loop that just runs that operation 100000 times (or whatever it takes to make it a little bit slow), then make some changes to GroupSize to make it slower and see if it affects it. If it does, then you're probably doing the right thing.
The thing is, if the thing is only 2000 by 2000, then we're talking on the order of 4000000 operations (probably a lot less), which isn't actually all that many unless you run it a bunch of times (like tens or hundreds of times).
SmasherStarting to get dizzyRegistered Userregular
edited October 2008
Something to consider is that there's no guarantee on the granularity of currentTimeMillis(). The result is in milliseconds, but that doesn't necessarily mean the underlying clock (which is OS dependent) will reflect time differences that small. You can test it with a loop.
Anybody know what the best way to got about getting PyGTK installed for Python 2.6 would be? The guy who built the all in one installer for 2.5 hasn't updated, and I don't think I've got the know-how to build it from source.
I'm in a class that uses Scheme and it's an honors or advanced version of the usual Intro to Comp Sci class. Right now we're about to take our first midterm and I've gotta tell ya, I'm fucking nervous.
I'm nervous for most tests, but for this I'm really panicked. In the past assignments I've been close, very close to getting most exercises right but I always need some help to get me to the final destination. In reality, this shit is not that hard. The answers are always right in front of me but I can never seem to actually grasp them.
Regardless, I'm worried. I want to be in computer development in future technologies after college, but I feel like I might be incompetent. I don't know what I'm asking for really, but I just want some reassurance - am I fucked for the majority of my future education and career?
I'm in a class that uses Scheme and it's an honors or advanced version of the usual Intro to Comp Sci class. Right now we're about to take our first midterm and I've gotta tell ya, I'm fucking nervous.
I'm nervous for most tests, but for this I'm really panicked. In the past assignments I've been close, very close to getting most exercises right but I always need some help to get me to the final destination. In reality, this shit is not that hard. The answers are always right in front of me but I can never seem to actually grasp them.
Regardless, I'm worried. I want to be in computer development in future technologies after college, but I feel like I might be incompetent. I don't know what I'm asking for really, but I just want some reassurance - am I fucked for the majority of my future education and career?
CS 145 at University of Waterloo by any chance? :P
(I took 135 back before 145 existed and thought it was really neat.)
No, you are not even remotely fucked. Scheme can be a difficult thing to grasp, and doesn't necessarily reflect on your programming ability. Is this the first programming you've ever done? It might just take a term or two to get used to it.
No, I did some experimenting with C in the summer (but that was from a book that was just "type what you read and watch magic!" kind). And sorry, not Waterloo.
I do enjoy it, I like being able to operate a machine that we rely upon for day-to-day use, and I know that there's no stopping the development of it to be even more incorporated in the future. What I'm struggling with is grasping the thought process our teacher invokes on us, and sometimes it just takes it's toll on me.
No, I did some experimenting with C in the summer (but that was from a book that was just "type what you read and watch magic!" kind). And sorry, not Waterloo.
I do enjoy it, I like being able to operate a machine that we rely upon for day-to-day use, and I know that there's no stopping the development of it to be even more incorporated in the future. What I'm struggling with is grasping the thought process our teacher invokes on us, and sometimes it just takes it's toll on me.
It's hard to give you any advice, since we don't know what your tests will look like, but thinking like a computer scientist takes time. I remember when I started with C in high school, our coding was shit-tacular. Even the teacher was pretty bad at it.
It's not in scheme, but I enjoyed Thinking in C++. Considering Scheme is a Lisp dialect and functional in nature, this book might not be that helpful. The upshot, though, is that you need to figure out the "native idioms" of a given language. How does Scheme "want" to be written?
If nothing else, go to your teacher and present your concerns. Ask what is likely to be on the test, and what you should study to prepare for it. Teachers are usually very eager to help students that ask for it.
are you using SICP as your textbook? because I read it last summer and really liked it. It gave clear and concise explanations about how scheme worked, and it taught me a huge amount.
It doesn't work as is, but I was looking at doing an XMLHttpRequest for the first submit instead. However, I only need to POST the data, I don't need to worry about getting the response. Any suggestions for how I can get this to work?
This question is a few days old, so you may have figured it out on your own already, but if you haven't:
That's a snippet from a Greasemonkey script I wrote earlier this year. The actual key information:
1) That content type header is required when you're submitting a POST request
2) The data you're posting must be in the format <name>=<value>, with ampersands separating the bits.
3) The data you send must be URI encoded
As for ignoring the response, you can do that if you want, but I would suggest at least checking the HTTP status code to make sure the server received your data and didn't error out on it for some reason. If you really don't care, just omit the onload entirely.
No, I did some experimenting with C in the summer (but that was from a book that was just "type what you read and watch magic!" kind). And sorry, not Waterloo.
I do enjoy it, I like being able to operate a machine that we rely upon for day-to-day use, and I know that there's no stopping the development of it to be even more incorporated in the future. What I'm struggling with is grasping the thought process our teacher invokes on us, and sometimes it just takes it's toll on me.
The other thing is, I don't know how your prof grades, but he might give partial credit for being mostly right. If you do well enough at doing the right kind of stuff but not quite being all the way there, you might get through.
Is it possible to dynamically invoke functions/methods based on an object's class name? Something like:
class LevelFactory
{
private HackerAttacks hackerAttacks;
private ShillAttacks shillAttacks;
private SoldierAttacks soldierAttacks;
public LevelFactory()
{
this.hackerAttacks = new HackerAttacks();
this.shillAttacks = new ShillAttacks();
this.soldierAttacks = new SoldierAttacks();
}
public void levelUp(out PlayerCharacter pc)
{
if((pc.Level % 3) == 0)
{
string charClass = pc.Class.GetType().ToString();
pc.Attacks.Add(this.(charClass + "Attacks").getAttack(pc.Level)); // <--- unsure if something like this is valid
//more level adjustment code
}
}
}
I mean, I could always do a quick series of conditionals to test against the character class, but I'm used to doing things like this in that fast, dirty, and loose PHP way. I really only want to save myself some typing and code bloat, so it's not a big deal if it's not possible.
Nightslyr on
PSN/XBL/Nintendo/Origin/Steam: Nightslyr 3DS: 1607-1682-2948 Switch: SW-3515-0057-3813 FF XIV: Q'vehn Tia
Is there any kind of command to clear out the command line? I've got a bunch of text that I just want to... disappear, without having to just print a bunch of empty lines.
EliteBattleman on
This is my sig.
There are many others like it, but this one is mine.
Someone's using LC-3 here, eh? One of my freshman engineering classes was all about learning the basics with LC-3.
The setup was nice at the school (NCSU) because later in one of my senior classes, we had to actually design and build LC-3 with verilog (hardware modeling language, basically write code that defines each gate/wire and how to hook them up into fun components). It was super neat.
Is there any kind of command to clear out the command line? I've got a bunch of text that I just want to... disappear, without having to just print a bunch of empty lines.
I don't know if there's a dedicated command to it but system("CLS") will accomplish that.
Woot427 on
0
jackalFuck Yes. That is an orderly anal warehouse.Registered Userregular
Is there any kind of command to clear out the command line? I've got a bunch of text that I just want to... disappear, without having to just print a bunch of empty lines.
Console.Clear() will do this. I think it was added in .Net 2.0, so if you are using 1.1 you are out of luck.
Is it possible to dynamically invoke functions/methods based on an object's class name? Something like:
class LevelFactory
{
private HackerAttacks hackerAttacks;
private ShillAttacks shillAttacks;
private SoldierAttacks soldierAttacks;
public LevelFactory()
{
this.hackerAttacks = new HackerAttacks();
this.shillAttacks = new ShillAttacks();
this.soldierAttacks = new SoldierAttacks();
}
public void levelUp(out PlayerCharacter pc)
{
if((pc.Level % 3) == 0)
{
string charClass = pc.Class.GetType().ToString();
pc.Attacks.Add(this.(charClass + "Attacks").getAttack(pc.Level)); // <--- unsure if something like this is valid
//more level adjustment code
}
}
}
I mean, I could always do a quick series of conditionals to test against the character class, but I'm used to doing things like this in that fast, dirty, and loose PHP way. I really only want to save myself some typing and code bloat, so it's not a big deal if it's not possible.
Oh, good lord, no. Technically you can use reflection, but you shouldn't be trying to write the idioms on one language into another, especially when they are so dissimilar.
Is there any kind of command to clear out the command line? I've got a bunch of text that I just want to... disappear, without having to just print a bunch of empty lines.
Console.Clear() will do this. I think it was added in .Net 2.0, so if you are using 1.1 you are out of luck.
This is exactly what I was looking for -- thanks.
EliteBattleman on
This is my sig.
There are many others like it, but this one is mine.
I might regale you with many a tale about how an attempt to save typing wound up costing everyone else maintaining that code much more time later. I won't, but let me just say :x
Also, jackal is right in that you should try to express yourself in a language's native idioms. In many cases, they serve your purposes better and/or are more secure or reliable. In large teams, it's much easier to read code that is written "natively" than to have to explain what you're doing.
Is it possible to dynamically invoke functions/methods based on an object's class name? Something like:
class LevelFactory
{
private HackerAttacks hackerAttacks;
private ShillAttacks shillAttacks;
private SoldierAttacks soldierAttacks;
public LevelFactory()
{
this.hackerAttacks = new HackerAttacks();
this.shillAttacks = new ShillAttacks();
this.soldierAttacks = new SoldierAttacks();
}
public void levelUp(out PlayerCharacter pc)
{
if((pc.Level % 3) == 0)
{
string charClass = pc.Class.GetType().ToString();
pc.Attacks.Add(this.(charClass + "Attacks").getAttack(pc.Level)); // <--- unsure if something like this is valid
//more level adjustment code
}
}
}
I mean, I could always do a quick series of conditionals to test against the character class, but I'm used to doing things like this in that fast, dirty, and loose PHP way. I really only want to save myself some typing and code bloat, so it's not a big deal if it's not possible.
The easiest way to do it would be to move the logic into the class object so it applies all the appropriate actions upon level up:
pc.Class.levelUp(pc, pc.Level);
Then have the character class add the attacks back to the character. That way you can add, remove, and change classes independently of this code. uses actual inheritance for it's purpose-- to erase the type of the individual objects.
The double dispatch-ish way would be to add a map from charClass to the object, then do a lookup and add it to the character. It's equivalent in function, but slower and less obvious, but might be useful under some scenarios.
This looks like the best place to put this without creating a whole new thread for it.
So I'm back on the track of learning C++. Every so often I get determined to do so - I'm on about my tenth run now. Up until last run I'd read a lot of books and took a lot of classes...so I know the basics pretty well up until the OOP stuff kicks in. Last run was VERY nice, I found an amazing set of Youtube videos that showed me a lot, and I actually comprehended them, unlike the book lessons. I know a lot of you have disdain for the videos, preferring the books, but I genuinely think they helped me through a rough spot in the lessons.
Anyway, what always happens in these runs is I learn a bunch of crap, then decide I should do some sample programming to get some experience. It doesn't mean anything without experience. I start and finish a few Tic Tac Toe programs, I screw around with a library, I try and use the concepts I've learned but ultimately I get distracted, pulling off into OGRE or something way over my head. I get confused and frustrated and fail at gaining any experience. I give up for a while, forget everything, and the process starts anew.
I need experience. I know a lot of concepts but they aren't helping me because I haven't used them much in a familiar manner - in my own code. If I come up with sample programs to make, I cheat in my head and pick things that I know how to do. I need some example program challenges or something. The problem with those is that usually you start from scratch every new challenge. This means you do the easy stuff over and over, and rarely ever get to the latest subject matter - the point of that challenge. So you know the easy stuff really well, but the new stuff never sinks in.
So I was wondering if any of you knew a series of challenges like this that ramped up in concepts, but as it ramped up the mundane crap was done so I could just focus on the new concept. Like a program that was mainly complete but was missing a class, and I was told how the class was supposed to behave. Do these exist on the internet anywhere en masse? If not - can any of you offer any suggestions? This is really frustrating - I've kind of been a programmer since early middle school, and I graduated four years ago. The skill is here - I modify stuff all the time, but I just can't make it from scratch. I just can't nail this barrier, and I need experience for that. Nothing too easy or hard, and nothing I can cheat myself on - I need objective experience. I've looked and come up empty handed, so I thought maybe some of you knew of some, or of an alternative way to gain some experience with the language.
JAVA Question I amt trying to compare a number the user imputs from a JTextfield with a number generated earlier. The problem is that the numbers never compare as equal. I have tried a few ways to figure this out and can't seem to get this to work. Here is an example of what I'm working with.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GamePanel extends JPanel {
private JTextField input;
private int target, guess;
public GamePanel(){
int target = 10;
System.out.println(target);
input = new JTextField (4);
input.addActionListener(new GuessNumber());
add(input);
setPreferredSize(new Dimension(033,75));
setBackground(Color.white);
}
private class GuessNumber implements ActionListener
{
public void actionPerformed (ActionEvent event){
String text= input.getText();
System.out.println(guess==target);
}
}
}
In the above example even if you enter 10 into the text field the comparison will not return true. Can someone point me in the right direction? Thanks.
Ruis on
0
jackalFuck Yes. That is an orderly anal warehouse.Registered Userregular
JAVA Question I amt trying to compare a number the user imputs from a JTextfield with a number generated earlier. The problem is that the numbers never compare as equal. I have tried a few ways to figure this out and can't seem to get this to work. Here is an example of what I'm working with.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GamePanel extends JPanel {
private JTextField input;
private int target, guess;
public GamePanel(){
int target = 10;
System.out.println(target);
input = new JTextField (4);
input.addActionListener(new GuessNumber());
add(input);
setPreferredSize(new Dimension(033,75));
setBackground(Color.white);
}
private class GuessNumber implements ActionListener
{
public void actionPerformed (ActionEvent event){
String text= input.getText();
System.out.println(guess==target);
}
}
}
In the above example even if you enter 10 into the text field the comparison will not return true. Can someone point me in the right direction? Thanks.
guess is never set to a value, so it will always be 0, and it will always print false. You probably want something like:
String text= input.getText();
guess = int.parseInt(text) //I haven't used Java in years, so this is probably wrong, but you get the idea.
System.out.println(guess==target);
What I like to do when learning a new programming language, is whenever a new concept is introduced, I'll write a program to play around with that concept.
For example, if I just learned about private and public object inheritance in C++, I would write a program that uses both those concepts correctly, and then I'd mess around doing things that you aren't supposed to be able to do and see what happens (what errors get thrown, if any, etc.). It wouldn't have to be a useful program, but just something to test the concepts with.
I find that just *using* everything, even in a simple context, really helps to solidify it.
Of course, once you know a fair bit, you'll want to write a larger program that takes advantage of everything you've learned. That's a bit of a harder problem to solve, trying to find an appropriate project. One thing that I've done in the past is googled for courses at various universities that use the language I'm trying to learn, and then try to do some of their assignments.
guess is never set to a value, so it will always be 0, and it will always print false. You probably want something like:
String text= input.getText();
guess = int.parseInt(text) //I haven't used Java in years, so this is probably wrong, but you get the idea.
System.out.println(guess==target);
I actually had that before must of deleted the line at some point during my edits. Still doesn't work. I've updated the code though if anyone else has any idea why this doesn't work.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GamePanel extends JPanel {
private JTextField input;
private int target, guess;
public GamePanel(){
int target = 10;
System.out.println(target);
input = new JTextField (4);
input.addActionListener(new GuessNumber());
add(input);
setPreferredSize(new Dimension(033,75));
setBackground(Color.white);
}
private class GuessNumber implements ActionListener
{
public void actionPerformed (ActionEvent event){
String text= input.getText();
guess = Integer.parseInt(text);
System.out.println(guess==target);
}
}
}
Ruis on
0
jackalFuck Yes. That is an orderly anal warehouse.Registered Userregular
edited October 2008
Oh, I see. You redeclared target insides GamePanel's constructor. So you have a class level target, which is always 0, because you are assigning 10 to a local variable inside the constructor. Replace "int target = 10;" with "target = 10" and it should probably work.
Does anyone have any ideas on how I can take a user's input (in LC-3 code) and output the memory location of whatever the enter, but in Hex.
Hmm. It's easier to provide an example:
Basically, this is what the program will do:
Enter starting memory location: x3001
Enter ending memory location: x3003
Memory contents x3001 to x3003:
x3001 xF030
x3002 xF025
x3003 xF001
I hope that makes more sense.
The problem I'm running into is that I don't know if there's an easy way to convert binary to hex. See in x3001 it will have something like: "1111 0000 0011 0000" and I need to find a way to output that as xF030. Any ideas? Even if it's just brainstorming, it will help out a lot.
Posts
(I don't know much about assembly, so I apologize is this is obviously incorrect)
For reference:
The furthest bit to the right is bit 0, right? If so, that means that has to be read first. Damn this will be harder than I thought. I may just turn in my single program without this one... but 10 points out of 50 (-5 already for a day late) is a lot of points.
If you think about this, how bad can this be in the worst case? I'll ignore the fact that only 20% of the squares are filled for now.
There is a possibility that every point on the grid is SET, in which case you need to check every square. Because of the nature of your algorithm, there are at most 4 squares from which you can access any given square, meaning that groupSize is called at most 5 times (if you start with the square you might hit it 5 times) for each square in the grid.
This means that the total running time is proportional (some number of comparisons and additions and things are used) to 5(size of grid) = 5n^2 if n is the side-length.
Which means it's O(n^2), or quadratic.
You can see that only having 20% or fewer of the squares filled would only make a constant difference to this running time.
The best case is obviously constant (if a SET point is surrounded by all EMPTYs).
A more interesting question is the average case. That would require some more thought though and it's late.
That is a gnarly recursion to solve, so I think this approach is probably much easier.
Approach this one in the same way as the above one. Think about, on a per-square bases, how many times groupSize can be called in the worst case. As in the above example, it is 5. Once it and all its surrounding squares are set to VISITED or EMPTY, it will never be visited again. Thus we see that despite calling a quadratic time function a few times, this function is still just quadratic in the worst case!
Heh, just looked at the LC-3 instruction set. Very minimalistic, I have to say. Yeah, you do appear to be lacking a bit shift instruction... but you do have an alternative.
Do you remember in higher level programming languages that you might do multiplications by bit shifting?
e.g. nNewValue = nOldValue << 1; // Multiply nOldValue by 2
?
You can also go the other way - do bit shifts by multiplying. Now, you don't have an multiply instruction... but you do have an addition instruction...
Logical or.
You've got logical "and" and logical "not", and every single logic instruction can be made up out of "nand"s.
Edit: Ah, just remembered - De Morgan's Law.
Just realised. Totally overthinking this.
Just add the two together.
What do you get when you add:
0xAB00 + 0x00CD?
You get 0xABCD!
this is part of the code in the test class our teacher wrote for us:
// Start timing the groupsSize method startGroupSizeTime = System.currentTimeMillis(); int size = g.groupSize(c.getRow(), c.getCol()); stopGroupSizeTime = System.currentTimeMillis(); totalGroupSizeTime += (stopGroupSizeTime - startGroupSizeTime); // Reset the matrix to be used again g.resetMatrix(c.getRow(), c.getCol()); // Start timing the numberOfGroups method startNumberOfGroupsTime = System.currentTimeMillis(); int numberOfGroups = g.numberOfGroups(); stopNumberOfGroupsTime = System.currentTimeMillis(); totalNumberOfGroupsTime += (stopNumberOfGroupsTime - startNumberOfGroupsTime); // Reset the matrix to be used again g.resetMatrix(); // Start timing the listGroups method startListGroupTime = System.currentTimeMillis(); g.listGroups(false); stopListGroupTime = System.currentTimeMillis(); startListGroupTime += (stopListGroupTime - startListGroupTime); // Reset the matrix to be used again g.resetMatrix(); System.out.println("Grid size is " + theSize + " x " + theSize); System.out.println(pctSet + "% elements set in grid"); System.out.println("groupSize time = " + totalGroupSizeTime); System.out.println("numberOfGroups time = " + totalNumberOfGroupsTime); System.out.println("listOfGroups time = " + totalListGroupTime); System.out.println();It's in the middle of a couple for loops that change the size of the matrix and the percent of the matrix that is filled; it calls a few of the methods I wrote and times them, then prints out the times. The problem is that no matter what the size of the matrix, and no matter what the percent filled (up to 87%, the highest I can go without getting a stack overflow), it always returns 0.0 for groupSize time (this is in milliseconds). Is there a flaw in my teacher's code that isn't obvious to me, or is my groupSize method really that fast?
Also, it might just be that fast. Try setting up a loop that just runs that operation 100000 times (or whatever it takes to make it a little bit slow), then make some changes to GroupSize to make it slower and see if it affects it. If it does, then you're probably doing the right thing.
The thing is, if the thing is only 2000 by 2000, then we're talking on the order of 4000000 operations (probably a lot less), which isn't actually all that many unless you run it a bunch of times (like tens or hundreds of times).
I'm nervous for most tests, but for this I'm really panicked. In the past assignments I've been close, very close to getting most exercises right but I always need some help to get me to the final destination. In reality, this shit is not that hard. The answers are always right in front of me but I can never seem to actually grasp them.
Regardless, I'm worried. I want to be in computer development in future technologies after college, but I feel like I might be incompetent. I don't know what I'm asking for really, but I just want some reassurance - am I fucked for the majority of my future education and career?
CS 145 at University of Waterloo by any chance? :P
(I took 135 back before 145 existed and thought it was really neat.)
No, you are not even remotely fucked. Scheme can be a difficult thing to grasp, and doesn't necessarily reflect on your programming ability. Is this the first programming you've ever done? It might just take a term or two to get used to it.
As long as you enjoy it, stick with it.
I do enjoy it, I like being able to operate a machine that we rely upon for day-to-day use, and I know that there's no stopping the development of it to be even more incorporated in the future. What I'm struggling with is grasping the thought process our teacher invokes on us, and sometimes it just takes it's toll on me.
It's hard to give you any advice, since we don't know what your tests will look like, but thinking like a computer scientist takes time. I remember when I started with C in high school, our coding was shit-tacular. Even the teacher was pretty bad at it.
It's not in scheme, but I enjoyed Thinking in C++. Considering Scheme is a Lisp dialect and functional in nature, this book might not be that helpful. The upshot, though, is that you need to figure out the "native idioms" of a given language. How does Scheme "want" to be written?
If nothing else, go to your teacher and present your concerns. Ask what is likely to be on the test, and what you should study to prepare for it. Teachers are usually very eager to help students that ask for it.
url = 'http://' + document.domain + '/searchplayer.php?'; dataString = 'searching=Yep.&searchstring='+playerName; GM_xmlhttpRequest({ method:'POST', url:url, headers: {'Content-type': 'application/x-www-form-urlencoded'}, data:encodeURI(dataString), onload:addClass });That's a snippet from a Greasemonkey script I wrote earlier this year. The actual key information:1) That content type header is required when you're submitting a POST request
2) The data you're posting must be in the format <name>=<value>, with ampersands separating the bits.
3) The data you send must be URI encoded
As for ignoring the response, you can do that if you want, but I would suggest at least checking the HTTP status code to make sure the server received your data and didn't error out on it for some reason. If you really don't care, just omit the onload entirely.
The other thing is, I don't know how your prof grades, but he might give partial credit for being mostly right. If you do well enough at doing the right kind of stuff but not quite being all the way there, you might get through.
Will you get an error if you try to recur a procedure that usually takes one argument, but instead you pass it two (although one's value is #void)?
You should do the operation that produces #void first, before doing the recursive call.
And procedure1 takes only 1 argument, but procedure2 has no value and is #void.
Is it possible to dynamically invoke functions/methods based on an object's class name? Something like:
class LevelFactory { private HackerAttacks hackerAttacks; private ShillAttacks shillAttacks; private SoldierAttacks soldierAttacks; public LevelFactory() { this.hackerAttacks = new HackerAttacks(); this.shillAttacks = new ShillAttacks(); this.soldierAttacks = new SoldierAttacks(); } public void levelUp(out PlayerCharacter pc) { if((pc.Level % 3) == 0) { string charClass = pc.Class.GetType().ToString(); pc.Attacks.Add(this.(charClass + "Attacks").getAttack(pc.Level)); // <--- unsure if something like this is valid //more level adjustment code } } }I mean, I could always do a quick series of conditionals to test against the character class, but I'm used to doing things like this in that fast, dirty, and loose PHP way. I really only want to save myself some typing and code bloat, so it's not a big deal if it's not possible.
Switch: SW-3515-0057-3813 FF XIV: Q'vehn Tia
Is there any kind of command to clear out the command line? I've got a bunch of text that I just want to... disappear, without having to just print a bunch of empty lines.
There are many others like it, but this one is mine.
The setup was nice at the school (NCSU) because later in one of my senior classes, we had to actually design and build LC-3 with verilog (hardware modeling language, basically write code that defines each gate/wire and how to hook them up into fun components). It was super neat.
I don't know if there's a dedicated command to it but system("CLS") will accomplish that.
Console.Clear() will do this. I think it was added in .Net 2.0, so if you are using 1.1 you are out of luck.
Oh, good lord, no. Technically you can use reflection, but you shouldn't be trying to write the idioms on one language into another, especially when they are so dissimilar.
This is exactly what I was looking for -- thanks.
There are many others like it, but this one is mine.
I might regale you with many a tale about how an attempt to save typing wound up costing everyone else maintaining that code much more time later. I won't, but let me just say :x
Also, jackal is right in that you should try to express yourself in a language's native idioms. In many cases, they serve your purposes better and/or are more secure or reliable. In large teams, it's much easier to read code that is written "natively" than to have to explain what you're doing.
The easiest way to do it would be to move the logic into the class object so it applies all the appropriate actions upon level up:
Then have the character class add the attacks back to the character. That way you can add, remove, and change classes independently of this code. uses actual inheritance for it's purpose-- to erase the type of the individual objects.
The double dispatch-ish way would be to add a map from charClass to the object, then do a lookup and add it to the character. It's equivalent in function, but slower and less obvious, but might be useful under some scenarios.
So I'm back on the track of learning C++. Every so often I get determined to do so - I'm on about my tenth run now. Up until last run I'd read a lot of books and took a lot of classes...so I know the basics pretty well up until the OOP stuff kicks in. Last run was VERY nice, I found an amazing set of Youtube videos that showed me a lot, and I actually comprehended them, unlike the book lessons. I know a lot of you have disdain for the videos, preferring the books, but I genuinely think they helped me through a rough spot in the lessons.
Anyway, what always happens in these runs is I learn a bunch of crap, then decide I should do some sample programming to get some experience. It doesn't mean anything without experience. I start and finish a few Tic Tac Toe programs, I screw around with a library, I try and use the concepts I've learned but ultimately I get distracted, pulling off into OGRE or something way over my head. I get confused and frustrated and fail at gaining any experience. I give up for a while, forget everything, and the process starts anew.
I need experience. I know a lot of concepts but they aren't helping me because I haven't used them much in a familiar manner - in my own code. If I come up with sample programs to make, I cheat in my head and pick things that I know how to do. I need some example program challenges or something. The problem with those is that usually you start from scratch every new challenge. This means you do the easy stuff over and over, and rarely ever get to the latest subject matter - the point of that challenge. So you know the easy stuff really well, but the new stuff never sinks in.
So I was wondering if any of you knew a series of challenges like this that ramped up in concepts, but as it ramped up the mundane crap was done so I could just focus on the new concept. Like a program that was mainly complete but was missing a class, and I was told how the class was supposed to behave. Do these exist on the internet anywhere en masse? If not - can any of you offer any suggestions? This is really frustrating - I've kind of been a programmer since early middle school, and I graduated four years ago. The skill is here - I modify stuff all the time, but I just can't make it from scratch. I just can't nail this barrier, and I need experience for that. Nothing too easy or hard, and nothing I can cheat myself on - I need objective experience. I've looked and come up empty handed, so I thought maybe some of you knew of some, or of an alternative way to gain some experience with the language.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GamePanel extends JPanel { private JTextField input; private int target, guess; public GamePanel(){ int target = 10; System.out.println(target); input = new JTextField (4); input.addActionListener(new GuessNumber()); add(input); setPreferredSize(new Dimension(033,75)); setBackground(Color.white); } private class GuessNumber implements ActionListener { public void actionPerformed (ActionEvent event){ String text= input.getText(); System.out.println(guess==target); } } }In the above example even if you enter 10 into the text field the comparison will not return true. Can someone point me in the right direction? Thanks.
guess is never set to a value, so it will always be 0, and it will always print false. You probably want something like:
What I like to do when learning a new programming language, is whenever a new concept is introduced, I'll write a program to play around with that concept.
For example, if I just learned about private and public object inheritance in C++, I would write a program that uses both those concepts correctly, and then I'd mess around doing things that you aren't supposed to be able to do and see what happens (what errors get thrown, if any, etc.). It wouldn't have to be a useful program, but just something to test the concepts with.
I find that just *using* everything, even in a simple context, really helps to solidify it.
Of course, once you know a fair bit, you'll want to write a larger program that takes advantage of everything you've learned. That's a bit of a harder problem to solve, trying to find an appropriate project. One thing that I've done in the past is googled for courses at various universities that use the language I'm trying to learn, and then try to do some of their assignments.
I actually had that before must of deleted the line at some point during my edits. Still doesn't work. I've updated the code though if anyone else has any idea why this doesn't work.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GamePanel extends JPanel { private JTextField input; private int target, guess; public GamePanel(){ int target = 10; System.out.println(target); input = new JTextField (4); input.addActionListener(new GuessNumber()); add(input); setPreferredSize(new Dimension(033,75)); setBackground(Color.white); } private class GuessNumber implements ActionListener { public void actionPerformed (ActionEvent event){ String text= input.getText(); guess = Integer.parseInt(text); System.out.println(guess==target); } } }Hmm. It's easier to provide an example:
Basically, this is what the program will do:
I hope that makes more sense.
The problem I'm running into is that I don't know if there's an easy way to convert binary to hex. See in x3001 it will have something like: "1111 0000 0011 0000" and I need to find a way to output that as xF030. Any ideas? Even if it's just brainstorming, it will help out a lot.