Hard coding in C++ - visual-c++

I have an assignment to submit in this week. And I did not understand this jargon "hardcode". It's my first class in C++ , where my professor asked me to demonstrate a simple math operation. By using all the 9 integral data types and a single "hard coded" value. Can some one please explain what hardcoding is? I am new to c++ and would appreciate if some one can throw some light on this.
Thanks!

Basically it means that the value is set inside the source code. If you asked for the variable it's not hardcoded, but if you say int x = 5; then x has been hardcoded to 5.

Related

Why an "i" in "for i in range"?

I'm new to programming and Python is my first language of choice to learn. I think it's generally very easy and logical and maybe that's why this minor understanding-issue is driving me nuts...
Why is "i" often used in learning material when illustrating the range function?
Using a random number just seems more logical to me when the range function is dealing with numbers..
Please release me from my pain.
A little extra help!
Asking why we use i is kinda like asking why so many people user the letter x in math problems. It is mostly because i is just a very easy variable to use to represent the current increment in a loop.
I also think you are confused about the place of i in a loop. When you use a range loop you are saying that you want to count one by one from one number until you hit another. Typically it would look like this
for i in range(0, 5):
This means I want to count from 0-4 and set ito the current loop I am currently on.
A great way to test this.
for i in range(0, 5):
print("i currently equals: ", i)
The result will be
i currently equals: 0
i currently equals: 1
i currently equals: 2
i currently equals: 3
i currently equals: 4
In your question you ask why don't you set i to a number and it is because you can not use numbers as variable names. Python can not accept what you are asking, but if it could it would look like this
for 54 in range(0, 5):
print(54)
Try reading up a little more on what variables are and how to properly use them in programming.
https://en.wikibooks.org/wiki/Think_Python/Variables,_expressions_and_statements
Lastly good luck in your pursuit to become a programmer! Coding is one of the most exciting things in this world to many of us and I hope one day you will feel the same!
i is used across nearly all programming languages to indicate a counting variable for a iteration loop.
Answered here.
i and j have typically been used as subscripts in quite a bit of math for quite some time (e.g., even in papers that predate higher-level languages, you frequently see things like "Xi,j", especially in things like a summation).
When they designed Fortran, they (apparently) decided to allow the same, so all variables starting with "I" through "N" default to integer, and all others to real (floating point). For those who've missed it, this is the source of the old joke "God is real (unless declared integer)".
Most people seem to have seen little reason to change that. It's widely known and understood, and quite succinct. Every once in a while you see something written by some psychotic who thinks there's a real advantage to something like:
for (int outer_index_variable=0; outer_index_variable < 10; outer_index_variable++) for (int inner_index_variable=0; inner_index_variable < 10; inner_index_variable++) x[outer_index_variable][inner_index_variable] = 0;
Thankfully this is pretty rare though, and most style guides now point out that while long, descriptive variable names canbe useful, you don't always need them, especially for something like this where the variable's scope is only a line or two of code.

Does Julia have a way to solve for unknown variables

Is there a function in Julia that is similar to the solver function in Excel where I can provide and equation, and it will solve for the unknown variable? If not, does anybody know the math behind Excel's solver function?
I am not expecting anybody to solve the equation, but if it helps:
Price = (Earnings_1/(1+r)^1)+(Earnings_2/(1+r)^2)++(Earnings_3/(1+r)^3)+(Earnings_4/(1+r)^4)+(Earnings_5/(1+r)^5)+(((Earnings_5)(RiskFreeRate))/((1+r)^5)(1-RiskFreeRate))
The known variables are: Price, All Earnings, and RiskFreeRate. I am just trying to figure out how to solve for r.
Write this instead as an expression f(r) = 0 by subtracting Price over to the other side. Now it's a rootfinding problem. If you only have one variable you're solving for (looks to be the case), then Roots.jl is a good choice.
fzero(f, a::Real, b::Real)
will search for a solution between a and b for example, and the docs have more choices for algorithms when you don't know a range to start with and only give an initial condition for example.
In addition, KINSOL in Sundials.jl is good when you know you're starting close to a multidimensional root. For multidimensional and needing some robustness to the initial condition, I'd recommend using NLsolve.jl.
There's nothing out of the box no. Root finding is a science in itself.
Luckily for you, your function has an analytic first derivative with respect to r. That means that you can use Newton Raphson, which will be extremely stable for your function.
I'm sure you're aware your function behaves badly around r = -1.

HaxeFlixel Puyo Puyo

I've been wanting to make a block type game for a while now but have never understood how to actually make one. I have googled forever and there is not much and what is there comes with a stipulation that I am not wanting to bother with (gpl license, entire code base, AND the license in any project, bleh). So I took to a forums with my problem. I did not know it, but I was trying to make a Puyo Puyo type game. With blocks dropping from the ceiling and then clearing if there's a match of 3 or more. I had no idea on how to do the matching. Which is what I wanted to know. A very nice, charming, and intelligent fellow provided me with this:
http://hastebin.com/ziyejejoxu.js
Granted, that's quite a lot, but the way he managed to code it allowed me to somewhat grasp it. However, there is a single infuriating problem. One, exactly ONE, line of code does not compile and breaks. I asked him if I could email him about it and he said okay. I haven't go a response yet so I may not be getting one so I'm taking this here. Here is how I am using the code so far. There are two parts, the play state, and the puzzle piece:
http://pastebin.com/SvMR9mMb
The program breaks in the playstate, giving this error:
source/PlayState.hx:291: characters 33-52 : Array access is not allowed on x : Int -> Int
What I have tried:
I had assumed that it was not allowed because the puzzle piece x is a float, and of course, you can't push a float into an int array. So what I did was simply in the puzzle piece first, convert the the float to an int. That did not work. THEN in the state, I switched the float to an int. That did not work. As an exercise, I attempted to convert a Flixel game to HaxeFlixel to see if I could learn anything. I probably did it wrong and did not.
So the question is: Why does that line not compile and what do I need to do to make it compile or to achieve it's intended purpose?
The syntax is wrong. push is a function, and function calls use (). [] is for array access (hence the error message).
This should work:
if (this_piece_is_in_a_match) matched_pieces.push(_i);

Is 5dp same as 5.0dp?

Reading android code I find out is not uncommon to fill the xml with the second variant, like here.
To be honest, this alone would be enough for my head to torment me with the question, but then I see code in which the two notations are even mixed, like in this StackOverflow question.
Google wasn't of help, and I couldn't find this question on SO.
Anyone knows what's the difference, if any?
EDIT
Based on the firsts question I got here, there seems to be no improvement in performance or other things like that when using the second variant, so writing them as float when they can be integers just tells people they can insert decimal values in there. Anything else?
If that's the only reason, I think using the second option it's just like polluting the xml for little to no reason, are there other opinions?
Check out this link: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension. It shows the possible values for dimensions. They are all floats. So, you can write them either way.
The 5.0 version is because certain properties accept a float/double value. All in all, 5.0 is the same as 5 when DP are involved.
one is an int the other is a float. They are types of primitives
It is the same. They both have the same values. 5 and 5.0 are of different types. 5 is of type int and 5.0 is of type float/double.

What is a Pointer? [duplicate]

This question already has answers here:
Closed 14 years ago.
See: Understanding Pointers
In many C flavoured languages, and some older languages like Fortran, one can use Pointers.
As someone who has only really programmed in basic, javascript, and actionscript, can you explain to me what a Pointer is, and what it is most useful for?
Thanks!
This wikipedia article will give you detailed information on what a pointer is:
In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining or requesting the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). Pointers to data improve performance for repetitive operations such as traversing string and tree structures, and pointers to functions are used for binding methods in Object-oriented programming and run-time linking to dynamic link libraries (DLLs).
A pointer is a variable that contains the address of another variable. This allows you to reference another variable indirectly. For example, in C:
// x is an integer variable
int x = 5;
// xpointer is a variable that references (points to) integer variables
int *xpointer;
// We store the address (& operator) of x into xpointer.
xpointer = &x;
// We use the dereferencing operator (*) to say that we want to work with
// the variable that xpointer references
*xpointer = 7;
if (5 == x) {
// Not true
} else if (7 == x) {
// True since we used xpointer to modify x
}
Pointers are not as hard as they sound. As others have said already, they are variables that hold the address of some other variable. Suppose I wanted to give you directions to my house. I wouldn't give you a picture of my house, or a scale model of my house; I'd just give you the address. You could infer whatever you needed from that.
In the same way, a lot of languages make the distinction between passing by value and passing by reference. Essentially it means will i pass an entire object around every time I need to refer to it? Or, will I just give out it's address so that others can infer what they need?
Most modern languages hide this complexity by figuring out when pointers are useful and optimizing that for you. However, if you know what you're doing, manual pointer management can still be useful in some situations.
There have been several discussions in SO about this topic. You can find information about the topic with the links below. There are several other relevant SO discussions on the subject, but I think that these were the most relevant. Search for 'pointers [C++]' in the search window (or 'pointers [c]') and you will get more information as well.
In C++ I Cannot Grasp Pointers and Classes
What is the difference between modern ‘References’ and traditional ‘Pointers’?
As someone already mention, a pointer is a variable that contains the address of another variable.
It's mostly used when creating new objects (in run-time).

Resources