Dot Product (includes attachment)

This is a discussion on Dot Product (includes attachment) within the C Programming forums, part of the General Programming Boards category; I think you're way out of your league. It's hard to say whether you don't understand the programming aspect, or ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    I think you're way out of your league.

    It's hard to say whether you don't understand the programming aspect, or understand the math aspect.

    If you don't understand the math, then it's going to be hard explaining what each of those parameters are for.

    Since conj is unused inside the function, there's no point in passing anything useful, so
    Code:
    c_sdot ( 0,        // unused conjugate
             3,        // int n,
             0.0,      // float alpha,
             array_a,  // const float* x,
             1,        // int incx,
             0.0,      // float beta,
             array_b,  // const float* y,
             1,        // int incy,
             results   // float* r
        );
    Now paste that into your main() function and try compiling it.

    Then add a loop to print the results[] array, and then perhaps figure out whether the resutl is what you expect.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    attached is the code that i have been working on. could you please look at the code that i have written that is supposed to pass the arrays to the function so that it can calculate the answer
    Attached Files Attached Files

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Try this.
    Code:
    int main(void)
    {
       float array_a[3] = {1.0F,0.0F,1.0F};
       float array_b[3] = {3.0F,3.0F,3.0F};
       float result;
    
       c_sdot(blas_no_conj, 3, 1.0F, array_a, 1, 1.0F, array_b, 1, &result);
       printf("The answer is %f\n", result);
    
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #19
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    it gives me six errors

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >it gives me six errors

    And those might be...?
    Code:
    #include <stdio.h>
    
    enum blas_conj_type {
                blas_conj    = 191,
                blas_no_conj = 192 };
    
    void c_sdot(enum blas_conj_type conj,
                int n,
                float alpha,
                const float *x, int incx,
                float beta,
                const float *y, int incy,
                float *r)
    {
       int i, ix = 0, iy = 0;
       float *r_i = r;
       const float *x_i = x;
       const float *y_i = y;
       float alpha_i = alpha;
       float beta_i = beta;
       float x_ii;
       float y_ii;
       float r_v;
       float prod;
       float sum;
       float tmp1;
       float tmp2;
    
       if ( n <= 0 )
       {
          *r_i = 0.0;
          return;
       }
       r_v = r_i[0];
       sum = 0.0; /* sum = 0 */
    
    
       if ( incx < 0 ) ix = (-n+1)*incx;
       if ( incy < 0 ) iy = (-n+1)*incx;
       for ( i = 0; i < n; ++i )
       {
          x_ii = x_i[ix];
          y_ii = y_i[iy];
          prod = x_ii * y_ii; /* prod = x[i]*y[i] */
          sum = sum + prod; /* sum = sum+prod */
          ix += incx;
          iy += incy;
       } /* endfor */
       tmp1 = sum * alpha_i; /* tmp1 = sum*alpha */
       tmp2 = r_v * beta_i; /* tmp2 = r*beta */
       tmp1 = tmp1 + tmp2; /* tmp1 = tmp1+tmp2 */
       *r = tmp1; /* r = tmp1 */
    } /* end c_sdot */
    
    int main(void)
    {
       float array_a[3] = {1.0F,0.0F,1.0F};
       float array_b[3] = {3.0F,3.0F,3.0F};
       float result;
    
       c_sdot(blas_no_conj, 3, 1.0F, array_a, 1, 1.0F, array_b, 1, &result);
       printf("The answer is %f\n", result);
    
       return 0;
    }
    
    /* my output
    The answer is 6.000000
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #21
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    Thank you so much for your help! My program works now, thanks to you. Just for my own clarification what does this mean:

    1. enum blas_conj_type
    blas_conj = 191,
    blas_no_conj = 192

    2. Also when you declared the array why did you put an F behind them

    If I have any more questions I will be sure to ask you. You are very smart!

  7. #22
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Just for my own clarification what does this mean:

    1. enum blas_conj_type
    blas_conj = 191,
    blas_no_conj = 192
    I don't have the header file cblas.h that you are #include-ing. So I did a quick search and found a definition here.
    2. Also when you declared the array why did you put an F behind them
    My preference. Floating-point constants without F suffixes are doubles by default. With the F suffixes, they are floats.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #23
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    Thanks again for your help. If I need to reach you again. How do I do so

  9. #24
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    My preference. Floating-point constants without F suffixes are doubles by default. With the F suffixes, they are floats.

    But no matter what all floats are converted to doubles by the compiler. Doubles are qwords and the native data type of the FPU. There is no speed hit/gain from using doubles/singles as it takes just as many clock cycles for the FPU to load a float as it does a double and vice versa.

    Thanks to Fordy and others at flashdaddee for this information.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How Can I draw What I want?
    By CChakra in forum Game Programming
    Replies: 22
    Last Post: 09-11-2008, 09:38 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 01:48 PM
  4. Dot Product.......Help
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-07-2003, 08:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21