• 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

Big Chungus

Member
I am not a Java dev, but it seems like all of the values not being written are integer values. Looking at the parameters for the write function, it looks like it really wants a string -- have you tried converting those values to strings?

that did it!

thanks a lot!
 

PlayDat

Member
Computer science major here. Just finished my freshman year and the final project was a game. I'd like some help turning it into something that can run on its own without an IDE or using command lines. The game was made in Java and I'm told the first step is to put the classes together in a jar file. I tried doing this in Eclipse but for some reason the game wouldn't run after using a wrapper tool on it. When I try making the jar file using the command prompt I see this message "'jar' is not recognized as an internal or external command, operable program or batch file." What am I doing wrong here?
 
Computer science major here. Just finished my freshman year and the final project was a game. I'd like some help turning it into something that can run on its own without an IDE or using command lines. The game was made in Java and I'm told the first step is to put the classes together in a jar file. I tried doing this in Eclipse but for some reason the game wouldn't run after using a wrapper tool on it. When I try making the jar file using the command prompt I see this message "'jar' is not recognized as an internal or external command, operable program or batch file." What am I doing wrong here?

a jar file is only the package, you still need a JRE to run it. Just like you need an app to open zip files.

Probably you want to bundle your .jar with a standalone jre and create a simple script that when executed calls that jre with your .jar as its target.

I think minecraft is done in Java and is standalone. Take a look at how that is packaged.
 
This sounds really interesting, as it combines two topics I'm currently interested in (C++ and L-Systems). I'll definitely try it on Linux, to compile I just have to use the makefile, right?
Yeah, you just need to install SFML 2.0. You either need to compile it yourself or download a pre-compiled package. Here's the tutorial I used:
http://sfml-dev.org/tutorials/2.0/start-linux.php
After that is done, just do "make all" in the LSystem directory and it should build. Don't forget to do export LD_LIBRARY_PATH=<sfml-install-path>/lib or else the executable won't start. The app is currently slightly broken because I started using a different method for scrolling around the image, that should be fixed soon (hopefully)

valgrind is more powerful than you think it is!

Code:
valgrind --tool=massif ./thingyouwanttorun
massif is a heap profiler. There is a program called massif-visualizer that shows you its output in pretty graph format:



There is also
Code:
valgrind --tool=callgrind ./thingyouwanttorun
...callgrind for more traditional CPU profiling. With kcachegrind it's basically the greatest invention since beer.
Awesome. thanks. That looks very promising.
 

Magni

Member
The new system would add discrete structures and comp organization and assembly language as well. I could take the internship(or the advanced c/c++) regards of what system I'm in. I just want to make sure I'm taking the best classes to help me along the way.

Keep in mind, that this is a 2 year school.



Any class in particular you feel you shouldn't have missed out on?

Thanks fellas, I appreciate your thoughts.

Ah right two year school (side-question, are you in China or am I confusing you with another C&H avatar?).

Definitely do the internship and C/C++ (every future software engineer should do some C at some point, preferably early on, IMO). Assembly is less essential unless you want to work on embedded systems, in which case it's absolutely essential.

Personally I'd stock up on theoretical courses (as much math as you can bear, data structures and algorithms), since the practical side of things you'll learn during the internship and all throughout your career.

Also, don't forget to ask older students which professors are good and which ones suck. I didn't my first semester, only took courses with great titles/descriptions, and that's a mistake I haven't repeated since.

A course seeming more interesting on paper than another doesn't necessarily mean you should take it over the over.
 
Dam so close finishing my minor project(Software Rendering no OpenGL or Dx) one last nasty bug and a refactor round.

soclosekquiu.png
 

injurai

Banned
Dam so close finishing my minor project(Software Rendering no OpenGL or Dx) one last nasty bug and a refactor round.

very awesome

may I inquire as to how one would start learning to write something like this? I really need some cool projects to actually motivate me to practice in my free time.
 
very awesome

may I inquire as to how one would start learning to write something like this? I really need some cool projects to actually motivate me to practice in my free time.

I am really into graphics and rendering and want to specialize in it.
So my train of thought was how did this all started.

If you're interested i would save up some money for july when the new version of the defacto CGI bible comes out. And fresh up your algebra and linear algebra skills. Or you can get

http://www.amazon.com/Computer-Graphics-Principles-Practice-3rd/dp/0321399528/ref=sr_1_1?s=books&ie=UTF8&qid=1370004389&sr=1-1&keywords=computer+graphics

Or get this one if you want to start now.
http://www.amazon.com/Fundamentals-Computer-Graphics-Peter-Shirley/dp/1568814690/ref=sr_1_4?s=books&ie=UTF8&qid=1370004389&sr=1-4&keywords=computer+graphics
 

phoenixyz

Member
Man, I'm in an Intro to Computer Programming this semester, learning the basic basics using Python.
The only thing that throws me sometimes is the switch between Python 2 in class and Python 3 on some other stuff I'm doing personally. That dang raw_input vs input annoys me, although I vastly prefer Python 3.
I would recommend using Python 2 at home too. Many modules and libraries are not availabe or stable for Python 3. And much of the resources about python on the net are still about version 2. Is there a certain reason why you use python3?
 
Dam so close finishing my minor project(Software Rendering no OpenGL or Dx) one last nasty bug and a refactor round.

Writing a software renderer is pretty fun. I did that about a year ago for an introductory computer graphics class, we got a Java framework and had to implement the most important stuff like line drawing, polygon clipping, Z-buffering, triangle rasterization and finally shaders. Writing a simple ray-tracer is something I've been meaning to do ever since, but I didn't really get to it yet.
 
Writing a software renderer is pretty fun. I did that about a year ago for an introductory computer graphics class, we got a Java framework and had to implement the most important stuff like line drawing, polygon clipping, Z-buffering, triangle rasterization and finally shaders. Writing a simple ray-tracer is something I've been meaning to do ever since, but I didn't really get to it yet.

The graphics courses i could choose are openGL based.
But i wanted to dive in deeper in the material and what is happening behind the scene so opted for a project. I used this minor period to learn c++,improve my math which is still meh level wish i could kick myself as a 13 year old for slacking in math.

Still have to re implement my line clipping stuff to work with triangles. Thought that rasterization was a more important milestone to show my minor supervisor. Still have to work out in the clipper how to clip a triangle that cover the complete screen.

I could have done a shitload more if i had access to a good mentor/teacher. My minor supervisor has also done all that software rendering stuff but he forgot most of it.

I still have one minor period to go i think i will do a project with dx11 or animation not sure which i will choose. Writing a simple raytracer seems like fun and from what i heard a lot easier then triangle rasterization based graphics.

All i really have to is write my minor documentation :'(
 

Aleph

Member
Yeah, you just need to install SFML 2.0. You either need to compile it yourself or download a pre-compiled package. Here's the tutorial I used:
http://sfml-dev.org/tutorials/2.0/start-linux.php
After that is done, just do "make all" in the LSystem directory and it should build. Don't forget to do export LD_LIBRARY_PATH=<sfml-install-path>/lib or else the executable won't start. The app is currently slightly broken because I started using a different method for scrolling around the image, that should be fixed soon (hopefully)

I was getting some compilation errors but I guess it was because I don't have SFML, as yu mentioned. I'll install and see if it works then.

Unrelated: just got "The C++ Programming Language", the C++11 edition (1346 pages...)
 
I was getting some compilation errors but I guess it was because I don't have SFML, as yu mentioned. I'll install and see if it works then.

Unrelated: just got "The C++ Programming Language", the C++11 edition (1346 pages...)

Wut it is out already totally forgot about it.

Edit:And bought. No drinking this weekend
 

Tantalus

Neo Member
From the few C++11 features that I've used, it seems really neat. Even just the range-based for loops and initializer lists being built into the STL make things a lot easier to write. Still not entirely sold on the "auto" keyword but maybe that'll grow on me over time too.

Might pick up a copy of The C++ Programming Language myself. I don't actually own any C++ books right now and what better place to start than with the man himself.
 

injurai

Banned
I am really into graphics and rendering and want to specialize in it.
So my train of thought was how did this all started.

If you're interested i would save up some money for july when the new version of the defacto CGI bible comes out. And fresh up your algebra and linear algebra skills. Or you can get

http://www.amazon.com/Computer-Graphics-Principles-Practice-3rd/dp/0321399528/ref=sr_1_1?s=books&ie=UTF8&qid=1370004389&sr=1-1&keywords=computer+graphics

Or get this one if you want to start now.
http://www.amazon.com/Fundamentals-Computer-Graphics-Peter-Shirley/dp/1568814690/ref=sr_1_4?s=books&ie=UTF8&qid=1370004389&sr=1-4&keywords=computer+graphics

Neat thanks, btw I couldn't find the De Facto CGI bible through any google searching so If I could get a link or something that would be great.
 
Neat thanks, btw I couldn't find the De Facto CGI bible through any google searching so If I could get a link or something that would be great.

The first link is the defacto CGI bible. Its the 3rd edition :p
Computer Graphics: Principles and Practice
I'm waiting for it to release, already saved up some money for it :)

Jim blinns Corner is also a good series i have "A trip down the graphics pipeline". A lot of the books are old. So that is why i advised to wait for the 3rd edition in july and im talking 1995 old.
 

injurai

Banned
The first link is the defacto CGI bible. Its the 3rd edition :p
Computer Graphics: Principles and Practice
I'm waiting for it to release, already saved up some money for it :)

Jim blinns Corner is also a good series i have "A trip down the graphics pipeline". A lot of the books are old. So that is why i advised to wait for the 3rd edition in july and im talking 1995 old.

Whoa, I had no idea how land mark of a release this 3rd edition would be. Guess I'm finding out about it at a great time.

Thanks a bunch man, you may be getting surprise pms from me down the road as I start going down the graphics route in my studies.
 

Slavik81

Member
Speaking of C++11, gcc 4.8.1 released today! They say "Support for C++11 ref-qualifiers was added to the GCC 4.8 branch, making G++ the first C++ compiler to implement all the major language features of the C++11 standard."

4.8 scares me a little, though, since it's optimization-engine has apparently become a little more of a language-lawyer. Woe is you if you invoke undefined behaviour. Still... that allows it to churn out faster programs if you do stick to the rules.


qbs 1.0 released today as well! It seems pretty cool. I like the idea of a build system with (semi)declarative syntax.

There seems to be a quiet revolution brewing in build tools. New build systems like tup, waf and qbs are looking much faster than make. Especially for incremental builds.
 

usea

Member
std::map<int, std::vector<std::pair<double,double>>>::const_iterator it = stuff.begin();

vs

auto it = stuff.begin();
Yeah, pretty much the best thing ever. I don't use c++ right now, but in C#, go and rust it is a great feature and they don't even have type/scope names as long as C++ sometimes does.

In rust, type inference goes a step further. It will infer the type from lines of code that take place later in the function. Like you could say
Code:
let x;
if something() < 10.0f {
  x = foo();
}
else {
  x = bar();
}
And as long as foo() and bar() return the same type it will compile. It's able to look ahead and figure out what type x is because of how it gets used later. Although that's not very idiomatic rust code for other reasons.
 

Tantalus

Neo Member
std::map<int, std::vector<std::pair<double,double>>>::const_iterator it = stuff.begin();

vs

auto it = stuff.begin();

I get why it's useful in situations like this, because declaring iterators is often verbose and hard to read. But take something like:

Code:
auto x = getValue();

for example. I could see it becoming more difficult to understand code written by someone else who is frequently using auto like this. Having the type declared explicitly might take longer to write, but wouldn't it help with readability?
 

injurai

Banned
I get why it's useful in situations like this, because declaring iterators is often verbose and hard to read. But take something like:

Code:
auto x = getValue();

for example. I could see it becoming more difficult to understand code written by someone else who is frequently using auto like this. Having the type declared explicitly might take longer to write, but wouldn't it help with readability?

Couldn't something like Clang, or an IDE be used to create auto comments or something alongside the code.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
for example. I could see it becoming more difficult to understand code written by someone else who is frequently using auto like this. Having the type declared explicitly might take longer to write, but wouldn't it help with readability?

Couldn't you solve that with comments or descriptive variable names?
 

Tantalus

Neo Member
Couldn't you solve that with comments or descriptive variable names?

Yeah, I guess descriptive variable names would make this less of an issue.

I've always been a strong believer in writing descriptive, readable code that doesn't need a lot of comments. If you are taking time to write comments explaining what the "auto'd" type actually is, it seems like it's defeating the purpose of using auto at all to some extent.

The more I think about it though, defining iterators using auto seems like it could be quite useful. It's pretty easy to understand what an iterator is there for; iteration over a container. And auto is sure to keep everything const-correct too. After not using it for a period of time, I tend to forget that const_iterator exists at all, haha.
 

Kalnos

Banned
New Grads (or anyone):

I have the option of either just applying to a few companies online or having my graduate adviser contact the HR they know at the company to forward my resume. If it matters, these are mostly small-medium sized companies that are local. Is there any preference here? My instinct tells me to use any possible connection but I kind of worry that it would get lost in someone's email inbox.
 

tokkun

Member
I get why it's useful in situations like this, because declaring iterators is often verbose and hard to read. But take something like:

Code:
auto x = getValue();

for example. I could see it becoming more difficult to understand code written by someone else who is frequently using auto like this. Having the type declared explicitly might take longer to write, but wouldn't it help with readability?

Yes, like most powerful things, it can be abused. You should not use auto in cases where
1. The actual type is not easy to discern for someone reading your code AND
2. It is important to reason about the type because changes to the type could affect correctness without being caught by the compiler.

For instance, if you were passing x to an overloaded function, assigning or copying x to another auto'd variable, performing a bit_cast or unsafe pointer cast, or were using it in an arithmetic operation where overflow was possible, then using auto would be a bad idea.

I mainly use auto to deal with templated variables (such as in my earlier example) or in cases where the scope of the variable is very short. It works great with range-based for.

Example:

Code:
for (auto channel : data_channels) {
  channel.Init();
}

If I end up changing the type of 'data_channels' later, this code may still compile without needing to be updated, and if it doesn't, the compiler will let me know.

This sort of behavior is great, because it can sometimes let you use static type-checking in situations where you would otherwise have relied on an interface class and polymorphism to achieve the same function, which can lead to more difficult to debug error situations.
 

Sandfox

Member
So I'm trying to program a graph using either DFS or BFS in C++ and I'm having a problem that I can't figure out. Does anyone have a good resource that I could use to get a better understanding so that I will be able to go through my code line by line? I really need to get this done this weekend and I've been stuck for a while lol.
 

usea

Member
So I'm trying to program a graph using either DFS or BFS in C++ and I'm having a problem that I can't figure out. Does anyone have a good resource that I could use to get a better understanding so that I will be able to go through my code line by line? I really need to get this done this weekend and I've been stuck for a while lol.
Not sure what you mean by this. Are you looking for an resource like a textbook/wikipedia, or a program that will let you step through your lines of code?
 

Sandfox

Member
Not sure what you mean by this. Are you looking for an resource like a textbook/wikipedia, or a program that will let you step through your lines of code?

I don't need a program. I just need a resource to learn more about what I am trying to do in order to see where the problem in my code.
 

missile

Member
I am really into graphics and rendering and want to specialize in it.
So my train of thought was how did this all started. ...
Software rendering ...

Unfortunately, software rendering is considered a lost art. But there is so
much power into it that the future will turn back on it some day. The GPUs
becomes so powerful that one can implement very sophisticated rendering
techniques, perhaps adapted for a special game, in software computed by the
GPU. For example, one can implement a software rasterizer to render perfectly
rounded shapes
on the GPU. With the increase in CPU/GPU performance it becomes
feasible computing your own rendering technique fitting the aesthetic of a
game you may have in mind. Look what SM, ND, and GG does. And wait until they
utilize the PS4's compute units to their fullest. More and more software
graphics code will be used to generate kind of a different look for a given
game, like it was prior to 1997 until the graphics accelerators took over.
However, this requires a fundamental understanding of rendering techniques out
of the API box. That's why I consider software rendering so important. Some
universities don't even teach and practice the fundamentals rightfully any
longer. The way they raster lines is by calling glBegin(GL_LINES). So props to
dragonelite doing it the other way around! I appreciate your work. :+

... may I inquire as to how one would start learning to write something like this? ...
Define the corners of an object and perspective project them onto the screen,
connecting them by lines getting a wireframe image. That's the basics start.

Hence,
(1) learn how to draw a pixel onto the screen,
(2) learn how to draw a line onto the screen (most cg students can't), and
(3) learn about perspective projection.

Do everything by hand, derive everything from scratch. Spend considerable time
with point (2) and (3). Don't use any API.

Getting the basics right gains momentum.


Here is software rasterizer I started to write a few months ago for the
DCPU-16. The framebuffer is 64x64 in size and has a depth of 1-bit per pixel.
Coded in fully 16-bit DCPU assembly language.

26964501.gif



I also implemented a simple lighting model and a halftone shader.

27991861.gif



... for the win.
 

Certinty

Member
Big noob here trying to work with Python. I'm trying to get this working so that if the user inputs a 4 bits in binary it'll convert it into denary. I know something is wrong with my recursion but I don't quite understand how to fix it, any help would be appreciated.

import math

Answer = 0
Column = 8

def loop():
Bit = int(input("Enter bit value: "))
Answer = Answer + Column*Bit
Column = Column/2

if Column > 1:
loop()
else:
print(Answer)

loop()
 

Tantalus

Neo Member
I mainly use auto to deal with templated variables (such as in my earlier example) or in cases where the scope of the variable is very short. It works great with range-based for.

Example:

Code:
for (auto channel : data_channels) {
  channel.Init();
}

If I end up changing the type of 'data_channels' later, this code may still compile without needing to be updated, and if it doesn't, the compiler will let me know.

Cool stuff. Range based for loops combined with auto make for some beautiful code when compared with manually declaring iterators.

Since we're on the topic of graphics right now, I have a quick question. I made a tile map system in SFML. It loads a bunch of numeric values or "tiles" from a text file, then creates a vertex array of 32x32 quads for each of these.

Could someone point me in the general direction of how to create a 2D lighting system? Something similar to what has been implemented in games like Terraria. I tried setting the colour to each tile as black and then setting it's transparency based on how close to a light source it is, but I'm not sure if there is an easier way. Thanks!
 

leroidys

Member
I don't need a program. I just need a resource to learn more about what I am trying to do in order to see where the problem in my code.

How well do you understand pointers? What kind of errors/bugs are you getting? Are you using classes or structs?
 

injurai

Banned
Software rendering ...

Unfortunately, software rendering is considered a lost art. But there is so
much power into it that the future will turn back on it some day. The GPUs
becomes so powerful that one can implement very sophisticated rendering
techniques, perhaps adapted for a special game, in software computed by the
GPU. For example, one can implement a software rasterizer to render perfectly
rounded shapes
on the GPU. With the increase in CPU/GPU performance it becomes
feasible computing your own rendering technique fitting the aesthetic of a
game you may have in mind. Look what SM, ND, and GG does. And wait until they
utilize the PS4's compute units to their fullest. More and more software
graphics code will be used to generate kind of a different look for a given
game, like it was prior to 1997 until the graphics accelerators took over.
However, this requires a fundamental understanding of rendering techniques out
of the API box. That's why I consider software rendering so important. Some
universities don't even teach and practice the fundamentals rightfully any
longer. The way they raster lines is by calling glBegin(GL_LINES). So props to
dragonelite doing it the other way around! I appreciate your work. :+


Define the corners of an object and perspective project them onto the screen,
connecting them by lines getting a wireframe image. That's the basics start.

Hence,
(1) learn how to draw a pixel onto the screen,
(2) learn how to draw a line onto the screen (most cg students can't), and
(3) learn about perspective projection.

Do everything by hand, derive everything from scratch. Spend considerable time
with point (2) and (3). Don't use any API.

Getting the basics right gains momentum.


Here is software rasterizer I started to write a few months ago for the
DCPU-16. The framebuffer is 64x64 in size and has a depth of 1-bit per pixel.
Coded in fully 16-bit DCPU assembly language.

26964501.gif



I also implemented a simple lighting model and a halftone shader.

27991861.gif



... for the win.

I've seen some of your stuff in the indie game dev thread. Do you have any sorts of other advice or recommendations on getting started. Resources, goals, approach of attack. My math skills aren't incredible, though I have a willingness to learn, and should be able to grasp just about anything given time. Eventually I want to be able to write my own shaders and things for engines like Unity, though I'm still beginning my computer science path. I'm trying to get away from all the high level languages that take the magic away from you (currently have an Internship where is use Groovy...)

I'm hoping to get a masters in graphics and CGI, really hate databases. Yuck.
 
I've seen some of your stuff in the indie game dev thread. Do you have any sorts of other advice or recommendations on getting started. Resources, goals, approach of attack. My math skills aren't incredible, though I have a willingness to learn, and should be able to grasp just about anything given time. Eventually I want to be able to write my own shaders and things for engines like Unity, though I'm still beginning my computer science path. I'm trying to get away from all the high level languages that take the magic away from you (currently have an Internship where is use Groovy...)

I'm hoping to get a masters in graphics and CGI, really hate databases. Yuck.

Work on your algebra that is only tip i could give. Software rendering is a lost art.
But understanding the train of thought behind it will make working with opengl and directx so much easier.
All i can give as a hint is GDI sucks ass fuck you microsoft it takes 400% more time then i need to render that simple affine mapped texture sphere.
Hope to get some dx11 stuff as a presenter i hear you can lock the backbuffer and write to it and present hope it will speed up thing significantly.
 

survivor

Banned
Spent the whole day today and yesterday working with threads, pipes, and processes for OS assignment. Some real challenging stuff, but still fun even though passing pipes around is a pain in the ass and causing me huge problems now.
 

leroidys

Member
I'm still a beginner with C++. I'm using classes and structs and I have an ok understanding of pointers.

Which part are you having trouble with, building the graph, or implementing the search?

Spend whole day today and yesterday working with threads, pipes, and processes for OS assignment. Some real challenging stuff, but still fun even though passing pipes around is a pain in the ass and causing me huge problems now.

Unix I'm assuming? Are you using popen()s or pipe()s? popens make it pretty simple
 

usea

Member
Big noob here trying to work with Python. I'm trying to get this working so that if the user inputs a 4 bits in binary it'll convert it into denary. I know something is wrong with my recursion but I don't quite understand how to fix it, any help would be appreciated.
Since nobody has answered you yet, I'll give it a shot. I don't know python.

You wiped out the formatting of your code when you pasted it. Formatting is important in python code, so if there's an error in your indentation or something we can't see it. Put it between [code] and [/code] tags.

You also didn't mention what problem you're having with the code. Why do you claim it doesn't work? Does it give you an error, or behave unexpectedly, or what?

This is python 3 right?

Maybe the error has something to do with the scoping of the Answer and Column variables? They're declared outside of the function, and I don't know what rules python has for reconciling local and global variables. Maybe try putting these two lines at the beginning of your function:
Code:
global Answer
global Column

A better suggestion is to pass those variables as arguments to your loop function. I don't have python 3 installed so I have no idea if this works or what.
 

hateradio

The Most Dangerous Yes Man
Big noob here trying to work with Python. I'm trying to get this working so that if the user inputs a 4 bits in binary it'll convert it into denary. I know something is wrong with my recursion but I don't quite understand how to fix it, any help would be appreciated.
As said . . .

If you forgot, you need white space. Python is delimited by it, so add it.

The other thing is that you do need to use the global keyword if a function is calling a variable outside of its scope -- the only exception are array variables, I believe.

Lastly, Column will not do anything because you're dividing it by half every time the function is called.

Column is 8, then it's divided by 2, which is 4, which is greater than 1, so it's going to loop until it's 1.

You either have to set Column to 8 inside the loop function or remove it as a global.


edit: Try this.

Code:
import math

Answer = 0
Column = 8

def loop():
    global Answer, Column;

    Bit = int(input("Enter bit value: "))
    Answer = Answer + Column * Bit
    Column = Column/2
    # print Column

    if Column > 1:
        loop()
    else:
        print(Answer)

loop()
 

usea

Member
Lastly, Column will not do anything because you're dividing it by half every time the function is called.

Column is 8, then it's divided by 2, which is 4, which is greater than 1, so it's going to loop until it's 1.

You either have to set Column to 8 inside the loop function or remove it as a global.
I think the intention is that it multiplies the first bit by 8, the second by 4, the third by 2, the last by 1. Because those are the values of the first four bits. It assumes the input is four characters, either 0s or 1s.

If it's set to 8 in the function, it will become an infinite loop and not behave correctly.

edit: nice catch with regards to getting the input every call, I didn't even notice.
 

hateradio

The Most Dangerous Yes Man
I think the intention is that it multiplies the first bit by 8, the second by 4, the third by 2, the last by 1. Because those are the values of the first four bits. It assumes the input is four characters, either 0s or 1s.

If it's set to 8 in the function, it will become an infinite loop and not behave correctly.
I misinterpreted then, so really it was just missing the global keyword.
 

Chaos

Member
So I'm in the process of learning my first language, C# using the Head First book, but I got stuck in their first lab activity, and it made me think that I might want to switch to JavaScript instead.

The head first books are not really good for starting from scratch.
Im trying to learn C# and these books look better for beginners -

Introduction to C# Joes 2 Pros - Peter Bako
Starting out with Visual C# - Tony Gaddis
 

Certinty

Member
Since nobody has answered you yet, I'll give it a shot. I don't know python.

You wiped out the formatting of your code when you pasted it. Formatting is important in python code, so if there's an error in your indentation or something we can't see it. Put it between [code] and [/code] tags.

You also didn't mention what problem you're having with the code. Why do you claim it doesn't work? Does it give you an error, or behave unexpectedly, or what?

This is python 3 right?

Maybe the error has something to do with the scoping of the Answer and Column variables? They're declared outside of the function, and I don't know what rules python has for reconciling local and global variables. Maybe try putting these two lines at the beginning of your function:
Code:
global Answer
global Column

A better suggestion is to pass those variables as arguments to your loop function. I don't have python 3 installed so I have no idea if this works or what.
As said . . .

If you forgot, you need white space. Python is delimited by it, so add it.

The other thing is that you do need to use the global keyword if a function is calling a variable outside of its scope -- the only exception are array variables, I believe.

Lastly, Column will not do anything because you're dividing it by half every time the function is called.

Column is 8, then it's divided by 2, which is 4, which is greater than 1, so it's going to loop until it's 1.

You either have to set Column to 8 inside the loop function or remove it as a global.


edit: Try this.

Code:
import math

Answer = 0
Column = 8

def loop():
    global Answer, Column;

    Bit = int(input("Enter bit value: "))
    Answer = Answer + Column * Bit
    Column = Column/2
    # print Column

    if Column > 1:
        loop()
    else:
        print(Answer)

loop()
Thanks so much guys, just starting to get grips with Python and you both explained what I needed to do perfectly. Very much appreciated.
 
Work on your algebra that is only tip i could give.

echo this. though I'm only a lowly openGL coder at the moment, would like to work my way up to software rendering one day perhaps.

(2) learn how to draw a line onto the screen

this however, I can do! wrote a little game on this DS many moons ago that was simple 2D vector graphics using a line-drawing algoithm. pretty much kickstarted my rediscovery of programming. now I'm on a mad dash to get my 3D spacewars assignment (C / OpenGL) handed in. 6 and a half hours to go!
 
Top Bottom