• 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

Exuro

Member
quick question. When I pipe read and write with parent/child I context switch back and forth. My TA gave me this simple algorithm and told me it would initiate at least 2 context switches via either parent/child first. I'm not seeing how running child first would record 2 though.

parent:
start timer
write pipe1
read pipe2
end timer

child
read pip1
write pipe2

the only way I could see it working is if exiting a process initiates a context switch, but I don't know if that's true.

Tips?
 
Do "write" and "read" mean "open for write, write" and "open for read, read"?

If so, all of the calls will block (and thus require a context switch to be unblocked)

http://www.lighterra.com/papers/modernmicroprocessors/

A fantastic article about modern CPU architecture. It requires a basic understanding of CPU architecture, pipelining, caches and so on. (as you would get in an introductory computer architecture course)
My favorite beginner level breakdown of this background info is Security Now #254 - What We'll Do For Speed, FWIW.
 

Chris R

Member
I've got a mid-term exam next Monday (Intro to Programming - c#), and I'm struggling a bit with Loop and if/else structures.

Some of the problems I'm struggling with.

Post some snippits and what you are having issues with are we are more than willing to help.

Also, yay for office bureaucracy bullshit. Wish I hadn't locked into a new lease last year or I'd break it and head to Seattle for a new job ASAP :(
 

Exuro

Member
Do "write" and "read" mean "open for write, write" and "open for read, read"?

If so, all of the calls will block (and thus require a context switch to be unblocked)
.
Yes they do. Thanks for the answer. When a process is terminated that will also context switch correct?
 
I've got a mid-term exam next Monday (Intro to Programming - c#), and I'm struggling a bit with Loop and if/else structures.

(code is just pseudo code)

if/else
Code:
if (test) {
  //executes if test is true
} else {
  //executes if test is false
}

Loops are always some variation of 'while condition is true do thing'; choosing between while/do/for loops is largely a matter of taste. Learn 'while' first and then figure the rest out.
Code:
//adds the numbers 3-10
i = 3
sum = 0
limit = 10
while (i <= limit) {
  sum = sum + i
  i++
}
 

heytred

Neo Member
Question: What did you guys do at an entry level to improve your programming?

I'm in my second semester of College learning Java (I have some experience with Python as well, mainly syntax), but I don't feel as though the programming we do is very challenging or productive. Half the time we are learning 'theory' and won't do any assigned programming for 2-3 weeks at a time. To make up for it I would spend time on Codingbat or CodeAcademy, but those almost seem monotonous and uninteresting to me. I like solving problems, but can't think of a good way to get experience coding on a basic level in my free time.

suggestions?
 

heytred

Neo Member
Do sideprojects you want to do, like make small games?

Well, I'm only in my second semester so my knowledge is vast by any means. Can you recommend me a good place to start possibly? Either a solid website resource or book?

That sounds like the kind of stuff I would love to do in my free time.
 

upandaway

Member
Well, I'm only in my second semester so my knowledge is vast by any means. Can you recommend me a good place to start possibly? Either a solid website resource or book?

That sounds like the kind of stuff I would love to do in my free time.
You could take a look at LibGDX and use that to make something. I'm in a similar situation (will start university in October) and that's what I'm doing.
 
Question: What did you guys do at an entry level to improve your programming?

I'm in my second semester of College learning Java (I have some experience with Python as well, mainly syntax), but I don't feel as though the programming we do is very challenging or productive. Half the time we are learning 'theory' and won't do any assigned programming for 2-3 weeks at a time. To make up for it I would spend time on Codingbat or CodeAcademy, but those almost seem monotonous and uninteresting to me. I like solving problems, but can't think of a good way to get experience coding on a basic level in my free time.

suggestions?

If you like math-y problems (especially number theory), Project Euler might be something for you. If you like biology and find DNA in particular interesting, you can try Rosalind.info. You can also learn a new language and try solving your old problems in a new language, I've done that a lot over the last year. It's a very interesting exercise. The advantage of those two resources is that they don't force you to use any particular programming language and that each problem is small and mostly self-contained. The easier problems won't be longer than 50 lines of code and a lot of the time a lot shorter.
 

tokkun

Member
Question: What did you guys do at an entry level to improve your programming?

I'm in my second semester of College learning Java (I have some experience with Python as well, mainly syntax), but I don't feel as though the programming we do is very challenging or productive. Half the time we are learning 'theory' and won't do any assigned programming for 2-3 weeks at a time. To make up for it I would spend time on Codingbat or CodeAcademy, but those almost seem monotonous and uninteresting to me. I like solving problems, but can't think of a good way to get experience coding on a basic level in my free time.

suggestions?

Do the Division II algorithm challenges at TopCoder.

http://community.topcoder.com/tc?module=MatchList
 

r1chard

Member
Question: What did you guys do at an entry level to improve your programming?

I'm in my second semester of College learning Java (I have some experience with Python as well, mainly syntax), but I don't feel as though the programming we do is very challenging or productive. Half the time we are learning 'theory' and won't do any assigned programming for 2-3 weeks at a time. To make up for it I would spend time on Codingbat or CodeAcademy, but those almost seem monotonous and uninteresting to me. I like solving problems, but can't think of a good way to get experience coding on a basic level in my free time.

suggestions?
Write some games :)

Start here if you like: https://bitbucket.org/r1chardj0n3s/pygame-tutorial
 

heytred

Neo Member
Thanks for all the replies guys, I'm checking out all the links, I'll let you know what I think once I sink some time into them. I really appreciate it =)
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
Update:

Placement new is weird, amazing, and the use of it to re-initialize pool objects should be supported with the renew keyword in C++. Or something.




Don't listen to this idiot. Placement new is evil. :V
 

GaimeGuy

Volunteer Deputy Campaign Director, Obama for America '16
we explicitely declare all of our destructors and make them virtual.

Since they were all trivial, we believed it was safe to not call the object destructors before using placement new.

Well... problem is, they had standard library strings as data members, which are not non-trivial. So even though our objects have trivial destructors, we have to call them

The fun part is I checked for memory leaks by triggering the placement new a few thousand times in a test rim while monitoring the heap size.

Saw no problems, none of the code inspectors saw an issue either, so we thought we were good to go.

well... we have a 500 MB heap, and we're leaking only between 4 and 32 bytes of memory each time we do this, so there's no way a single tester could have caught this.

Hooray for hidden surprises
 

iapetus

Scary Euro Man
Question: What did you guys do at an entry level to improve your programming?

More programming.

Seriously, it didn't matter what. I wrote games. I wrote game construction kits. I wrote a typing tutor. I wrote utilities. I wrote music players. I learned new languages. I wrote a curses library because I wanted to port a game and couldn't get the existing one to work properly. I wrote graphical demos. I wrote a traffic simulation program because it was someone else's coursework and I liked the idea (but because mine wasn't bound by the rules of the coursework, I added animation, music and a road editor).

I wrote everything that came to mind. And I got better at programming.
 

tokkun

Member
we explicitely declare all of our destructors and make them virtual.

Since they were all trivial, we believed it was safe to not call the object destructors before using placement new.

Well... problem is, they had standard library strings as data members, which are not non-trivial. So even though our objects have trivial destructors, we have to call them

The fun part is I checked for memory leaks by triggering the placement new a few thousand times in a test rim while monitoring the heap size.

Saw no problems, none of the code inspectors saw an issue either, so we thought we were good to go.

well... we have a 500 MB heap, and we're leaking only between 4 and 32 bytes of memory each time we do this, so there's no way a single tester could have caught this.

Hooray for hidden surprises

If you're going to do any sort of freelisting, I have found that it is better practice to use an Init() method rather than placement new.

That said, placement new is still worthwhile if you want to write something like an arena-based memory allocation scheme.
 

heytred

Neo Member
More programming.

This is obviously my intention, but the pace at which our curriculum is moving (5 weeks into my seconds semester and we are just now moving into working with multiple connected classes...) isn't doing it for me. I don't have the knowledge base to just go out and start writing working code - at least not code that actually does something, so before I started doing just that I figured I'd ask for some good places to start/resources. =)

EDIT: Again, thanks for all the resources and ideas everyone.
 

iapetus

Scary Euro Man
This is obviously my intention, but the pace at which our curriculum is moving (5 weeks into my seconds semester and we are just now moving into working with multiple connected classes...) isn't doing it for me.

Then you've got to find ways to go beyond the curriculum. Burn through your textbooks to get to the more advanced topics. Look at good code. Modify that good code - there's plenty of open source software out there that you could contribute to if you feel up to it or just play with if you don't. Always be writing something that's just beyond the level of your ability.
 
Anyone here willing to Skype with me some time this weekend? I need a tutor for my mid-term exam V_V

It's for c#, the only things I have problems with are Loops and the Mod function.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Wow, ProgrammingGAF. I didn't know this existed. Subbed for sure.

I have a deep question for you guys to start with.
How important is the schooling when going for your Bachelor's?

The reason I ask is because I will be finishing my first year into my BS in CS at a state school that isn't really geared towards STEM programs, they are more into management and business, and I've been really underwhelmed with my courses. I have been thinking of transferring to a different state school that has more of a focus on STEM (would cost $700 - 1000 more per semester), but I want to know if it would be worth it.

I have heard from people that your Bachelor's doesn't really matter at all as long as you complete it, but I'm not really convinced. My thinking is that it would be better to get more of an understanding while in school then to rely on learning it in the field.

What are your thoughts?
 

Slavik81

Member
If you're dissatisfied with your university, and don't feel you're getting good schooling for your money then switch.

When people say your undergraduate university isn't important, they mean that most schools offer programs of similar quality and most employers are not going to judge you based on the school name.

They don't mean that the schooling is unimportant. A good education is definitely important, and if you are not getting it from your university, then absolutely, you should switch universities.
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
If you're dissatisfied with your university, and don't feel you're getting good schooling for your money then switch.

When people say your undergraduate university isn't important, they mean that most schools offer programs of similar quality and most employers are not going to judge you based on the school name.

They don't mean that the schooling is unimportant. A good education is definitely important, and if you are not getting it from your university, then absolutely, you should switch universities.

That is my thinking as well.
Glad to know it's not crazy.
 
Yes they do. Thanks for the answer. When a process is terminated that will also context switch correct?
Depends what you mean. Strictly speaking (from the perspective of the cpu) yes, since it's going to unload the context and do something else, but the process doesn't care anymore since it's dead jim!
 

Fantastical

Death Prophet
I'm trying to do something I've never done before with programming.

Basically I have a program that uses a webcam and does something to the image captured. While that program is running, I want another program to do something else with the images captured. The problem is that the programs can't use the webcam at the same time.

What I want to do is have an intermediary program take the images in memory and pass them off to the appropriate programs to use. When calling any image processing program, it will look to see if that intermediary program is running (if it's not start it) and then automatically get the frames captured with the camera.

I've been looking at popen(), but I'm a little confused. Basically the intermediary program would get the image and put it in a struct (it's in C), and I need to use that struct in the image processing program. How would I return that struct from the intermediary program?

If anyone could explain how to do this at a higher level, I would appreciate it. Definitely don't want someone to have to spell it out, but if you could point me somewhere with how to do it I would be very grateful!
 
Is this an okay place to post help on programs I may be currently working on? I don't want to bog down off topic with help threads anymore.
 
I'm trying to do something I've never done before with programming.

Basically I have a program that uses a webcam and does something to the image captured. While that program is running, I want another program to do something else with the images captured. The problem is that the programs can't use the webcam at the same time.

What I want to do is have an intermediary program take the images in memory and pass them off to the appropriate programs to use. When calling any image processing program, it will look to see if that intermediary program is running (if it's not start it) and then automatically get the frames captured with the camera.

I've been looking at popen(), but I'm a little confused. Basically the intermediary program would get the image and put it in a struct (it's in C), and I need to use that struct in the image processing program. How would I return that struct from the intermediary program?

If anyone could explain how to do this at a higher level, I would appreciate it. Definitely don't want someone to have to spell it out, but if you could point me somewhere with how to do it I would be very grateful!
It sounds like what you want to do is capture the images, process them in program A, then send them over to program B, which does something else with them, which would be inter-process communication (IPC). There's a few ways to do this, I only have very limited experience with this sort of stuff and I don't know anything about how to do this on Windows, but on Linux, I would probably put the images in a shared memory section or send them over with sockets or pipes. I don't really know which of those approaches is appropriate for your particular problem, but here's a really good guide for Linux IPC: http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html If you're using a shared memory segment, you'll probably have to look at how to synchronize access to it as well, since you don't want process A to overwrite the image as process B is reading it, for instance.

^^ Yes, of course you can ask questions.
 

Fantastical

Death Prophet
It sounds like what you want to do is capture the images, process them in program A, then send them over to program B, which does something else with them, which would be inter-process communication (IPC). There's a few ways to do this, I only have very limited experience with this sort of stuff and I don't know anything about how to do this on Windows, but on Linux, I would probably put the images in a shared memory section or send them over with sockets or pipes. I don't really know which of those approaches is appropriate for your particular problem, but here's a really good guide for Linux IPC: http://beej.us/guide/bgipc/output/html/singlepage/bgipc.html If you're using a shared memory segment, you'll probably have to look at how to synchronize access to it as well, since you don't want process A to overwrite the image as process B is reading it, for instance.

^^ Yes, of course you can ask questions.

That looks like a great reference! Thank you!
 
Does anyone have a good way of explaining how to do this in java?

You can initialize the elements of an array using a for loop:
Code:
double [] data = new double [100];
for (int i = 0 ; i < data.length; i++) {
data[i] = 1.0;
I am having trouble wrapping my head around this.

Where it says int i = 0 is that saying at position 0 in the index? also what does mean?
 

heytred

Neo Member
Does anyone have a good way of explaining how to do this in java?

You can initialize the elements of an array using a for loop:
Code:
double [] data = new double [100];
for (int i = 0 ; i < data.length; i++) {
data[i] = 1.0;
I am having trouble wrapping my head around this.

Where it says int i = 0 is that saying at position 0 in the index? also what does mean?


In Java a For Loop is similar to a While Loop, except that it has three parameters - initialization of a index/counter variable, a condition to continue the loop, and then an increment. So basically what's happening is this: an index is created with the variable i, and set to zero. Each loop will test the condition "i < data.length" which means the loop will continue until it has either incremented through every element in the array or met a certain goal/requirement/whatever within the loop (but not in this case since we are simply assigning a value to each element) and lastly "i++" is the increment amount. In this case, incrementing the index by a value of 1.

data is the position in the array. So what's happening step by step is from the beginning i = 0. So the first pass through the loop the first element in the array ( data[0] ) will be set to "1.0". The body of the loop will complete and so the For loop will then increment the index by 1, then test the condition again. Since 1 IS < data.length, the loop will execute again, setting the second position in the array ( data[1] ) to 1.0.

This will continue until i is no longer less than the length of the array.
 
In Java a For Loop is similar to a While Loop, except that it has three parameters - initialization of a index/counter variable, a condition to continue the loop, and then an increment. So basically what's happening is this: an index is created with the variable i, and set to zero. Each loop will test the condition "i < data.length" which means the loop will continue until it has either incremented through every element in the array or met a certain goal/requirement/whatever within the loop (but not in this case since we are simply assigning a value to each element) and lastly "i++" is the increment amount. In this case, incrementing the index by a value of 1.

data is the position in the array. So what's happening step by step is from the beginning i = 0. So the first pass through the loop the first element in the array ( data[0] ) will be set to "1.0". The body of the loop will complete and so the For loop will then increment the index by 1, then test the condition again. Since 1 IS < data.length, the loop will execute again, setting the second position in the array ( data[1] ) to 1.0.

This will continue until i is no longer less than the length of the array.


Code:
    String[] BoatNames = {"B.Boat","C.Boat","N.Boat",
                                "R.Boat","W.Boat","M.Boat"};
        for(int x = 0; x<BoatNames.length;x++) {
            System.out.println(BoatNames[x]);

Why does this work? I couldn't figure this out... and I kept telling the system to print out
BoatNames.length... I had to look at the instructors code to get it to work... but that wont help me in the long run... (Im going to go over what you typed here above, thanks for the reply)


Edit: So is this saying, FOR X which is an int of 0, use 0 as the starting point when 0 < the length add 1? Why does print BoatNames[X] print out the list of arguments?
 

Granadier

Is currently on Stage 1: Denial regarding the service game future
Edit: So is this saying, FOR X which is an int of 0, use 0 as the starting point when 0 < the length add 1? Why does print BoatNames[X] print out the list of arguments?

Each time the loop goes around it calls
Code:
System.out.println(BoatNames[x]);
and that call prints out element 'x' from the String array 'BoatNames'. So it's printing out each string from that array to the console.

Is that what you were wondering about? I didn't quite understand your question 100%.
 

heytred

Neo Member
It's literally saying this:

int x = 0 *The index or counter for the loop. It will be incremented by a certain amount (dictated within the parentheses after "for") each pass through the loop.

The next parameter, in this example, x<BoatNames.length is the condition tested after each pass through the loop. The loop will continue UNTIL this condition is FALSE.

The last parameter is the amount by which you will increment the index (or x in this example) AFTER each execution of the loop body.

In case there is any confusion about what the body of the loop is, it's the statements contained within the curly brackets { }.

So in the second example you provided, the for loop is printing out elements in an array. At the end of each pass (before the index is incremented and condition tested again) the Boat Name at position X in the array will be printed.

Does that make sense?
 
It's literally saying this:

int x = 0 *The index or counter for the loop. It will be incremented by a certain amount (dictated within the parentheses after "for") each pass through the loop.

The next parameter, in this example, x<BoatNames.length is the condition tested after each pass through the loop. The loop will continue UNTIL this condition is FALSE.

The last parameter is the amount by which you will increment the index (or x in this example) AFTER each execution of the loop body.

In case there is any confusion about what the body of the loop is, it's the statements contained within the curly brackets { }.

So in the second example you provided, the for loop is printing out elements in an array. At the end of each pass (before the index is incremented and condition tested again) the Boat Name at position X in the array will be printed.

Does that make sense?

ah ok, that makes it clearer... but my next issue was how could I use those names to create objects?

I kept getting an error like "SailBoat" cannot be converted to String[].

I have another class called sailboat and I wanted to create an object for each name in the array.
 

heytred

Neo Member
ah ok, that makes it clearer... but my next issue was how could I use those names to create objects?

I kept getting an error like "SailBoat" cannot be converted to String[].

I have another class called sailboat and I wanted to create an object for each name in the array.

If I understand what you're wanting to do, you'll need to create an array of the object type you want. You can't put these 'Sailboat' objects in a Str Array. Think of objects as a new Data Type you created. Just as you can't put Strings into an array of Integers. Treat it just the same as you would the examples earlier - create the array of those objects.

So for example: (keep in mind, I've only been messing with Java for a few months)

public static void main (String[] args) { // This is your main method

sailBoat[] a = new sailBoat[10]; // Creation of a 10 element array of the type 'sailBoat'.

for ( int i = 0; i < a.length; i++) { // For Loop for 'efficiency'.

a = new sailBoat(); } // Simple Constructor for the objects. It will create a new object at each element, and increment through the array, then finish the loop.


EDIT: If anyone who has more Java experience than I, feel free to correct any incorrect information I provide.

Also added Comments in case you aren't sure what's happening at each step.
 
Somehow I feel as though we are doing your homework for you =P

no don't worry I already created and solved the classes, using the examples the instructor left for us. I'm just trying to figure out how to write it differently....

I really do appreciate all the help.
 
If I understand what you're wanting to do, you'll need to create an array of the object type you want. You can't put these 'Sailboat' objects in a Str Array. Think of objects as a new Data Type you created. Just as you can't put Strings into an array of Integers. Treat it just the same as you would the examples earlier - create the array of those objects.

So for example: (keep in mind, I've only been messing with Java for a few months)

public static void main (String[] args) { // This is your main method

sailBoat[] a = new sailBoat[10]; // Creation of a 10 element array of the type 'sailBoat'.

for ( int i = 0; i < a.length; i++) { // For Loop for 'efficiency'.

a = new sailBoat(); } // Simple Constructor for the objects. It will create a new object at each element, and increment through the array, then finish the loop.


EDIT: If anyone who has more Java experience than I, feel free to correct any incorrect information I provide.

Also added Comments in case you aren't sure what's happening at each step.


What I was able to grasp from the example the instructor did was...

he created an additional array to store the values of the argument. I was under the impression we had to define the elements of the array within the first array, but he wanted us to use (in netbeans you can go to properties and type in your own arguments under run) some option to emulate user submitted arguments?

Anyway he had two array, I wanted to create one where the array elements I defined within the code could be stored and printed, but I kept getting 9 number 9's cause I had 9 elements typed into my array... it was not giving me text like I wanted.
 
I've started blogging (about programming). My first real posts will be about L-Systems and how I implemented them in Clojure, Scala and C#. I wrote all of the first part yesterday at about 2 AM, I went over it once today but if there's any mistakes or something, let me know. By the way, Tumblr's editor is shitty for longer posts like this. I wrote my post in Sublime Text using HTML instead of theirs and it was a lot better that way.
http://gyrosofwar.tumblr.com/
 

Linkhero1

Member
I just did an online interview programming problem for a company looking to hire and I'm pretty sure I fucked up the problem itself but I'm here to ask about something they've asked me to answer and I had no clue.

How does one determine the space and time complexity of a programming solution? Back in college we went over Big O notation but I don't recall talking about how to calculate it for a program.
 

usea

Member
I just did an online interview programming problem for a company looking to hire and I'm pretty sure I fucked up the problem itself but I'm here to ask about something they've asked me to answer and I had no clue.

How does one determine the space and time complexity of a programming solution? Back in college we went over Big O notation but I don't recall talking about how to calculate it for a program.
http://stackoverflow.com/questions/3255/big-o-how-do-you-calculate-approximate-it
 

Big Chungus

Member
just started c programming and i find this memory allocation stuff very difficult to understand...

does anyone know if there are any good resources to learn this stuff?

i'm more of a visual learned type of guy so if there are any videos or diagrams i would appreciate it.
 

Rapstah

Member
just started c programming and i find this memory allocation stuff very difficult to understand...

does anyone know if there are any good resources to learn this stuff?

i'm more of a visual learned type of guy so if there are any videos or diagrams i would appreciate it.

It's probably a lot simpler than you think. Read up on and completely grasp scope and the sizeof operator before you try to understand pointers and I think those two will help you a lot on the way. The basic idea is that the compiler can only handle memory for you as long as it knows exactly how much you're going to use beforehand. The second you (indirectly or directly) say "I need X bytes of RAM to store this string" instead of "I need 65 bytes of RAM to store this string" in your code, the compiler needs you to handle asking for and returning those X bytes of RAM.
 
Top Bottom