• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Perhaps GAF can help me with this:

I'm writing a Bit Utility class for Java that basically serves a a shorthand for implementing things like encryption algorithms.

I'm trying to write a method that essentially acts like Integer#rotateLeft(int,int) (and also rotateRight), As an example, here's part of my unit test:

Code:
    int[] testInInt  = { 0x1FFF0000, 0x2FFF0000 }
        , testOutInt = { 0x00002FFF, 0x00001FFF };
    assertArrayEquals(testOutInt, BitUtils.cycleLeft(testInInt, 0x10));

Here's what I have so far, my test cases have been failing and I've been staring at this code for too long...:

Code:
    public static int[] cycleLeft(int[] bits, int shift) 
    { 
        final int INT_BITS = 0x20;
        final int INT_MAX = 0xFFFFFFFF;
        final int ARRAY_SIZE = bits.length;

        int result[] = new int[ARRAY_SIZE];
        int mask[] = new int[ARRAY_SIZE];
        int i, mod, div;

        shift %= numberOfBits(bits); // (in this case will be %= 32 * ARRAY_SIZE)

        mod = shift % INT_BITS;
        div = shift / INT_BITS;

        for (i = 0; i < ARRAY_SIZE; i++) {
            mask[i] = bits[i] & (INT_MAX << (INT_BITS * i + shift));
            result[(i - div) % ARRAY_SIZE] = bits[i] << mod;
            result[(i + div + ARRAY_SIZE - 1) % ARRAY_SIZE] |= mask[i] >> (INT_BITS - mod);
        }
        return result;
    }

It's a mess to debug. I think I might try and nut it out on my whiteboard.

EDIT: Found the problem. Never mind. Fix for reference:
Code:
        for (i = 0; i < ARRAY_SIZE; i++) {
            mask[i] = bits[i] & (INT_MAX << (INT_BITS * i + shift));
            result[(i - div) % ARRAY_SIZE] = bits[i] << mod;
        }
        for (i = 0; i < ARRAY_SIZE; i++) {
            result[(i + div + ARRAY_SIZE - 1) % ARRAY_SIZE] |= mask[i] >> (INT_BITS - mod);
        }

Typical, soon as I ask for help I solve my own problem.
 
Okay, feeling stupid in this one problem. From codingbat, you're supposed to remove all the x's except for the beginning and end. So:
stringX("xxHxix") &#8594; "xHix"
After almost an hour, I gave up even though I was sorta close, just had trouble getting that last x in the string.
Their solution:
public String stringX(String str) {
String result = "";
for (int i=0; i<str.length(); i++) {
// Only append the char if it is not the "x" case
if (!(i > 0 && i < (str.length()-1) && str.substring(i, i+1).equals("x"))) {
result = result + str.substring(i, i+1); // Could use str.charAt(i) here
}
}
return result;
}
The part I'm having trouble understanding is how they kept all the non-x characters and the x in the beginning and end. It seems like this is the trick:
!(i > 0 && i < (str.length()-1) && str.substring(i, i+1).equals("x")))
I'm not really getting this and making myself confuse. Can someone clear this up? I tried writing down the logic that I think is happening but just confusing myself in the process...
 

CrankyJay

Banned
Okay, feeling stupid in this one problem. From codingbat, you're supposed to remove all the x's except for the beginning and end. So:
stringX("xxHxix") &#8594; "xHix"
After almost an hour, I gave up even though I was sorta close, just had trouble getting that last x in the string.
Their solution:

The part I'm having trouble understanding is how they kept all the non-x characters and the x in the beginning and end. It seems like this is the trick:

I'm not really getting this and making myself confuse. Can someone clear this up? I tried writing down the logic that I think is happening but just confusing myself in the process...


!(i > 0 && i < (str.length()-1) && str.substring(i, i+1).equals("x")))

think of it like this with real numbers:

i = 0

is 0 > 0? no, so everything in that first set of parenthesis is false...so !(false) = true

i = 1

is 1 > 0? yes... is 1 < str.length-1 (the string is 6 characters long)? yes...then everything in this set of parenthesis is true... so !(true) = false

This part of the condition is basically making sure that i (the current index), is greater than 0 and less than the length of the string (minus 1)...strings are zero based, so the FIRST character in a string is at the zeroith index.....and the LAST character in the string is at the length of the string MINUS 1...

string = xxHxix

string[0] = x
string[1] = x
string[2] = H
string[3] = x
string[4] = i
string[5] = x
 
!(i > 0 && i < (str.length()-1) && str.substring(i, i+1).equals("x")))

think of it like this with real numbers:

i = 0

is 0 > 0? no, so everything in that first set of parenthesis is false...so !(false) = true

i = 1

is 1 > 0? yes... is 1 < str.length-1 (the string is 6 characters long)? yes...then everything in this set of parenthesis is true... so !(true) = false

This part of the condition is basically making sure that i (the current index), is greater than 0 and less than the length of the string (minus 1)...strings are zero based, so the FIRST character in a string is at the zeroith index.....and the LAST character in the string is at the length of the string MINUS 1...

string = xxHxix

string[0] = x
string[1] = x
string[2] = H
string[3] = x
string[4] = i
string[5] = x
Wait, now I get it. The ! is affecting the whole statement, right? So if the char is not x and x is not in the beginning of the string or end, it stores the char at the instance variable! Wow, I'm so stupid.

Thanks for the explanation.
 

CrankyJay

Banned
Wait, now I get it. The ! is affecting the whole statement, right? So if the char is not x and x is not in the beginning of the string or end, it stores the char at the instance variable! Wow, I'm so stupid.

Thanks for the explanation.

The ! is affecting the statements inside of the ( ), it's all order of operations stuff
 

jokkir

Member
Can someone help me understand this workshop I have?

The operator << accepts a C-style string on the right side
and stores the contents of the string in the Stream object (if possible)
making sure *NOT* to exceed the Stream's available capacity.

The show(Stream) function accepts a copy of a stream object and
uses cout to display all of the Stream's contents on the screen
followed by a newline. You will need to decide if you wish
to make this a global or friend function!

So I have to code a operator<< that overloads the << operator? This is in the main we're supposed to use:

Code:
x1 << data;

So it just concatenates it to the Stream object?

Also, for the show... it doesn't seem to be finding it in the .cpp file.

Code:
void Stream::show(Stream){
	cout << "hello" << endl;
}

And the header file:
Code:
void show(Stream);

Is this the wrong syntax? >__> Sorry for the n00b questions
 
Does anyone know some resources for teaching programming to Middle Schoolers. Just got brought on the payroll at an internship and I was put in charge of planning and teaching the whole 16 day program.

We have done 3 days so far. Mostly MS Word stuff that the school wanted me to teach.

Also I have no teaching experience and don't know what I am doing here
 
Does anyone know some resources for teaching programming to Middle Schoolers. Just got brought on the payroll at an internship and I was put in charge of planning and teaching the whole 16 day program.

We have done 3 days so far. Mostly MS Word stuff that the school wanted me to teach.

Also I have no teaching experience and don't know what I am doing here

codingbat.com has some fairly simple Java and Python exercises. You can also create an account and track what exercises you've done.
 
codingbat.com has some fairly simple Java and Python exercises. You can also create an account and track what exercises you've done.

Good start. I know there is codeacademy and Scratch as well. I might just stick to Lego Mindstorm "coding" an do the rest of the required materials not related to programming.
 

tokkun

Member
Can someone help me understand this workshop I have?



So I have to code a operator<< that overloads the << operator? This is in the main we're supposed to use:

Code:
x1 << data;

So it just concatenates it to the Stream object?

Also, for the show... it doesn't seem to be finding it in the .cpp file.

Code:
void Stream::show(Stream){
	cout << "hello" << endl;
}

And the header file:
Code:
void show(Stream);

Is this the wrong syntax? >__> Sorry for the n00b questions

Your function definition for show says its a member of Stream. Is the function declaration inside the Stream class?

Perhaps this also provokes the question of whether show ought to be a member function. If you want to keep it as a member function, you should probably make it static.
 

Lonely1

Unconfirmed Member
Anyone knows of an algorithm for generating the binary numbers distributed within a range with a set number of 1's?
 

usea

Member
Anyone knows of an algorithm for generating the binary numbers distributed within a range with a set number of 1's?
The naive solution would be to just enumerate all the numbers in the range, and only accept ones with a set number of 1s.

solution in a made up language:
Code:
list<int> generate_n_set(int n, int range)
{
  list<int> result;
  for(int i = 0; i < range; i++)
  {
    if(count_set_bits(i) == n)
    {
      result.add(i);
    }
  }
  return result;
}

int count_set_bits(int value)
{
  int c = 0;
  while(v != 0)
  {
    c += v & 1;
    v = v >> 1;
  }
  return c;
}

A better solution would be finding the minimum number of bits used for the max range. Say your range is 10,000 and you want to find all numbers between 0 and 10,000 that have 5 bits set. 10,000 needs 14 bits to represent it. Generate all permutations of nine 0s and five 1s. Then remove any that are above the max range (should be very few of them, if any).

edit: there are much more efficient ways of counting set bits than my example above. See this page

edit2: there are several ways to find the minimum number of bits needed to represent a number. One way is to enumerate powers of 2 until you find the first one larger than your number.
 

Lonely1

Unconfirmed Member
The naive solution would be to just enumerate all the numbers in the range, and only accept ones with a set number of 1s.

solution in a made up language:
Code:
list<int> generate_n_set(int n, int range)
{
  list<int> result;
  for(int i = 0; i < range; i++)
  {
    if(count_set_bits(i) == n)
    {
      result.add(i);
    }
  }
  return result;
}

int count_set_bits(int value)
{
  int c = 0;
  while(v != 0)
  {
    c += v & 1;
    v = v >> 1;
  }
  return c;
}

A better solution would be finding the minimum number of bits used for the max range. Say your range is 10,000 and you want to find all numbers between 0 and 10,000 that have 5 bits set. 10,000 needs 14 bits to represent it. Generate all permutations of nine 0s and five 1s. Then remove any that are above the max range (should be very few of them, if any).

edit: there are much more efficient ways of counting set bits than my example above. See this page

edit2: there are several ways to find the minimum number of bits needed to represent a number. One way is to enumerate powers of 2 until you find the first one larger than your number.
Thanks for the reply. But it isn't what I'm looking for, my words are of hundred of thousands of bits in size, but I only need to read them up to the most significant bit, so getting all the permutations would be very inefficient. My fault for no specifying the problem, though. I found a solution that I believe is better suited for my program (by changing the numbers of 0's between the 1's).
 

usea

Member
Thanks for the reply. But it isn't what I'm looking for, my words are of hundred of thousands of bits in size, but I only need to read them up to the most significant bit, so getting all the permutations would be very inefficient. My fault for no specifying the problem, though. I found a solution that I believe is better suited for my program (by changing the numbers of 0's between the 1's).
If I misunderstood your problem, then I still do. Not sure what you're talking about if you're not asking for all the numbers with N bits set between two other numbers.
 

Lonely1

Unconfirmed Member
If I misunderstood your problem, then I still do. Not sure what you're talking about if you're not asking for all the numbers with N bits set between two other numbers.

Oh, it is what I'm looking for, but just that for my program needs a different way to do it, as calculating the permutations of a list of 200k elements is not desirable (i have to do it millions of times).
 

usea

Member
Oh, it is what I'm looking for, but just that for my program needs a different way to do it, as calculating the permutations of a list of 200k elements is not desirable (i have to do it millions of times).
If your range is 0 to 2^200k then that's probably the best you're going to do. If your max is a lot smaller than 2^200k then maybe you misunderstood what I said. I said to only use up to the bits required by your max range. Word size is irrelevant.
 

Lonely1

Unconfirmed Member
If your range is 0 to 2^200k then that's probably the best you're going to do. If your max is a lot smaller than 2^200k then maybe you misunderstood what I said. I said to only use up to the bits required by your max range. Word size is irrelevant.

Yeah, my max is of ~2^200k, but I only need to store up to the most significant 1. So, if I need all words with 3 bits, 111 and 1110 are the first two values i will store on my list. I don't need 000000....0000111 and 0000....0001101. If I use the permutations, I will operate over a bunch of zeroes that I don't need.
 

usea

Member
Yeah, my max is of 2^200k, but I only need to store up to the most significant 1. So, if I need all words with 3 bits, 111 and 1110 are the first two values i will store on my list. I don't need 000000....0000111 and 0000....0001101. If I use the permutations, I will operate over a bunch of zeroes that I don't need.
Ah, I see what you're saying.
 
Does anyone know some resources for teaching programming to Middle Schoolers. Just got brought on the payroll at an internship and I was put in charge of planning and teaching the whole 16 day program.

We have done 3 days so far. Mostly MS Word stuff that the school wanted me to teach.

Also I have no teaching experience and don't know what I am doing here
Scratch (free visual programming tool) is a great place to start. Easy introduction to basic CS concepts, tons of curriculum materials out there to use for your situation, and you can easily incorporate game based projects which are generally better received by that crowd.
 

nan0

Member
Does anyone know some resources for teaching programming to Middle Schoolers. Just got brought on the payroll at an internship and I was put in charge of planning and teaching the whole 16 day program.

We have done 3 days so far. Mostly MS Word stuff that the school wanted me to teach.

Also I have no teaching experience and don't know what I am doing here

If you want to use Python or Ruby, you can use repl.it as an online interpreter for basic programming tasks, I'm sure they'd find that pretty cool.

If you want to go in a different direction; I learned POV-Ray in middle school. You can supply them some example files and let them fiddle around with colors, lighting, camera and texture settings. It also supports basic programmign constructs such as loops and the rendering results can look pretty nice if todays middle schoolers can be as easily impressed as we back then.

On resources, you didn't mention what kind of internship you do, but if you're assigned to that kind of task, you should have some experience, right? Any more complicated stuff will probably bore them, so I would stick to easy stuff that produces some visible results. They won't be amazed by "Wow, this implementation of Quicksort is soo fast, it's amazing!". Though Radixsort can also be interesting, because it's principle is easy enough to understand and could be fun if you instruct them to do it on a whiteboard.
 

r1chard

Member
Does anyone know some resources for teaching programming to Middle Schoolers. Just got brought on the payroll at an internship and I was put in charge of planning and teaching the whole 16 day program.

We have done 3 days so far. Mostly MS Word stuff that the school wanted me to teach.

Also I have no teaching experience and don't know what I am doing here

Grab a copy of "Python for Kids" and work through it. It'll hopefully help you in the bits where you're not sure how to progress.

You can use http://repl.it/languages/Python if you can't install anything, otherwise IDLE is bearable once you figure out the initial UI quibbles (I just helped teach a room of 8-11 year olds on Raspberry Pis using IDLE and it worked) and IDLE is installed with the standard Python clicky-clicky installer for Windows/Mac.
 

Hellix

Member
Has anyone taken a Computer Programming Aptitude Test? About to take one for the first time as part of an interviewing process. The only information I have about the test is it contains Verbal Meaning, Reasoning, Letter Series, Number Ability, and Diagramming. Given that, I still have no idea what to expect in terms of what questions or tasks may be asked. Is there something I could to prepare for this?
 
Scratch (free visual programming tool) is a great place to start. Easy introduction to basic CS concepts, tons of curriculum materials out there to use for your situation, and you can easily incorporate game based projects which are generally better received by that crowd.

We can't download anything on the school computers. When the classes are held at our office this is what we use.

If you want to use Python or Ruby, you can use repl.it as an online interpreter for basic programming tasks, I'm sure they'd find that pretty cool.

If you want to go in a different direction; I learned POV-Ray in middle school. You can supply them some example files and let them fiddle around with colors, lighting, camera and texture settings. It also supports basic programmign constructs such as loops and the rendering results can look pretty nice if todays middle schoolers can be as easily impressed as we back then.

On resources, you didn't mention what kind of internship you do, but if you're assigned to that kind of task, you should have some experience, right? Any more complicated stuff will probably bore them, so I would stick to easy stuff that produces some visible results. They won't be amazed by "Wow, this implementation of Quicksort is soo fast, it's amazing!". Though Radixsort can also be interesting, because it's principle is easy enough to understand and could be fun if you instruct them to do it on a whiteboard.

Thanks for those suggestions!
I am currently a CompSci sophomore. No teaching experience. No idea why they put me in this position, but I believe I can do it.

Grab a copy of "Python for Kids" and work through it. It'll hopefully help you in the bits where you're not sure how to progress.

You can use http://repl.it/languages/Python if you can't install anything, otherwise IDLE is bearable once you figure out the initial UI quibbles (I just helped teach a room of 8-11 year olds on Raspberry Pis using IDLE and it worked) and IDLE is installed with the standard Python clicky-clicky installer for Windows/Mac.

Thanks. I will. Also great work with the PyCon thing. Looks like it was a lot of fun :)
Wish stuff like that was available when I was a middle schooler.
 

Certinty

Member
Major beginner here but could use some help with something very simple with Python:
menu = "What would you like to do today? \n\
1. Take a walk\n\
2. Go shopping\n\
3. Stay at home\n\
4. Go to the beach\n"
x = int(input(menu))
if x == 1:
print("You're healthy.")
if x == 2:
print("You have a lot of money to spend.")
if x == 3:
print("You're boring.")
if x == 4:
print("Lovely choice.")

menu2 = "Would you like to change your mind? \n\
1. Yes\n\
2. No\n"
y = int(input(menu2))
if y == 1:
(I want to loop back to the first menu from here)
if y == 2:
(I want the program to exit)
In the last few lines I've put what I want to do in brackets but not sure how I do, can anyone help me?
 

nan0

Member
Major beginner here but could use some help with something very simple with Python:

In the last few lines I've put what I want to do in brackets but not sure how I do, can anyone help me?

Put the logic into a while loop, if the user wants to repeat, do nothing (the loop will start again). If the user wants to quit, break out of the loop and exit.

Something like this:

Code:
boolean again = true
while again:
menu = "What would you like to do today? \n\
1. Take a walk\n\
2. Go shopping\n\
3. Stay at home\n\
4. Go to the beach\n"
x = int(input(menu))
if x == 1:
print("You're healthy.")
if x == 2:
print("You have a lot of money to spend.")
if x == 3:
print("You're boring.")
if x == 4:
print("Lovely choice.")

menu2 = "Would you like to change your mind? \n\
1. Yes\n\
2. No\n"
y = int(input(menu2))
if y == 1:
pass
if y == 2:
again=false
 

Certinty

Member
Put the logic into a while loop, if the user wants to repeat, do nothing (the loop will start again). If the user wants to quit, break out of the loop and exit.

Something like this:

Code:
boolean again = true
while again:
menu = "What would you like to do today? \n\
1. Take a walk\n\
2. Go shopping\n\
3. Stay at home\n\
4. Go to the beach\n"
x = int(input(menu))
if x == 1:
print("You're healthy.")
if x == 2:
print("You have a lot of money to spend.")
if x == 3:
print("You're boring.")
if x == 4:
print("Lovely choice.")

menu2 = "Would you like to change your mind? \n\
1. Yes\n\
2. No\n"
y = int(input(menu2))
if y == 1:
pass
if y == 2:
again=false
Thank you very much, massive help.
 
A question to long time professionals here...

I'm being offered a job at an important company consisting of developing web applications using the usual languages for that (Java, HTML, CSS, JavaScript, PHP, XML), now this would be my first job since graduating from school, so it's obviously an entry position.

The thing is, I don't really see myself as a "Web Developer" in the long run, as I always liked doing more back-end stuff back in school (Compiler design and Operating systems were some of my favorite university classes), so my question is: Does the first job really affect one's career? Would taking it make it harder to get more "back-end" stuff jobs in the future?

I'm not in the position to be picky about jobs since decent ones are scarce over here (and I need money!), so it's not like I can discard this opportunity and wait for another one to come.
 

Slavik81

Member
Just started with the Euler Project problems to test my Ruby learning...

Two done, 418 to go ;)

IIRC, #3 is the first one that requires some thought. Good luck.

A question to long time professionals here...

I'm being offered a job at an important company consisting of developing web applications using the usual languages for that (Java, HTML, CSS, JavaScript, PHP, XML), now this would be my first job since graduating from school, so it's obviously an entry position.

The thing is, I don't really see myself as a "Web Developer" in the long run, as I always liked doing more back-end stuff back in school (Compiler design and Operating systems were some of my favorite university classes), so my question is: Does the first job really affect one's career? Would taking it make it harder to get more "back-end" stuff jobs in the future?

I'm not in the position to be picky about jobs since decent ones are scarce over here (and I need money!), so it's not like I can discard this opportunity and wait for another one to come.
I would recommend going for it. Ask to work on back-end stuff in that job whenever you can. There is a steep cost to switching, but I think it's possible. Especially if you do it after only a couple years.

But take my advice with a grain of salt. I've never actually tried to switch. I found my niche (C++/Qt) and I've specialized in it.
 

moka

Member
A question to long time professionals here...

I'm being offered a job at an important company consisting of developing web applications using the usual languages for that (Java, HTML, CSS, JavaScript, PHP, XML), now this would be my first job since graduating from school, so it's obviously an entry position.

The thing is, I don't really see myself as a "Web Developer" in the long run, as I always liked doing more back-end stuff back in school (Compiler design and Operating systems were some of my favorite university classes), so my question is: Does the first job really affect one's career? Would taking it make it harder to get more "back-end" stuff jobs in the future?

I'm not in the position to be picky about jobs since decent ones are scarce over here (and I need money!), so it's not like I can discard this opportunity and wait for another one to come.

Go for it. Any experience is good experience.
 
A question to long time professionals here...

I'm being offered a job at an important company consisting of developing web applications using the usual languages for that (Java, HTML, CSS, JavaScript, PHP, XML), now this would be my first job since graduating from school, so it's obviously an entry position.

The thing is, I don't really see myself as a "Web Developer" in the long run, as I always liked doing more back-end stuff back in school (Compiler design and Operating systems were some of my favorite university classes), so my question is: Does the first job really affect one's career? Would taking it make it harder to get more "back-end" stuff jobs in the future?

I'm not in the position to be picky about jobs since decent ones are scarce over here (and I need money!), so it's not like I can discard this opportunity and wait for another one to come.

As others have said, go for it. When you have a job, finding a better job is so much easier and stress free. You'll be working for a heck of a long time (hopefully!) so no rush.

I'd just be up front about the type of work you want to do. As much as they can, they should try to help you out there. If not? Well they can't really hold a grudge about you for taking on another opportunity when it pops up.
 

CrankyJay

Banned
A question to long time professionals here...

I'm being offered a job at an important company consisting of developing web applications using the usual languages for that (Java, HTML, CSS, JavaScript, PHP, XML), now this would be my first job since graduating from school, so it's obviously an entry position.

The thing is, I don't really see myself as a "Web Developer" in the long run, as I always liked doing more back-end stuff back in school (Compiler design and Operating systems were some of my favorite university classes), so my question is: Does the first job really affect one's career? Would taking it make it harder to get more "back-end" stuff jobs in the future?

I'm not in the position to be picky about jobs since decent ones are scarce over here (and I need money!), so it's not like I can discard this opportunity and wait for another one to come.

I was in your exact position back in 2003. I ended up taking the web job and eventually moved onto a new company doing more traditional development after 2 years. It was a stepping stone job for me that probably got me the job that I really wanted.

This will be your first job in a long career of many.
 

Jokab

Member
What would you guys say is the essential game programming book? No specific subject, just in general. Preferably for someone who knows a bit of programming but is new to programming games.
 

Mew2

Neo Member
So...I have my Java final project and it's been getting really busy for me here deployed...would anyone be interested in doing my final project? Of course I will pay ;]

Pms please!
 
So...I have my Java final project and it's been getting really busy for me here deployed...would anyone be interested in doing my final project? Of course I will pay ;]

Pms please!
image.php
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
Hey, outsourcing is part of the software engineering process. His strategy is totally legit!
 
Recently bought this:

http://www.amazon.com/gp/product/098478280X/?tag=neogaf0e-20

And it's actually pretty good from what I can glean from first flip through.

Also came across this when I opened to a random page:

Find the bug in these lines of code

Code:
unsigned int i;
for (i = 100; i >= 0; --i)
    printf("%d\n", i);

and I was quite surprised at myself for how fast I was able to spot the issue. Made me feel a little more confident in what I know as I start to go into interviews for jobs and such, even if it is a small bug.
 
Top Bottom