Thread: Integration using pointer

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Integration using pointer

    So I was playing around with trying to do an integral using pointers. I've written what I'm trying to do already using arrays

    Code:
    #include <stdio.h>
    #include <math.h>
    #define RANGE 500 // Final temp = T0 + RANGE
    
    
    int main()
    {
        int i, j, h = 1, T0 = 300;
        float k300 = 148, alpha = -1.25;   
        float Temp[RANGE + 1], k[RANGE + 1], theta[RANGE];   
        float sum[RANGE], trap[RANGE - 1];        
        
        for (i = 0; i <= RANGE ; i++) 
        {
            Temp[i] = i + T0;                                     
            k[i] = k300 * pow((Temp[i]/T0), alpha);  
        }        
         
        for (j = 0; j <= RANGE; j++) 
        {
            trap[j] = 0.5*h*(k[j+1] + k[j]);                    // Trapezoidal Integration
            sum[j] = sum[j-1] + trap[j];                            
            if (j > 0) 
            {
              theta[j] = T0 + (1/k300)*sum[j-1];  
            }       
        }
        
        return 0;
    }
    which works just fine. Instead I want to try to go through it using pointers. Is there a good way to go through the first loop above, something like?

    Code:
        #include <stdio.h>
        #include <math.h>
        #define N 501 // Final temp = T0 + N
        
        
    int main()
    {
    
        int T0 = 300;
        float k300 = 148, alpha = -1.25;         
        float temp[N], kappa[N], theta[N -1];   
        float sum, trap[N - 1];  
        float *tl_i = temp, *tl_start = temp, *tl_end = (temp + N);   
        float *kappa_i = kappa, *kappa_end = (kappa + N), *kappa_start = kappa;
        float *trap_i = trap, *trap_start = trap, *trap_end = (trap + N - 1);
        float *kirch = theta;
        
    
        for (tl_i; tl_i<tl_end ; ++tl_i) 
        {
            *tl_i = (tl_i - tl_start) + T0;            
            *kappa_i = k300 * pow((*tl_i/T0), alpha);     
        }
    Last edited by shallowbay; 04-25-2013 at 10:33 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > float Temp[RANGE + 1], k[RANGE + 1], theta[RANGE];
    > float sum[RANGE], trap[RANGE - 1];
    All these +/-1 on your array sizes are an invitation to buffer overflows.

    > for (j = 0; j <= RANGE; j++)
    > trap[j] = 0.5*h*(k[j+1] + k[j]); // Trapezoidal Integration
    The last TWO indices of trap are off in the weeds somewhere.

    We'll discuss pointers when you can demonstrate bug-free (at least as far as subscripting goes) array code.
    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
    Mar 2013
    Posts
    11
    Thanks for the help! I believe that this is a little better:

    Code:
    #include <stdio.h>
    #include <math.h>
    #define RANGE 501 // Final temp = T0 + RANGE
    
    
    int main()
    {
        int i, j, h = 1, T0 = 300;
        float k300 = 148, alpha = -1.25;  
        float Temp[RANGE], k[RANGE], theta[RANGE];    
        float sum[RANGE], trap[RANGE];              
        
        for (i = 0; i < RANGE ; i++) {
            Temp[i] = i + T0;                           
            k[i] = k300 * pow((Temp[i]/T0), alpha);        
        }        
         
        for (j = 0; j < RANGE; j++) {
            trap[j] = 0.5*h*(k[j+1] + k[j]);                    // Trapezoidal Integration
            sum[j] = sum[j-1] + trap[j];                            
            if (j > 0) {
              theta[j] = T0 + (1/k300)*sum[j-1]; 
            }       
        }
        
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > trap[j] = 0.5*h*(k[j+1] + k[j]); // Trapezoidal Integration
    Still an issue with the j+1 on the last iteration of the loop.

    > sum[j] = sum[j-1] + trap[j];
    And here on the first iteration.
    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
    Mar 2013
    Posts
    11
    Quote Originally Posted by Salem View Post
    > trap[j] = 0.5*h*(k[j+1] + k[j]); // Trapezoidal Integration
    Still an issue with the j+1 on the last iteration of the loop.

    > sum[j] = sum[j-1] + trap[j];
    And here on the first iteration.
    I used printf statements to try to make sure everything was right. Results seemed good, so let's hope for the best.....

    Code:
    #include <stdio.h>
    #include <math.h>
    #define RANGE 501 // Final temp = T0 + RANGE
    
    
    int main()
    {
        int i, j, h = 1, T0 = 300;
        float k300 = 148, alpha = -1.25;  
        float Temp[RANGE], k[RANGE], theta[RANGE];    
        float sum[RANGE], trap[RANGE];              
        
        for (i = 0; i < RANGE ; i++) {
            Temp[i] = i + T0;                           
            k[i] = k300 * pow((Temp[i]/T0), alpha);        
        }        
         
        for (j = 1; j < RANGE; j++) {
            sum[0] = 0;
            trap[j] = 0.5*h*(k[j] + k[j-1]);                    // Trapezoidal Integration
            sum[j] = sum[j-1] + trap[j];                           
            theta[j] = T0 + (1/k300)*sum[j];
            printf("%f    %f\n", theta[j], Temp[j]);
        }
        
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    To answer your original question, yes you can always express arrays using pointer notation, but it is seldom worth the effort.

    So
    int a[N];
    for ( i = 0 ; i < N ; i++ ) a[i] = 0;

    becomes
    end = a + N;
    for ( p = a ; p < end ; p++ ) *p = 0;
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    To answer your original question, yes you can always express arrays using pointer notation, but it is seldom worth the effort.

    So
    int a[N];
    for ( i = 0 ; i < N ; i++ ) a[i] = 0;

    becomes
    end = a + N;
    for ( p = a ; p < end ; p++ ) *p = 0;
    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. Integration with C++
    By strokebow in forum C++ Programming
    Replies: 9
    Last Post: 10-06-2011, 06:15 AM
  2. Integration with Web?
    By dluthcke in forum C++ Programming
    Replies: 25
    Last Post: 09-30-2009, 12:02 AM
  3. PC Cam Integration
    By neo_phyte in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-25-2006, 01:34 AM
  4. Integration
    By westclok in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 05:06 AM
  5. Integration
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 01-03-2003, 04:08 AM