• 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

nan0

Member
It's not shown in the screencap, but I need the value of i to tell the user how many guesses he has left.

Yes, but you already do that in the loop by incrementing until MAX_NUMBER_OF_GUESSES is reached.
I see it's a lab assignment, so I'm guessing you should pass i via getter/setter to the other class?
In that case you'd have to just call the setter in the loop if the user failed to guess the number:

Code:
for(int i = 0;i < MAX_NUMBER_OF_GUESSES; ++i){

  if(secretNumber.makeGuess()){
    break;
  }
  else{
    secretNumber.SetGuessCounter(i);
    int triesLeft = MAX_NUMBER_OF_GUESSES - secretNumber.GetGuessCounter();
    System.out.println("Du här "+triesLeft+" försök kvar!"); // Sorry, I don't really speak swedish
  }
}
 

Majine

Banned
Yes, but you already do that in the loop by incrementing until MAX_NUMBER_OF_GUESSES is reached.
I see it's a lab assignment, so I'm guessing you should pass i via getter/setter to the other class?
In that case you'd have to just call the setter in the loop if the user failed to guess the number:

Thanks :)
 

Fantastical

Death Prophet
Is there really no way to connect to an ad-hoc network on Android without rooting? That puts a massive dent in an app I was planning for work.

EDIT: To be more specific, I was working on networking between nodes directly connected to each other using a ethernet switch. They weren't connected to any other external network. I assigned each node a static IP and just connected through those IPs. It doesn't seem as easy when you take it wirelessly, and I was thinking that the equivalent method would be an ad-hoc network. Is that correct? I just need an Android device to have an IP address and the Windows computer to have an IP address, and have them talk to each other without internet.

EDIT2: Could Wi-Fi Direct be a potential solution?
 
I just need an Android device to have an IP address and the Windows computer to have an IP address, and have them talk to each other without internet.

You seem to be conflating normal wifi with internet, when it is below IP in the network stack. Put something that acts as a wifi server (dedicated access point or network sharing on Windows, Mac, or bridging on Linux) on your private network and join the Android device to it; give it a static address and away you go.
 

RustyO

Member
Vague / generic / curiosity CRC question...

I'm playing around with a binary file sourced from a piece of hardware, which I can generate based on the different settings of the box; and I have a fairly good understanding of the structure and contents of the file, so can quite comfortably amend certain bytes, knowing the results will be correct.

Only issues is it not liking the CRC, which from what I can tell the CRC check at the end of the file is two bytes, e.g. B2 49

Given the above, how hard would it be to try and figure out the CRC algorithm and/or regenerate the CRC prior to sending back to the hardware?
 

Kansoku

Member
New problems, hurray!
First, Chrome is being a bitch about chrome://favicon/. I'm trying to put the favicon on a <a> element on a list trough JS, this way:
Code:
final+='<li><a href =" ' + url + ' " titlehref =" ' + url + '>'
+'<img src=" ' + favicon + ' " width="16" height="16" alt="">' + name + '</a>';
With the favicon var being favicon = 'chrome://favicon/'+url;
When I go see the results, the favicon doesn't apper, and when I inspect the element, the img src =" ... " doesn't have slashes (chrome://favicon/http://www.neogaf.com turns into chrome: favicon http: www.neogaf.com). And I have no idea why (Yes I put chrome://favicon/ in the permisions).

Second, I have a mouse out function for a div element, that right now is activated if I hover the child elements within the element, and I want it to activate only when I hover out of the div itself. I was using this, but it suddenly stopped working:

Code:
function sidebarOut(elem){
    var list = traverseChildren(elem);
    return function onMouseOut(event) {
        var e = event.toElement || event.relatedTarget;
        if (!!~list.indexOf(e)) {
            return;
        }
       var side = document.getElementById("sidebar");
	   side.parentNode.removeChild(side);
	};
}
function traverseChildren(elem){
    var children = [];
    var q = [];
    q.push(elem);
    while (q.length>0){
        var elem = q.pop();
        children.push(elem);
        pushAll(elem.children);
    }
	function pushAll(elemArray){
		for(var i=0;i<elemArray.length;i++){
			q.push(elemArray[i]);
		}            
	}
	return children;
}
 

CrankyJay

Banned
New problems, hurray!
First, Chrome is being a bitch about chrome://favicon/. I'm trying to put the favicon on a <a> element on a list trough JS, this way:
Code:
final+='<li><a href =" ' + url + ' " titlehref =" ' + url + '>'
+'<img src=" ' + favicon + ' " width="16" height="16" alt="">' + name + '</a>';
With the favicon var being favicon = 'chrome://favicon/'+url;
When I go see the results, the favicon doesn't apper, and when I inspect the element, the img src =" ... " doesn't have slashes (chrome://favicon/http://www.neogaf.com turns into chrome: favicon http: www.neogaf.com). And I have no idea why (Yes I put chrome://favicon/ in the permisions).[/CODE]

1. You didn't close your quotes (") for titlehref.
 
New problems, hurray!
First, Chrome is being a bitch about chrome://favicon/. I'm trying to put the favicon on a <a> element on a list trough JS, this way:
Code:
final+='<li><a href =" ' + url + ' " titlehref =" ' + url + '>'
+'<img src=" ' + favicon + ' " width="16" height="16" alt="">' + name + '</a>';
With the favicon var being favicon = 'chrome://favicon/'+url;
When I go see the results, the favicon doesn't apper, and when I inspect the element, the img src =" ... " doesn't have slashes (chrome://favicon/http://www.neogaf.com turns into chrome: favicon http: www.neogaf.com). And I have no idea why (Yes I put chrome://favicon/ in the permisions).

Second, I have a mouse out function for a div element, that right now is activated if I hover the child elements within the element, and I want it to activate only when I hover out of the div itself. I was using this, but it suddenly stopped working:

Code:
<snip />

First problem. I highly recommend not building HTML strings in the way you are doing. It is unsafe, messy and hard to read.

I know it's longer, but try something like:

Code:
var list_item = document.createElement("li");
var link = document.createElement("a");
link.href = encodeURIComponent(url);
link.setAttribute("titlehref", encodeURIComponent(url));
var img = document.createElement("img");
img.src = favicon;
img.width = 16;
img.height = 16;
link.appendChild(img);
list_item.appendChild(link);

This can be condensed by using jQuery.

----

Second problem, not certain. Hard to tell without a SSCCE.
 

Kansoku

Member
1. You didn't close your quotes (") for titlehref.

*facepalm*. Thanks.

First problem. I highly recommend not building HTML strings in the way you are doing. It is unsafe, messy and hard to read.

I know it's longer, but try something like:

Code:
var list_item = document.createElement("li");
var link = document.createElement("a");
link.href = encodeURIComponent(url);
link.setAttribute("titlehref", encodeURIComponent(url));
var img = document.createElement("img");
img.src = favicon;
img.width = 16;
img.height = 16;
link.appendChild(img);
list_item.appendChild(link);

This can be condensed by using jQuery.

----

Second problem, not certain. Hard to tell without a SSCCE.

I thought of doing that, but I do this on the background.js, and I need to add this stuff on I div I create on a content script. I'm passing is as a string which I add to the div with innerHTML IDK, but could I pass link_item after I run the function, and will it retain the children? (First time doing stuff with JavaScript, so I don't know much about it :p).

And second, I took the function form here: http://jsfiddle.net/amasad/TH9Hv/8/
 

CrankyJay

Banned
*facepalm*. Thanks.



I thought of doing that, but I do this on the background.js, and I need to add this stuff on I div I create on a content script. I'm passing is as a string which I add to the div with innerHTML IDK, but could I pass link_item after I run the function, and will it retain the children? (First time doing stuff with JavaScript, so I don't know much about it :p).

And second, I took the function form here: http://jsfiddle.net/amasad/TH9Hv/8/

Have you tried opening the JS console (guessing you're using chrome) by hitting f12 to see if your JavaScript is throwing an error?
 

Kansoku

Member
Have you tried opening the JS console (guessing you're using chrome) by hitting f12 to see if your JavaScript is throwing an error?

Yep. Everything is working alright, except that function (which doesn't trow any errors at the console). I could try debugging, but chrome debugger is kinda confusing.
 

CrankyJay

Banned
Yep. Everything is working alright, except that function (which doesn't trow any errors at the console). I could try debugging, but chrome debugger is kinda confusing.

Just throw in some console.log() calls so you can shoot out some debug strings to make sure the pertinent parts of the function are even being called.
 

Kansoku

Member
Just throw in some console.log() calls so you can shoot out some debug strings to make sure the pertinent parts of the function are even being called.

Did it, id doesn't enter these two:
Inside the function PushAll
Code:
for(var i=0;i<elemArray.length;i++){
			q.push(elemArray[i]);
		}
and
Code:
if (!!~list.indexOf(e)) {
            return;
        }

And I'm trying to do as Der Flatulator suggested, but using "document.createElement" on background.js is giving me a circular structure error. Is there any way to do this, or will I have to move it to another place (and then using a request for the background since I can't access chrome://favicon/ on a content script).
 

Fantastical

Death Prophet
You seem to be conflating normal wifi with internet, when it is below IP in the network stack. Put something that acts as a wifi server (dedicated access point or network sharing on Windows, Mac, or bridging on Linux) on your private network and join the Android device to it; give it a static address and away you go.

I'm not sure I understand. Would this solution work with only the Android device and the computer in the dessert or woods with nothing else? That's basically the use case for this application.

EDIT: Android development is fun! I read about something I don't know and branch off to 5 other pages of things I don't know.
 

Tristam

Member
It's not shown in the screencap, but I need the value of i to tell the user how many guesses he has left.

If this is your goal, and it isn't a requirement to have some sort of accessor, it seems more straightforward to simply pass the value of i as an argument to your makeGuess() method.
 

Linkhero1

Member
I have an interview with a huge tech company in silicon valley for a software engineering role. I don't think I'm fit for the role or ready for the interview. Anyone have any source for brushing up on computer science fundamentals and some data structure/algorithm problems I should look over?
 

Roubjon

Member
Hey guys I have a question. Do you find yourselves completely wiped by the end of the day? By the time there is around 1.5 hours left of work my productivity plummets and I can't think about anything with any clarity at all. This is my first programming job and I've been working here for a little over a Month, and I enjoy it for the most part. But that last 1.5 - 2 hours are torture and I feel guilty about not getting anything done.

Any advice?
 

Fantastical

Death Prophet
Hey guys I have a question. Do you find yourselves completely wiped by the end of the day? By the time there is around 1.5 hours left of work my productivity plummets and I can't think about anything with any clarity at all. This is my first programming job and I've been working here for a little over a Month, and I enjoy it for the most part. But that last 1.5 - 2 hours are torture and I feel guilty about not getting anything done.

Any advice?
I'm even worse than that. I wish I could go home and come back in like 3 hours sometimes. It's brutal.
 

CrankyJay

Banned
Hey guys I have a question. Do you find yourselves completely wiped by the end of the day? By the time there is around 1.5 hours left of work my productivity plummets and I can't think about anything with any clarity at all. This is my first programming job and I've been working here for a little over a Month, and I enjoy it for the most part. But that last 1.5 - 2 hours are torture and I feel guilty about not getting anything done.

Any advice?

Sounds like a diet issue or fatigue. Take short breaks. Drink more water. Take a walk.
 

pompidu

Member
So following a book and having trouble. I can add records succesfully but for some reason I cannot delete a record. I'm sure I'm overlooking something but I cannot find it. The Delete does run at all. Thanks guys!

Edit: NVM, of course I forgot a ".
 

oxrock

Gravity is a myth, the Earth SUCKS!
Hey guys I have a question. Do you find yourselves completely wiped by the end of the day? By the time there is around 1.5 hours left of work my productivity plummets and I can't think about anything with any clarity at all. This is my first programming job and I've been working here for a little over a Month, and I enjoy it for the most part. But that last 1.5 - 2 hours are torture and I feel guilty about not getting anything done.

Any advice?
My mental fatigue generally sets in a lot faster than that, you're lucky! Lots of short breaks is supposed to help.
 

oxrock

Gravity is a myth, the Earth SUCKS!
It's been a while since I've posted anything on my game "Deadly Dungeoneers" because to be honest, I kind of dropped the ball for a while. The ball has been picked back up and I've put a lot more work into it that I'd like to share with you all. I'm still very new to game development but until I start putting work out there, that'll never change. So comments, questions, criticism and high fives (if applicable) are most certainly welcomed.




ANIMATED GIFS:
(Because everybody loves a gif!)

rHmRuD9.gif
CDs1AL3.gif
D5sbi6q.gif



This is a shamelessly copied post from the independant game dev thread
 

Qurupeke

Member
Hello. I just finished high school and in a few months I'll be in a University to study electrical and computer engineering if everything goes as planned. Programming is important there and I have near to zero experience. I learned Basic a few years ago and recently (about a year ago) I learned a programming language for educational purpose that is very similar to Basic.

So, for the next months I have a lot of free time and I thought that this is my best chance to learn something advanced. I never really cared about programming till some months ago and actually I loved it. My question is, what language should I start with?

I'm interested in something that will help me in my future carrer, not just a language to start. Some friends I've talked with have said that I should start with Python, others propose C. I think that I'd like GAF's advice on that as there are quite a few programmers here.

And again, I have a lot of free time so something complex may not really be a problem.

Thanks in advance. :)
 

pompidu

Member
Hello. I just finished high school and in a few months I'll be in a University to study electrical and computer engineering if everything goes as planned. Programming is important there and I have near to zero experience. I learned Basic a few years ago and recently (about a year ago) I learned a programming language for educational purpose that is very similar to Basic.

So, for the next months I have a lot of free time and I thought that this is my best chance to learn something advanced. I never really cared about programming till some months ago and actually I loved it. My question is, what language should I start with?

I'm interested in something that will help me in my future carrer, not just a language to start. Some friends I've talked with have said that I should start with Python, others propose C. I think that I'd like GAF's advice on that as there are quite a few programmers here.

And again, I have a lot of free time so something complex may not really be a problem.

Thanks in advance. :)

I would first look through your degree requirements and see what programming class you have to take. Usually on your college website you can find the list of classes you have to take. It should say what language they are using.
 

Roubjon

Member
I would first look through your degree requirements and see what programming class you have to take. Usually on your college website you can find the list of classes you have to take. It should say what language they are using.

Yeah, if you're going to do some research ahead of time, I'd suggest looking into the language you're starting off in.

Regardless I went into programming during University without having any previous knowledge and although I enjoyed it, I really had no idea wtf I was doing until I had a good professor for the two core C programming classes. Once that happened I actually learned why I was doing the things I was doing before and because C deals so closely with the internal memory, I got to see how I was really effecting things.

I think most people go through that experience though. Because with programming you need to have proper structure starting off otherwise nothing will work, so it's a lot to digest at first, and you're always doing shit you have no idea why. That is, until later when you start learning about it.
 

Water

Member
I'm interested in something that will help me in my future carrer, not just a language to start. Some friends I've talked with have said that I should start with Python, others propose C.
Definitely not C. It's a specialist language that you shouldn't be using for vast majority of programming tasks even if you were already good with it. Also, you have to go all in with it, learning just a bit is useless. Python has good characteristics for a beginner and is a good companion to most other languages you might end up using - good for quickly hacking together whatever practical thing you might need, to automate your tools, build processes, testing, version control, etc. Through Numpy/Scipy, Python is also useful as an environment for scientific computation which might come in handy in a lot of physics and electrical engineering math stuff.
 

Qurupeke

Member
I would first look through your degree requirements and see what programming class you have to take. Usually on your college website you can find the list of classes you have to take. It should say what language they are using.

Most of the popular languages are there so I don't think that I'll have a problem with any language I choose. As far as I see it includes C, C++, C#, Java, Pascal, Prolog, Ruby, Python among others.
 

CrankyJay

Banned
Most of the popular languages are there so I don't think that I'll have a problem with any language I choose. As far as I see it includes C, C++, C#, Java, Pascal, Prolog, Ruby, Python among others.

Are you going for gaming programming or just in general/business software? Why not give C# a crack?
 

Water

Member
Are you going for gaming programming or just in general/business software? Why not give C# a crack?

He said he'll be going to school for electrical and computer engineering. C# makes no sense for that, doesn't have good beginner materials, and is not a handy scripting language so is useless whenever you aren't specifically working on a C# project.

This Fall I'm going to be teaching programming from scratch with C#, but it's absolutely not what I'd go with to start people on the path of becoming professional programmers. In this case my hand is forced since the main focus is teaching game prototyping and we'll be working in the Unity environment.
 

tokkun

Member
Hello. I just finished high school and in a few months I'll be in a University to study electrical and computer engineering if everything goes as planned. Programming is important there and I have near to zero experience. I learned Basic a few years ago and recently (about a year ago) I learned a programming language for educational purpose that is very similar to Basic.

So, for the next months I have a lot of free time and I thought that this is my best chance to learn something advanced. I never really cared about programming till some months ago and actually I loved it. My question is, what language should I start with?

I'm interested in something that will help me in my future carrer, not just a language to start. Some friends I've talked with have said that I should start with Python, others propose C. I think that I'd like GAF's advice on that as there are quite a few programmers here.

And again, I have a lot of free time so something complex may not really be a problem.

Thanks in advance. :)

It depends a bit on your objectives. If your goal is to learn about software engineering and design patterns for constructing programs, then you are probably much better off with Python. It is much higher level, easier to get started with, and requires less arcane knowledge than C.

However, since you are going to study ECE, if your goal is to gain a better understanding of hardware via programming you may be better off with C++, as it is possible to construct architectural microbenchmarks and has a more intuitive relationship between code and performance.
 

Qurupeke

Member
Python seems quite good for newbies. Easy language but quite powerful too. But this is what I am afraid of. C may be harder to learn but it sounds like a bridge to other languages like Java and C++. Python seems to be a great language but not useful if you're planning to learn more about programming. Or so I think.

Also, I'm pretty sure that it would be a bad idea starting with something like C++. I don't mind a hard language but C++ seems quite advanced for me.

I just want some programming experience and something that will potentially help me in the future. Until now I'm thinking either C or Python but apparently C may not be the best idea. Again, I don't mind a hard language for beginners, if this is C's main problem.
 

phoenixyz

Member
Personally I wouldn't want to learn C before learning some foundations on how computers work. I would also recommend Python. It's ideal for beginners imho.
 

Water

Member
Python seems quite good for newbies. Easy language but quite powerful too. But this is what I am afraid of. C may be harder to learn but it sounds like a bridge to other languages like Java and C++. Python seems to be a great language but not useful if you're planning to learn more about programming. Or so I think.
It's the opposite. Python and other good-for-beginners languages let you learn core programming skills faster whereas C and the like waste your time with tons of stuff that is simply poor usability. C is especially bad for learning on your own without instruction. Make yourself a decent programmer first, and learning new languages and dealing with their quirks is not a problem. Putting in half a year of hard work with Python, then half a year with C would be likely to make you a better C programmer than doing a year of C. Also, good C++ or Java code will tend to read more like Python code than C code.
Also, I'm pretty sure that it would be a bad idea starting with something like C++. I don't mind a hard language but C++ seems quite advanced for me.
...
Again, I don't mind a hard language for beginners, if this is C's main problem.
C++ is much more complicated than C, but actually provides better tools for the beginner in the unfortunate event that someone has to begin programming with either C or C++.
 

Massa

Member
I learned programming with C and it was great. It's a very simple language, the "hard" part comes from having to understand how computer memory works, which isn't a bad thing at all if you want to learn how to program.
 

emb

Member
Imo, your goals if you're trying to learn are to figure out how to:

a) Understand the logic and planning that goes into a (somewhat) complex project
b) Research, seek out, and debug language specific issues

It isn't super important which language you use for it. I think Python is a fine start, and you can probably get off the ground more quickly with it.

Can you find out what language your university is going to use for programming classes? I would check for that, and then try to learn something else. You're already going to have it taught to you, and if you're prepared you'll probably be ahead of the game anyway.
 

oxrock

Gravity is a myth, the Earth SUCKS!
As far as I know, python and java are the 2 languages most taught to newer programmers at university. I'm a python man myself and recommend it to anyone and everyone who's looking to break into programming. Especially with swift coming out and looking SOOOOO similar to python, it's just becoming an even better language to have in your arsenal. I'm a noob however and know nothing, discount my entire post :p
 

Qurupeke

Member
Can you find out what language your university is going to use for programming classes? I would check for that, and then try to learn something else. You're already going to have it taught to you, and if you're prepared you'll probably be ahead of the game anyway.

Pascal and C for Year 1.
 

BreakyBoy

o_O @_@ O_o
Definitely Python. I started with C ages ago and while it worked out just fine, I do wish I had Python back in the day to start with. Pascal was probably the best alternative I had back then.

As others have already mentioned, ultimately the language(s) you learn are secondary to getting a hold on the fundamental principles of programming. You'll get up to speed much faster with Python. You can always switch over to C later, and your experience with Python will be more valuable than you can probably imagine right now.
 
I learned programming with C and it was great. It's a very simple language, the "hard" part comes from having to understand how computer memory works, which isn't a bad thing at all if you want to learn how to program.

I don't agree. C might be a small language but it's not a simple language. The standard library is tiny (which means you have to write a lot of things yourself) and it lacks a ton of features that modern languages have. Also, the compiler does very little for you to protect you from doing horrible bad things (undefined behavior is everywhere) and you have to do error handling by yourself, explicitly. Not really relevant to a beginner, but C is also a security nightmare.

In contrast, Python, as an example, is "batteries included", so you can start doing cool stuff almost immediately because the standard library is very comprehensive, the syntax is even simpler than C and error handling is pretty easy with exceptions. The things C is good at (portability, low-level access to OS APIs, small footprint, runs on everything) aren't really that relevant to a newcomer to programming. You can always (and definitely should!) learn about pointers and contiguous memory and shared memory segments and what have you later.
 

CrankyJay

Banned
Not something specific. As I said I just want some programming experience as I'm just starting so I guess general software.

He said he'll be going to school for electrical and computer engineering. C# makes no sense for that, doesn't have good beginner materials, and is not a handy scripting language so is useless whenever you aren't specifically working on a C# project.

This Fall I'm going to be teaching programming from scratch with C#, but it's absolutely not what I'd go with to start people on the path of becoming professional programmers. In this case my hand is forced since the main focus is teaching game prototyping and we'll be working in the Unity environment.

He says otherwise. C# is as easy as anything to learn on. Too often do I run into people who started as a "scripter" and fall flat on their face when tasked with learning a new language. The point is to learn the programming constructs that are core to all major programming languages, not the language itself.

I saw this exact thing with Visual Basic programmers who probably got their start doing VBScript or VBA. .NET rolled around and we chose as a company to go with C# and the VB programmers flipped out.
 
Kind of weird question...

In Java (well, in many languages), is it a good idea to check if two objects are null by saying (o1 == o2) assuming that o1 and o2 will never be the same object by design? It's something I haven't thought about until now, but it seems like a good way to save a very tiny bit of speed.

I tried searching online to see what others had to say, but found nothing. Therefore, I'm kind of assuming it isn't a good idea, lol
 

Haly

One day I realized that sadness is just another word for not enough coffee.
My instincts say no. NULL checks are often used specifically to prevent cases where you suppose your design and implementation are correct, but when something fucks up, your program will throw some kind of exception/error and exit normally, rather than continue along as if nothing happened when there was actually a critical failure somewhere along the line.

Assuming o1 and o2 will "never be the same" is the kind of thinking that leads to bugs.
 
Kind of weird question...

In Java (well, in many languages), is it a good idea to check if two objects are null by saying (o1 == o2) assuming that o1 and o2 will never be the same object by design? It's something I haven't thought about until now, but it seems like a good way to save a very tiny bit of speed.

I tried searching online to see what others had to say, but found nothing. Therefore, I'm kind of assuming it isn't a good idea, lol

I don't think you'd gain a lot of speed from this. This sounds like a pretty dangerous micro-optimization that has more dangers than benefits. Null checks are very fast (should just come down to comparing the value of a pointer to zero) and they're probably the last thing you want to eliminate from your code for performance reasons. Also, they are a common idiom that any Java programmer will immediately understand.
 

Slavik81

Member
Kind of weird question...

In Java (well, in many languages), is it a good idea to check if two objects are null by saying (o1 == o2) assuming that o1 and o2 will never be the same object by design? It's something I haven't thought about until now, but it seems like a good way to save a very tiny bit of speed.

I tried searching online to see what others had to say, but found nothing. Therefore, I'm kind of assuming it isn't a good idea, lol
Why would you do that?
Member
Today, 07:32 AM, Post #9204
 
Top Bottom