help with vectors

Hi guys. Need help with a C excersise.
The teacher wrote the function for 2 vectors, V1 ( x1,y1,z1) and V2(x2,y2,z2)
to see if they are ortogonal. That means (x1x2+y1y2+z1z2) must be zero.
What Im wondering is, why is he using pointers even though he has not declared any pointers? Look at Line 4. Doesnt these stars mean pointers?
Or do they mean "contents pointed to by V1?

And why is he adding ++ to the vectors? Doesnt that skip the first number if he adds ++? The first number is in adress 0, which in this case is x1 and x2.
If he wants to calculate x1*x2 then he cannot because he just skipped them.



int ortogonal( int v1[], int v2[] ) {
int ort = 0, n;
for (n=0; n<3 ; n++){
ort=ort+((*v1++ )*( *v2++));
}
if(ort){
ort=0;
} else{
ort=1;
}
return (ort);
Last edited on
This may help a little bit. See the section Pointers and arrays on this tutorial page: http://www.cplusplus.com/doc/tutorial/pointers/

When an array is passed to a function, what is actually passed is a pointer to the start of the array. So v1 and v2 may be considered as either an array, or as a pointer, whichever is preferred or is most useful.

Regarding the use of the ++ operator, remember it may be used in two ways, either as a pre-increment ++x or post-increment x++. See operators, http://www.cplusplus.com/doc/tutorial/operators/
Last edited on
Thanks man. That was really helpful!
Allow me to ask another question.
There was an excersise where u had to compare a word to its reverse and see if its the same word forward and backwards. For instance Radar is the same is u read it forward or backward.

Look at line 3. As I have understood it, the teacher is trying to put the word radar like RadaradaR. And then compare the last letter to the first, and the second to the second to last. Etc.

But how can you explain that "word+size‐1" will make the word like RadaradaR? How can it become like that just by adding the size of the word and minus 1?

The teacher wrote

int pal=1;
char *back, *forward;

int size=strlen(word);
back=word+size‐1;
forward=word;
while( back>forward){
if(*forward!=*back) pal=0;
fram++;
bak‐‐;
}
return pal;
}
May I request that you put your code inside the tags, [code] your code here [/code] - use the <> button in the format menu on the right.

Also, the latest code you posted is incomplete, it has a return statement and closing brace but no matching code at the beginning, which makes it hard to follow.

I think you have not quite understood the code correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int palindrome (char * word)
{
    int pal=1;
    char *back, *forward;

    int size=strlen(word);

    back = word+size-1;
    forward=word;

    while ( back>forward)
    {
        if (*forward != *back)
            pal=0;
        forward++;
        back--;
    }

    return pal;
}

There are two pointers, char *back, *forward;

forward is set to point to the first letter of the word.
back is set to point to the last letter of the word.

The two characters pointed to are compared. If they are not the same, the word is not a palindrome, and so pal is set to zero.

Then the forward pointer is moved forward by one character, the back pointer is moved backward by one character. When the two pointers meet (or cross over) in the middle, the loop ends. Otherwise, it continues to compare each pair of characters.

The word itself remains unchanged at all times, it does not get longer or shorter, or change in any way.




Last edited on
Ah ok. Now I get it. I though that the word changed. Thank u very much! Im a beginner hehe.
Another question.
This is an excersise which i dont understand.
This is supposed to count and return a sum. The n is called an imparameter.
Ever seen such a thing before? Can you explain how it works and how to do it?


1
2
3

∑x=n, x=1   ((-1)^(x+1)) *(1/x) 

I believe that's just a mathematical description of the sum of the first n terms of a series.

So you need a loop like this
1
2
3
4
    for (int x=1; x <= n; x++)
    {
        // accumulate total of ((-1)^(x+1)) *(1/x) here
    }
but what is the value of n? What is n?
1
2
3
4
5
6
7
8
9
float nrsum( int n){ 
    float sum=0; 
    int x, sign=‐1; 
    for( x=1; x<n+1; x++){ 
         sign=(‐1)*sign; 
         sum=sum+ sign*((float)1/x); 
    } 
    return sum; 
} 


thats the code. Its hard to understand. Why does he take "n+1"? Why is he assigning "sign" to "-1" and then make "sign*(-1)"?
And why is he writing "((float)1/x)"? What does that float in parentheses indicate? Thats not how i learnt to use float.
Last edited on
What is n? It's any value supplied by the user. The same as the function sin(x) or sqrt(x), the value x is whatever the user chooses.

Do you understand the formula: ((-1)^(x+1)) *(1/x)

The first part is (-1) to the power of (x+1)
That's what the sign variable is used for.

Since x is an integer, 1/x will perform an integer division. In order to make it a floating-point division, the numerator 1 is cast to a float.
See type casting: http://www.cplusplus.com/doc/tutorial/typecasting/
sign=(‐1)*sign;

sign will always equal either 1 or -1, depending on the iteration of the for loop. The idea behind this is that every pass the operation of sum changes.The first time through, sum is adding the value. The second time through, sum is subtracting the value.

The (float) is a C style cast. It converts 1 to a floating point decimal just for the arithmetic. In C++, division with ints returns an int, division with either of them a float returns a float, and division with a double returns a double.

Essentially, the code looks like this:
1
2
3
4
5
6
7
8
9
float sum = 0; // Used to calculate the sum

for (int x = 1; x <= n; x ++) // get all values from x - n, including n
   if (x % 2) // if x is an odd number
      sum += (1.0 / x); // 1.0 is really a double, but works in this example
   else
      sum -= (1.0 / x); // Same as above, but doing subtraction instead

return sum;


It might not make much more sense now, but the comments may help understand the math behind it and how the code works.

If you want, I can help you with some other problems.
Ok, starting to get it. Is x<n+1 the same as x<=n??

So (float) is used to say that 1/x will be a float?

And if u look at this func. What is the parameter? Is is the whole name, or just the part in parentheses? Here is the name: int funkname( int sum1, int sum2);
Last edited on
Is x<n+1 the same as x<=n?
Yes, mathematically they are the same.

Though you might argue that x<n+1 is marginally less efficient as it involves the extra step of calculating the result of n+1, but in practice that doesn't matter, since the compiler will probably optimise the code in any case.
Last edited on
So (float) is used to say that 1/x will be a float?

More accurately, it is to say that 1 will be a float.
Think of it like this:
1
2
3
4
    int   a = 1;
    int   b = 2;
    float c = a / b;
    cout << c << endl;

The result is 0.

1
2
3
4
    float x = 1;
    int   y = 2;
    float z = x / y;
    cout << z << endl;

The result is 0.5.

Making at least one of the variables used in the divide operation of type float, forces the operation to be carried out using floating-point arithmetic.


int funkname( int sum1, int sum2);
The function takes two parameters: sum1 and sum2 , which is indeed the part in parentheses.
Last edited on
Topic archived. No new replies allowed.