Thread: Help with some arrays.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    Help with some arrays.

    I get to the second half of the assignment and my brain doesn't seem to want to work, been up for two days straight :P. Anyways I just can't figure out how I can manipulate the arrays for what's needed. Here are the instructions (I'm not trying to get you to do the assignment, just want some tips on what I should do):

    Use double as the type for all of your arrays.

    Step 1: Write the following five functions that perform various vector operations:

    (a) Vector Addition. Two vectors can be added, by adding each of their values component by component. Hence, given two vectors [1,2,3] and [4,5,6], the sum of these vectors is found by adding the first components, then the second, then the third, yielding the answer: [1+4, 2+5, 3+6] = [5, 7, 9]. Note that in order to add two vectors, they must be the same size.

    Write a method called add, that takes one integer, and three arrays. The integer specifies the size of the vectors. The first two arrays are the two vectors to be added. The final array is a reference parameter, where the result of the addition will be stored.

    Here is an example of some C++ code that uses the add function, as just described.

    Code:
    double a[] = {1,2,3};
    double b[] = {4,5,6};
    double c[3];
      
    add(3, a, b, c);
      
    cout << c[0] << ", " << c[1] << ", ", c[2] << endl; // outputs 5, 7, 9
    (b) Vector Scaling. A vector can be scaled by a constant value, by multiplying each of the values in the vector to that constant value. Given a vector [1,2,3] and a constant value 5, scaling the vector by that constant is performed by multiplying the constant to the first value, then the second, then the third, yielding the answer [1*5, 2*5, 3*5] = [5,10,15].

    Write a method called scale, that takes one integer, one double, and two arrays. The integer specifies the size of the vector. The double value specifies the constant to scale the vector by. The first array is the vector to be scaled. The final array is a refernece parameter, where the result of the scaling will be stored.

    Here is an example of some C++ code that uses the scale function, as just described.

    Code:
    double a[] = {1,2,3};
    double c[3];
      
    scale(3, 5.0, a, c);
      
    cout << c[0] << ", " << c[1] << ", ", c[2] << endl; // outputs 5, 10, 15
    (c) Dot Product. The dot product of two vectors is found by multiplying each of their values component by component, and summing the resulting values. Given vectors [1,2,3] and [4,5,6] the dot product of the two vectors is (1 * 4) + (2 * 5) + (3 * 6) = 32.

    Write a method called dot, that takes one integer, and two arrays. The integer specifies the size of the vector. The two arrays are the two vectors from which to calculate the dot product. The return value of this funtion should be a double value equaling the dot product of the two vectors.

    Here is an example of some C++ code that uses the dot function, as just described.

    Code:
      
    double a[] = {1,2,3};
    double b[] = {4,5,6};
    
    double dp = dot(3, a, b);
    
    cout << "Dot Product = " << dp << endl; // outputs 'Dot Product = 32'
    (d) Vector Magnitude. The magnitude of a vector is found by squaring each component in the vector, summing these values, and then taking the square root. Given a vector [1,2,3], the magnitude of this vector is the value sqrt( 1*1 + 2*2 + 3*3 ) = sqrt(14) = 3.74166.

    Write a method called magnitude, that takes one integer, and one array. The integer specifies the size of the vector. The array is the vector from which to calculate the magnitude. The return value of this function should be a double value equaling the magnitude of the vector.

    Here is an example of some C++ code that uses the magnitude function, as just described.

    Code:
    double a[] = {1,2,3};
      
    double mag = magnitude(3, a);
    
    cout << "Magnitude = " << mag << endl;  // outputs 'Magnitude = 3.74166'
    HINT: Use the dot function in your magnitude function. This function can be written with a very small line of code.

    (e) Unit Vector. The unit vector is a vector whose magnitude is 1. A vector's corresponding unit vector can be found by dividing each component of the vector by its magnitude (this is also known as normalizing a vector). Given a vector [1,2,3], and recalling that the magnitude of this vector is 3.742, the unit vector is [ 1 / 3.74166, 2 / 3.74166, 3 / 3.74166 ] = [ 0.267261, 0.534522, 0.801784 ].

    Write a function called unit, that takes one integer, and two arrays. The integer specifies the size of the vectors. The first array is the vector from which to calculate the unit. The second array is a reference parameter, where the solution will be stored.

    Here is an example of some C++ code that uses the unit function, as just described.

    Code:
    double a[] = {1,2,3};
    double c[3];
    
    unit(3, a, c); 
    
    cout << c[0] << ", " << c[1] << ", ", c[2] << endl; // outputs 0.267261, 0.534522, 0.801874
    Step 2: Complete a program to test your five functions. The program should read two 3-valued vectors, A and B, from the user. The program should then output:

    * The sum of A and B.
    * A scaled by the value 7.
    * The dot product of A and B.
    * The magnitude of A.
    * The unit vector of A.

    NOTE: The 3-valued vectors in your main program are only for testing your functions. Your functions should be able to accommodate arrays of any size, not just of size 3.

    Here is an example of the program output:

    $./vec
    Please enter first vector: 1 2 3
    Please enter second vector: 4 5 6
    The sum of these two vectors = [5, 7, 9]
    Vector A scaled by 7.0 = [7, 14, 21]
    The dot product of A and B = 32
    The magnitude of A = 3.74166
    The unit vector of A = [0.267261, 0.534522, 0.801784]
    $

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK, you've posted your homework, what have you managed to do so far?

    This is so we don't waste time explaining stuff you already know, or explaining it in a way which is too complicated for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    This is the second half of the project. I can build most of the functions I just can't for example in the first one figure out how to add the two vectors if I don't know how big the array will be.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > add(3, a, b, c);
    I thought that was what the first parameter was, the length of the arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    No, the 3 is not suppose to be there. It is the variable "size". In the examples it is using 3 sized arrays but it should be able to handle any sized array entered by the user.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK, so store the size in a variable, increment that variable as the user enters information, then pass that variable as the first parameter to add().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM