• 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

Ashes

Banned
object orienting part in code academy could improve a little. Feeling dizzy.

More on this

I stumbled through this. Just felt like it was all over the place. And then came the review section (I think); where you're going over a few tasks so as to review what you learnt before; then you're relearning objects in the review section, except this time it's like every other lesson. Steady Logical progression.

Feels like the later lesson of Objects should have been the former. I mean even the grammar part about where to place commas was in this second section. Whereas you'd think it should be in the first part like the rest of lessons.

Edit: Way easier, logical, and coherent progress. For some reason my browser must have flunked and gone to this 'introduction to objects'.
Edit: I think this is the explanation:
Section 6 - Data structures: You dive in here with objects willy nilly.
Section 7 - Objects 1: here you actually learn step by step what objects are in Javascript, construction and grammar etc. Weird to use objects in section 6 then to have a proper intro to Objects in section 7.
 

Cptkrush

Member
Hey guys, I'm currently a "front end developer" at an ad agency, but my day to day involves more customer service and building sites in a theme builder than actual custom coding work. I know HTML/CSS like the back of my hand, but JavaScript is something I've always considered myself good enough at. My current goal is to be JavaScript focused, get the hell out of Florida, and work in Seattle.

I thought I knew how to program decently enough, and know the basics of JavaScript, but I am currently working through Eloquent Javascript -- picked it up when I started over a year ago, and never really read it--and struggling at chapter 5 -- Higher Order Functions. As someone that's been hacking together JS programs for websites for a little over a year, it's super eye opening to learn that I really know jack shit about this language, and probably programming concepts in general.

My plan is to re read that chapter first, but does anyone have some advice for learning JavaScript and OOP and functional programming concepts in general more deeply? Documentation is something I use to supplement examples already, but in terms of programming more clean and complex I have no clue where to go for that. Thanks for any help and advice!
 
Wondering if someone could verify my logic. Trying to add elements to separate arrays and factor in the leftover bit (bring over the one). It's ALMOST working but I think it's off bit a bit or two. Here's my logic:

Code:
int carryOverBit = 0;

			for (int i = 0; i < this.longIntArray.length; i++) {
				if (this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit  > 9) {
					summedArray[i] = this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit;
					carryOverBit = 1;
				} else {
					summedArray[i] = this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit;
					carryOverBit = 0;
			}
		}
 

Somnid

Member
Hey guys, I'm currently a "front end developer" at an ad agency, but my day to day involves more customer service and building sites in a theme builder than actual custom coding work. I know HTML/CSS like the back of my hand, but JavaScript is something I've always considered myself good enough at. My current goal is to be JavaScript focused, get the hell out of Florida, and work in Seattle.

I thought I knew how to program decently enough, and know the basics of JavaScript, but I am currently working through Eloquent Javascript -- picked it up when I started over a year ago, and never really read it--and struggling at chapter 5 -- Higher Order Functions. As someone that's been hacking together JS programs for websites for a little over a year, it's super eye opening to learn that I really know jack shit about this language, and probably programming concepts in general.

My plan is to re read that chapter first, but does anyone have some advice for learning JavaScript and OOP and functional programming concepts in general more deeply? Documentation is something I use to supplement examples already, but in terms of programming more clean and complex I have no clue where to go for that. Thanks for any help and advice!

Learn other languages, especially those that do things differently. If you want to stay in front-end give elm a whirl. It's purely functional so higher-order functions are basically on the same level as loops in OOP. It's also a pretty simple language and small standard library so it's not too overwhelming (but also lacks some common features you might expect). If you then want to move to something a little more practical (but still fairly niche) syntactically it makes a nice transition to Haskell.

OOP-wise, pick up Angular 2. With typescript it's designed to be familiar especially to those who transitioned from languages like C# and Java. It doesn't really leverage the interesting parts of javascript but it's very OOP.

Learning though just takes a lot of time and a lot of practice.
 
Wondering if someone could verify my logic. Trying to add elements to separate arrays and factor in the leftover bit (bring over the one). It's ALMOST working but I think it's off bit a bit or two. Here's my logic:

Code:
int carryOverBit = 0;

			for (int i = 0; i < this.longIntArray.length; i++) {
				if (this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit  > 9) {
					summedArray[i] = this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit;
					carryOverBit = 1;
				} else {
					summedArray[i] = this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit;
					carryOverBit = 0;
			}
		}

Four things:
1) What happens if the sum is more digits than either of the two input numbers? E.g. 8 + 5? You will end up with 3 and a carry bit of 1, but you should end up with 13 (i.e., you never add in that last bit)

2) Pull the long sum out of the if statement (both lines are identical), and then say carryOverBit = this.longIntArray + longIntArrayTwo + carryOverBit > 9

3) Use parentheses on if statements. The above is fine, but there are some really subtle rules with order of operations that can bite you in the ass. Always use parentheses if there could be any ambiguity. I would write

carryOverBit = (this.longIntArray + longIntArrayTwo + (carryOverBit > 9))

4) carryOverbit can never be > 0, it can only be 1 or 0. Perhaps you meant the last digit?
 

Kave_Man

come in my shame circle
Hi,

Just wanted to say found this thread randomly when I realized GAF probably has a programming community.

I decided this year I wanted to do something different so deciding to take up learning how to code a bit. After extensive research I decided I'd attempt to learn Python.

Right now tho going through Codecademy and relearning html and CSS then hoping this weekend to start learning Python.

I'll mostly just be lurking here reading all your posts but I'll be sure to ask for any help if I need it.
 
Four things:
1) What happens if the sum is more digits than either of the two input numbers? E.g. 8 + 5? You will end up with 3 and a carry bit of 1, but you should end up with 13 (i.e., you never add in that last bit)

2) Pull the long sum out of the if statement (both lines are identical), and then say carryOverBit = this.longIntArray + longIntArrayTwo + carryOverBit > 9

3) Use parentheses on if statements. The above is fine, but there are some really subtle rules with order of operations that can bite you in the ass. Always use parentheses if there could be any ambiguity. I would write

carryOverBit = (this.longIntArray + longIntArrayTwo + (carryOverBit > 9))

4) carryOverbit can never be > 0, it can only be 1 or 0. Perhaps you meant the last digit?


A little clarification.

Just to make sure I understand, it sounds like you've got two arrays of integers, and they're supposed to be "digits" in a longer number, and you want to add them together. You need something like this (in C++, you didn't specify what language you need though):

Code:
// The new array needs to be at least as large as the largest of the original
// array, but also needs to be one item longer, in case there is a carry in the
// last digit.  For example [1] + [9] would yield [1, 0].  Similarly, [9,9,9] + [1]
// would yield [1,0,0,0].
int sum_size = max(array_one.length(), array_two.length()) + 1;
std::vector<int> array_three(sum_size);

// But now our offsets are different.  The "last" item of array_three is one past
// the last offset of array_one and array_two.  Furthermore, the offsets
// of array_one and array_two might be different if they have different numbers
// of digits.  For example, 234 + 6 = [2,3,4] + [6].  For the first one you need to
// start at index 2, for the second you need to start at index 0.  In any case,
// the common element here is that all 3 arrays need to start at the *end* and
// then work towards the front.
auto a1 = array_one.rbegin();
auto a2 = array_two.rbegin();
auto a3 = array_three.rbegin();

// Even though they might have different numbers of digits, we need to keep
// iterating as long as there is at least one array left with digits remaining.
while (a1 != array_one.rend() || a2 != array_two.rend()) {
  int d1 =0, d2 = 0;

  // First get a digit from the first array, or 0 if this number has no more
  // digits.  Move its pointer to the left, setting it to nullptr if it has no more
  // digits.
  if (a1 != array_one.rend()) {
    d1 = *a1;
    ++a1;
  }

  // Then do the same for the second array.
  if (a2 != array_two.rend()) {
    d2 = *a2;
    ++a2;
  }

  // Add together the current digits, plus any existing carry (which has already been written
  // to this position in the output by the previous step).  Note that we use += since we need
  // to factor in any carry which was already written here by an earlier step.
  *a3 += d1 + d2;

  // The new carry is the result of dividing the sum by 10 and throwing away the remainder.
  // Write it straight into the next position.
  *(a3+1) = *a3 / 1000000000;

  // While the single-digit value for the output array is the modulus.
  *a3 %= 1000000000;

  // Move to the next digit in the output.  Again, the carry has already been written here.
  ++a3;
}
 

Ashes

Banned
Hi,

Just wanted to say found this thread randomly when I realized GAF probably has a programming community.

I decided this year I wanted to do something different so deciding to take up learning how to code a bit. After extensive research I decided I'd attempt to learn Python.

Right now tho going through Codecademy and relearning html and CSS then hoping this weekend to start learning Python.

I'll mostly just be lurking here reading all your posts but I'll be sure to ask for any help if I need it.

let me know how the html and css goes please. I might go on to css next.
 

Koren

Member
Just to make sure I understand, it sounds like you've got two arrays of integers, and they're supposed to be "digits" in a longer number, and you want to add them together.
I'm coming and going away, but just to clarify, he's developping a long-integer class using arrays of 32 bits ints, that are used as "digits", doing the computations in base-1000000000 instead of base-10.

So any 10 in div/mod should be 10^9 (and 9 should be 999999999)


Things are a little more complicated since numbers have a sign flag, also, but adding positive numbers is a start (there'll be a substraction function, anyway)
 
I'm coming and going away, but just to clarify, he's developping a long-integer class using arrays of 32 bits ints, that are used as "digits", doing the computations in base-1000000000 instead of base-10.

So any 10 in div/mod should be 10^9 (and 9 should be 999999999)


Things are a little more complicated since numbers have a sign flag, also, but adding positive numbers is a start (there'll be a substraction function, anyway)

Ahh, so this is a number in base 10^9? Are the digits stored right to left, left to right, or up to you? It's a bit easier to write the code if you store them in little endian, but a bit less intuitive since humans do math in big endian.

In any case, my algorithm above should still work, just like you said, change all my "10"s to 10^9 and change all my 9s to 10^9-1
 
esmith08734 said:
cpp_is_king said:
What inputs are you giving it and what outputs are you getting?

I am trying to add -350003274594847454317890 with itself.

So, if I understand correctly, you have an array that looks like this:

[3500, 327459484, 7454317890]
and a sign bit of -1.

And your current code looks like this:

Code:
for (int i = this.longIntArray.length - 1; i >= 0 ; i--) {
  if (this.sign == 1 && element.getSign() == 1
      || this.sign == -1 && element.getSign() == -1) {
    int totalSum = this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit;
    if (totalSum > 999999999) {
      summedArray[i] = totalSum % 1000000000;
      summedArray[i + 1] = totalSum / 1000000000;
    } else {
      summedArray[i] = totalSum;
    }
  }
}

Your first if statement just checks if they are both positive or both negative. I.e. if they are the same. Might as well shorten that to make the code easier to read:

Code:
for (int i = this.longIntArray.length - 1; i >= 0 ; i--) {
  if (this.sign == element.getSign()) {
    int totalSum = this.longIntArray[i] + longIntArrayTwo[i] + carryOverBit;
    if (totalSum > 999999999) {
      summedArray[i] = totalSum % 1000000000;
      summedArray[i + 1] = totalSum / 1000000000;
    } else {
      summedArray[i] = totalSum;
    }
  }
}

Now, your loop starts at the end of the array. So let's go step by step.

Code:
Loop Iteration i = 2
int totalSum = 7454317890 + 7454317890 + 0 = 14908635780
if (14908635780 > 999999999)   { // true
  summedArray[2] = 14908635780 % 1000000000 = 908635780
  // Do you see the problem in the below line?
  summedArray[3] = 14908635780 / 1000000000 = 14
}

That is just one of the problems though, there are still at least 2 others. For one thing, you still aren't setting the carry bit anywhere in your code.


I've mentioned it a couple of times but you still haven't answered yet. Have you looked at the code I posted earlier? There are a lot of edge cases here that you need to handle, you're not going to get a working solution without understanding all of them. I think you're starting with one test case and just trying to make that one test case work without understanding the entire problem. So I really think that before you make another attempt, you should read and try to understand the code I posted earlier and then ask yourself why my algorithm differs, and why every single step is important.

Edit: There's also a lot of missing information from the problem statement. Are digits stored left to right? Right to left? How long are the arrays? Are they padded with zeros to some predefined length, or if the number has 5 digits is the array exactly 5 elements long? You're not going to be able to get a working solution without understanding and addressing all of these.
 

Koren

Member
Ahh, so this is a number in base 10^9? Are the digits stored right to left, left to right, or up to you? It's a bit easier to write the code if you store them in little endian, but a bit less intuitive since humans do math in big endian.

In any case, my algorithm above should still work, just like you said, change all my "10"s to 10^9 and change all my 9s to 10^9-1
If I remember well, it is indeed little endian, with variable-sized arrays, no padding.

They don't say anything about the possibility of having leading zeros, though.
 
If I remember well, it is indeed little endian, with variable-sized arrays, no padding.

They don't say anything about the possibility of having leading zeros, though.

So wait, I might have missed part of this conversation, but do you two know each other or something? Just curious how you know about the details of his problem :)
 

Ashes

Banned
Finished with Javascript. Had trouble with objects and the like. And managed to wade through.

I know enough HTML to get by, and CSS I care less about, but in the same vain, I kinda know about it. But it seems pretty stupid in hindsigh to have such a big knowledge gap, as I keep hearing one should know HTML and CSS first before touching on Javascript.*

So I'm now going to go and learn HTML and CSS proper. On Code Academy.

edit:

*Also, this guy seems like a pretty good teacher, and has a more down to earth approach to learning.
https://youtu.be/qoSksQ4s_hg

He sounds like he's a gaffer to be honest. So I'm posting this in the hope, that he comes forward. :p
 
So wait, I might have missed part of this conversation, but do you two know each other or something? Just curious how you know about the details of his problem :)

He's helped me in the past with issues. I am still collecting my bearings. I don't like bogging down threads so if someone might have an idea as to what I am trying to do, I usually go to them instead of flooding the thread with responses. Working through it, though.
 

Jokab

Member
Graduating as MSc this summer and going on my first interview in one hour. Wish me luck, impostor syndrome is in full force atm
 

Koren

Member
Quick question...

I usually try to keep a wiki up to date with solutions I find each time I stumble upon a programming / latex / OS administration problem so that I don't have to search again next time...

I've used MediaWiki on one of my own machines.

Problem, my server has some troubles now, and I don't have time to solve all of them.

So I look for a free hosted online solution, that allows:
- code blocks (if possible with syntax coloring, looking mostly for C/C++, Python, Caml, C#, D, Scala, PHP, JS, Bash and LaTeX)
- mathematic formulas
- decent mobile accessibility
- export in a format that I'll be able to use to re-convert it into a personnal Wiki
- edition section by section on large pages (import would be great, too, but that's not a big issue)
- table support
- full text search, and possible auto-TOC

I may be interested in WYSIWYG but complete control is what I want the most (if there's WYSIWYG, it should only be a "shortcut", I want to be able to edit the pages in plain text)

Editing it without a graphical interface would be great (e.g. with links), but I can live without it.

If you can also store a couple images, that would be welcome, too.

Any suggestion?

I'm looking into PBWiki now, Wikia and Mindtouch being possible candidates, too. But I have never used such a tool, and I don't want to try half a dozen.
 

Slo

Member
Well this is god damned annoying. The project I'm doing for class is a real time net-promoter score based on sentiment analysis from Twitter. So you type in a query string like "iPhone", "Lebron James", or "Trump" and I query the Twitter API for tweets then I apply some machine learning classification models to approximate text sentiment.

The end result should be a web page that says "75% of people on Twitter really like blowjobs!" or something, with some useful bar charts, maybe a word cloud or something.

The problem is that Twitter users are deadpanning assholes, and all my Tweets are being scored with Neutral sentiment because nobody outwardly says that they like or dislike something.

Example "Ice Cream" Tweet:
Code:
{
    "tweetText":"Chocolate ice cream has been proven to significantly reduce emotional and physical pain.",
    "score":7,
    "probabilities":
    {
       "values":[
                      negative=0.002021625426482936,
                      neutral=0.9968185496160253,
                      positive=0.0011598249574918088
       ]
    }
}

Be more concise with your Tweets, people!
 

phisheep

NeoGAF's Chief Barrister
I'm looking into PBWiki now, Wikia and Mindtouch being possible candidates, too. But I have never used such a tool, and I don't want to try half a dozen.

I used PBWiki years ago, for keeping my notes on statute and case law and cross-referencing the hell out of them. Found it perfectly serviceable (after a shortish learning curve), though I can't remember if it has any of the other things your are looking for.
 

Megasoum

Banned
Hey guys, quick question.

So I've been put in charge of a project at work where I will have to script quite a bit of automation work.

We already have a system in place to handle the task scheduling and automating stuff but we need to create the scripts that will do the actual work.

This will be mostly moving files around, running command line executables and things like that... Nothing too complicated.

I have a bit of programming experience (mostly c++ and java like 15 years ago) and a bit of scripting using AutoIT a couple of years ago.

I could go back to AutoIT which would probably be the most "confortable" option for me but at the same time I want to move to a more mainstream language... Both for ease of maintenance by the rest of the team and because I want to learn something new.

Any suggestions on which language I should use?
 

Makai

Member
Hey guys, quick question.

So I've been put in charge of a project at work where I will have to script quite a bit of automation work.

We already have a system in place to handle the task scheduling and automating stuff but we need to create the scripts that will do the actual work.

This will be mostly moving files around, running command line executables and things like that... Nothing too complicated.

I have a bit of programming experience (mostly c++ and java like 15 years ago) and a bit of scripting using AutoIT a couple of years ago.

I could go back to AutoIT which would probably be the most "confortable" option for me but at the same time I want to move to a more mainstream language... Both for ease of maintenance by the rest of the team and because I want to learn something new.

Any suggestions on which language I should use?
Bash
 

Makai

Member
I wrote some Rust code that's like 1000x slower than the C++ I ported from.

Code:
char *row = (char *)memory;

    for (int y = 0; y < height; ++y) {
        int *pixel = (int *)row;

        for (int x = 0; x < width; ++x) {
            char red = (char)(x + offset);
            char green = (char)(y + offset);

            *pixel++ = (green | red);
        }

        row += pitch;
    }

Code:
            let mut memory = Vec::new();

            for y in 0..self.height as u32 {
                for x in 0..self.width as u32 {
                    let red = (x + self.offset) as u32;
                    let green = (y + self.offset) as u32;

                    memory.push(green | red);
                }
            }

So much for safety
 

Somnid

Member
I wrote some Rust code that's like 1000x slower than the C++ I ported from.

Code:
char *row = (char *)memory;

    for (int y = 0; y < height; ++y) {
        int *pixel = (int *)row;

        for (int x = 0; x < width; ++x) {
            char red = (char)(x + offset);
            char green = (char)(y + offset);

            *pixel++ = (green | red);
        }

        row += pitch;
    }

Code:
            let mut memory = Vec::new();

            for y in 0..self.height as u32 {
                for x in 0..self.width as u32 {
                    let red = (x + self.offset) as u32;
                    let green = (y + self.offset) as u32;

                    memory.push(green | red);
                }
            }

So much for safety

What if you preallocate the vector/use a slice?
 

Makai

Member
Actually, I just noticed it's a lot closer in release mode. Although C++ debug still beats Rust release :p

I didn't notice much difference if I put the vector on a struct ahead of time. Also, weird bug if I set the capacity in release mode - destroys the window border.
 

Somnid

Member
Actually, I just noticed it's a lot closer in release mode. Although C++ debug still beats Rust release :p

I didn't notice much difference if I put the vector on a struct ahead of time. Also, weird bug if I set the capacity in release mode - destroys the window border.

I just find it really odd this would produce anything different than the C++ compiler except for eating the resize penalty of the vector (since the C++ version uses a pointer on some preallocated memory), so if you fixed that you could disassemble it and check. If you really wanted to push it further, and you are absolutely sure you didn't miss anything you can pretty much write it verbatim using unsafe.
 

DBT85

Member
Just starting the JS part on Codecademy and I have to say its not as clearly explained as the HTML/CSS stuff has been.

I've been doing the werewolf thing on there and its getting me to use === to check that a variable = a thing. I can't understand why it's not just =. It said === is to see if two values are the same. To my mind that would be checking that var moonPhase and var moonPhase2 have the same value, not just checking if 1 variable = something. Earlier on it wanted me to do a similar check to see if a variable = a number and it was just laid out as if (numberThing = 5).

Code:
var moonPhase = 'full';

if (moonPhase === 'full')
 

Makai

Member
Just starting the JS part on Codecademy and I have to say its not as clearly explained as the HTML/CSS stuff has been.

I've been doing the werewolf thing on there and its getting me to use === to check that a variable = a thing. I can't understand why it's not just =. It said === is to see if two values are the same. To my mind that would be checking that var moonPhase and var moonPhase2 have the same value, not just checking if 1 variable = something. Earlier on it wanted me to do a similar check to see if a variable = a number and it was just laid out as if (numberThing = 5).

Code:
var moonPhase = 'full';

if (moonPhase === 'full')
Most languages use these:

= is assignment. You put the value on the right into the name on the left.
== is equality check. You check if the thing on the left is equal to the thing on the right.

JavaScript has === as a third option which doesn't convert types before making the equality comparison. You'll probably see this later in more detail.

The confusion with
Code:
if (numberThing = 5)
is because = returns true or false after assignment, so it assigned the variable numberThing to 5 and then returned true
 

DBT85

Member
Most languages use these:

= is assignment. You put the value on the right into the name on the left.
== is equality check. You check if the thing on the left is equal to the thing on the right.

JavaScript has === as a third option which doesn't convert types before making the equality comparison. You'll probably see this later in more detail.

The confusion with
Code:
if (numberThing = 5)
is because = returns true or false after assignment, so it assigned the variable numberThing to 5 and then returned true

Thanks for the explanation. I even think I understood it!
 

barnone

Member
Do we have a thread for CS/software engineering jobs? I've been doing a lot of mobile/web/api development for the past few years at my first job and want to branch out a bit. Not sure where to start though, my current job was attained at a college career fair.
 

Kalnos

Banned
Do we have a thread for CS/software engineering jobs? I've been doing a lot of mobile/web/api development for the past few years at my first job and want to branch out a bit. Not sure where to start though, my current job was attained at a college career fair.

https://news.ycombinator.com/item?id=13764728 (should be an April version of this thread soon)
Indeed.com
Angel.co (startups)
LinkedIn can be useful if you have a good network.

Don't think there's a specific GAF thread.
 

Koren

Member
Philosophical question... Why

a=b=0

is a thing in Python?

Is it just a strange choice done early they can't remove now ? Doesn't totally fit with the original philosophy, I think. Not that it's alone in that. I mean,

0<a<1

seemed an interesting idea till you discover this syntax lead to

True == False is False

which returns False, which is totally counter-intuitive...
 
Philosophical question... Why

a=b=0

is a thing in Python?

Is it just a strange choice done early they can't remove now ? Doesn't totally fit with the original philosophy, I think. Not that it's alone in that. I mean,

0<a<1

seemed an interesting idea till you discover this syntax lead to

True == False is False

which returns False, which is totally counter-intuitive...

a=b=0 works in lots of other languages too where assignment is an expression (most C-like languages), which probably why it works in Python as well (you're assigning "a" the result of "b=0" which is 0). I generally prefer a, b = 0, 0 because it's much clearer.
 
I recently got an internship and I got hired despite not knowing anything about C# and .net stuff. They said they expect to do a lot of self learning to catch up which is fine. What is the best and clearest tutorial/free class online to get myself caught up on C# and .net?

I fooled around with the one VS generated for you to understand a basic web page and that's the only thing I have ever done with C# & .Net.
 
Jesus mixing PHP and HTML is such a mess. I wanted to go with the route of throwing in php whenever I needed it instead of wrapping the whole file in it but between indentation and syntax highlighting is a clusterf in Sublime.

Does anyone have any advice on this? Does an IDE like PHPstorm alleviate this issue?

Edit: Apparently I just need to look into Heredoc?
 
Top Bottom