Could someone please walk through this code step-by-step?

1
2
3
4
5
6
7
8
float pos[3] = {0.0f, 0.0f, 0.0f};

float distance(float mob1[3], float mob2[3])
{
    return sqrt(sqr(mob2[0]-mob1[0]) + 
                sqr(mob2[1]-mob1[1]) + 
                sqr(mob2[2]-mob1[2]));
}


It comes from this tutorial: http://noobtuts.com/programming/c-plus-plus/tutorial-for-noobs-part-8-arrays.

Thanks. Is there also a way to make this print something out on the screen so that I can see what is happening? I find it easier to learn when I can see an output.
Last edited on
closed account (j3Rz8vqX)
The link you provided is no longer available or has an issue with its address, it displays: "The page has not been found".

The address that worked for me is:
noobtuts.com/programming/c-plus-plus/tutorial-for-noobs-part-8-arrays

BTW: The sqrt function was called incorrectly in the post, so I've fixed it.
It should be sqrt, not sqr.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
float pos[3] = {0.0f, 0.0f, 0.0f};
/*
   Declares an array of float,
      with a size of 3,
      with three values of 0 with the data type of float.
*/

float distance(float mob1[3], float mob2[3])
{
/*
   A function or method that takes two arrays of type float
      with each array having a size of 3
*/
    return sqrt(sqrt(mob2[0]-mob1[0]) + 
                sqrt(mob2[1]-mob1[1]) + 
                sqrt(mob2[2]-mob1[2]));
/*
   the square root of mob2[2] - mob1[2] plus
   the square root of mob2[1] - mob1[1] plus
   the square root of mob2[0] - mob1[0] then

   taking the square root of the resulting sum and

   returning a float
*/    
}


In the above code, and at it's corresponding site: In the array tutorial, they never call the distance formula once.

You can find the resulting float by simply calling this function with cout.

Note, that it would have 2 arrays of type float, with at least 3 data.

An example would be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
using namespace std;

float distance(float mob1[3], float mob2[3])
{
    return sqrt(sqrt(mob2[0]-mob1[0]) + 
                sqrt(mob2[1]-mob1[1]) + 
                sqrt(mob2[2]-mob1[2]));
}

int main()
{
   float float_arr1[3] = {6.0f, 2.0f, 12.0f};
   float float_arr2[3] = {22.0f, 27.0f, 48.0f};

   cout<<"result: "<<distance(float_arr1,float_arr2)<<endl;
   cout<<"Press enter or return to exit: "<<endl;
   cin.get();
   return 0;
}

Thanks man. Sorry about the link. Also, it mentions that this code finds the distance, what would the units be? Metres^2 I presume?

I'm on this part now:
http://noobtuts.com/programming/c-plus-plus/tutorial-for-noobs-part-10-strings

If you scroll down to the bottom it has the following code:
1
2
3
4
5
6
7
8
9
10
11
std::string a = "abc";
std::string b = "bcd";
if (a == b) {
    std::cout<<"equal!";
}

std::string a = "abc";
std::string b = "bcd";
if (a.compare(b) == 0) {
    std::cout<<"equal!";
}


I don't really get what it means. If I use this code I get the same result despite the tutorial saying that it's different. Perhaps someone could go into this more in-depth?
Last edited on
closed account (j3Rz8vqX)
You can actually validate if a string is equal by just applying the equality operator.

In the tutorial that you were reading, the editor was stating that the two compares were a bit different.


== can only return a boolean


std::string compare can returns a negative, 0, or positive value depending on the difference of the two strings.
http://www.cplusplus.com/reference/string/string/compare/
(Check the possible return values - located in the box diagram)

Here's what others have got the say about it:
http://stackoverflow.com/questions/9158894/differences-between-c-string-and-compare

Both compares strings.

One uses the standard comparison method (==)
and the other uses string's comparison method.
I see, I see.

What does the == !0 do? I've tested it out and I'm thinking that it means "if string is not the same then do..." in my head. I don't get a result if my code is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	std::string a = "abc";
	std::string b = "fda";
	if (a.compare(b) == !0) {
		std::cout << "not equal";
	}
	cin.get();
	return 0;
}
Last edited on
closed account (j3Rz8vqX)
The not(!) typically means its opposite.

Since booleans have only two possibilities (true and false), !0 would be 1.

http://www.cplusplus.com/doc/boolean/

1 usually represents yes/true, 0 usually represents no/false.

If (a.compare(b) == !0) //If (compare's returned value) (is equal) (not false); Not false literally means true(1).

Using the reference from before:
http://www.cplusplus.com/reference/string/string/compare/

a.compare(b) will return a value less than 0 (<0) because its first character 'a', from string a, has a smaller integer value than 'f', from string b; in this case, you got -1.

Normally it would return 0 if a complete match was found, and a value greater than 0 if the first string was larger; aka 1.

http://www.asciitable.com/
'a' = 97
'f' = 102

So, if (-1) (is equal to) (not false(0) aka true(1))
print out "not equal"

This would never happen, since boolean could only have a value of 0 or 1

This could be consider an undefined behavior, because we were expecting any value that was not 0 to be valid, but in this case, -1 was also not valid.

If your carious as to what the result of the comparison was you can try this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	std::string a = "abc";
	std::string b = "fda";
	cout<<"Result of comparison of string a and string b: "<<a.compare(b)<<endl;
	cout<<"Result of comparison of string b and string a: "<<a.compare(b)<<endl;
	if (a.compare(b) == !0) {
		std::cout << "not equal";
	}
	cin.get();
	return 0;
}


Edit: Posted at 3 AM, hopefully it's legible.
Last edited on
I'm not home at the moment so lemme run through this and tell me if it's right.

So basically, !0 is true? So 0 is false? That's correct?

So, since "a" has a lower value on the ASCII chart (it's 97) then I would get <0, this isn't possible because boolean has only 0 and 1 which means true or false and I would get no output.

However, if I say put "g" in-place of "a" I would get >0 which is 1? But if both were the same, I'd get a 0?
Last edited on
Sorry for jumping back to an earlier part of the thread, but the distance from one point to another (in three dimensional space) is defined as:
mod(v2 - v1)

Which in turn equals:
sqrt((v2.x)2 - (v1.x)2 +
     (v2.y)2 - (v1.y)2 +
     (v2.z)2 - (v1.z)2)

Which is a scalar value, hence the result being in units.

Also, false is defined as being the value zero, and true is anything that isn't false. Hope that clears things up.
Last edited on
I see. Thanks. However, is it possible to get a 1 when there is an output? I only seem to get -1 when the values are same and 0 when it's not the same. Ah, was said above. I'll have to potch about more when I get home.
Last edited on
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	std::string a = "abc";
	std::string b = "fda";
	cout<<"Result of comparison of string a and string b: "<<a.compare(b)<<endl;
	cout<<"Result of comparison of string b and string a: "<<b.compare(a)<<endl;//Was Edited to implement the correct opposite test
	if (a.compare(b) == !0) {
		std::cout << "not equal";
	}
	cin.get();
	return 0;
}


My apologies, at 2 AM i'm not my best.
The above code now shows that it is 1, when comparing b to a.
Topic archived. No new replies allowed.