Maths equation

This is a discussion on Maths equation within the C Programming forums, part of the General Programming Boards category; Hi, I have a mathematical equation i was hoping to get some help with to get it working in C. ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Maths equation

    Hi,

    I have a mathematical equation i was hoping to get some help with to get it working in C. I'ts a small part of a program and if i figured it out it , i think i could figure out the rest of it! The equation is a second order derivative

    z[n] = x[n-1] - 2 x[n] + x[n+1]

    so for an array of 8 numbers say {2,5,7,4,3,5,9,8}
    it picks the number before the current no. - twice current no. + next number.
    For 5 in the array it would be z[n] = (2 - 10 + 7) = -1

    Here is my very poor attempt so far
    Code:
    #define MAX 8
    
    int z[MAX];
    int a[MAX] = {2,5,7,4,3,5,9,8};
    
    
        int main(void)
        {
         for (int i=0; i<8; i++)             
         z[i] = a[i-1] - (2(a[i]) + a[i+1];
         }
    I was hopingto store the ouput values into an array z. I know there is going to be a problem with the first value because n-1 = 0
    Any help would be greatly appreciated

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    (2(a[i])
    C doesn't understand implicit multiplication like in math. Make it 2*a[i].

    For your problem with the array bounds you could increment your array size by 1 and let the first element be a 0.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    thanks, the following error came up "for loop initial declaration used outside C99 mode" ???

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    i don't think you can declare your loop variables inside the loop, try doing it this way

    Code:
    int i = 0;
    for(i; i<8; i++)

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Excellent thanks, that fixed that problem and it compiles now anyway, i'm not sure what calculation it's doing exactly though but its not what i expected!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define MAX 8
    
    int a[MAX] = {14,10,5,0,65531,65524,65517,65511};
    int i;
    
    int z[MAX];
    
         
        /*      second order derivative      */
        int main(void)
        {
            
         int i = 0; for(i; i<8; i++)             // z[n] = x[n-1] - 2 x[n] + x[n+1]
         z[i] = a[i-1] - (2*a[i]) + a[i+1];
         printf("z[%d] = %d\n",i,z[i]);
         
         		
    		int ch;
            printf ("Press [Enter] to continue");
            while ((ch = getchar()) != '\n' && ch != EOF);
    	return 0;
         }
    This returns z[8] = 4007044, i was hoping to fill the array Z with correct answers to the formula and display them all, any ideas?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    An array of size 8 may be indexed from 0 to 7. So take extra care with such things as a[i-1] and a[i+1] (for example, plug in the starting and ending values of i and check which index this would be, and whether it is within the allowable range).
    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.*

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    as suggested ealier i plugged in 0s at the start and end of the array
    Code:
    #define MAX 10
    
    int a[MAX] = {0,14,10,5,0,65531,65524,65517,65511,0};
    and changed the for loop to
    Code:
      int i = 1; for(i; i<9; i++)
    so the array shouldnt go out of bounds for testing however ultimately i want to be able to pull in an array of 8 numbers and use this as a functon so i'm not sure how i'll be able to add the zeros at the start and end automatically and return 8 values to the array z?

    Now the output just displays z[9] = 0 , i'm stuck

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,707
    Ah I get it! It would be important to make this loop a compound statement. Try:
    Code:
      int i = 1; 
    for(i; i < 9; i++)  
    {
       z[i] = a[i-1] - (2*a[i]) + a[i+1];
       printf("z[%d] = %d\n",i,z[i]);
    }
    Last edited by whiteflags; 04-27-2006 at 02:47 PM.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Excellent thanks a million, it performs the calculation perfectly I've just one more small problem though if anyone could help. Would i be able to modify the it so it could read in an array of 8 numbers as a function, add the zeros at either end so the calculations is performed okay and the array doesnt go out of bounds, and return the values into the z array from z[0] to z[7] (at the moment the values are perfect but it goes from z[1] to z[8])

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define MAX 10
    
    int a[MAX] = {0,2,5,7,4,3,5,9,8,0};
    int i;
    
    int z[MAX];
    
         
        /*      second order derivative      */
        int main(void)
        {
            
           int i = 1; 
    for(i; i < 9; i++)  
    {
       z[i] = a[i-1] - (2*a[i]) + a[i+1];
       printf("z[%d] = %d\n",i,z[i]);
    }
         		
    		int ch;
            printf ("Press [Enter] to continue");
            while ((ch = getchar()) != '\n' && ch != EOF);
    	return 0;
         }

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by supermeew
    as suggested ealier i plugged in 0s at the start and end of the array
    I don't think that was the suggestion. My suggestion was to see what happens to i-1 and i+1 when i=0 and i=7, and whether you get indices of, say, -1 or 8 when you can only go from 0 to 7. But changing the loop to go from 1 to MAX-1 was the way to go. Note that the calculation cannot be done at the endpoints of the array. And by the way, if you are using MAX, don't later use a hard-coded 8. Frankly I don't care to use #defines this way.

    As far as defining another function, I might suggest starting with something like this.
    Code:
    #include <stdio.h>
    
    void foo(const int *a, size_t size, int *z)
    {
       size_t i;
       puts("foo");
       for ( i = 1; i < size - 1; ++i )
       {
          z[i] = a[i-1] - (2 * a[i]) + a[i+1];
          printf("z[%d] = %d\n", (int)i, z[i]);
       }
    }
    
    int main(void)
    {
       int a[] = {-5,-1,23,115,347,815,1639,2963};
       int b[] = {0,2,5,7,4,3,5,9,8,0};
       int z[sizeof b / sizeof *b]; /* same size as the bigger b array */
       foo(a, sizeof a / sizeof *a, z);
       foo(b, sizeof b / sizeof *b, z);
       return 0;
    }
    
    /* my output
    foo
    z[1] = 20
    z[2] = 68
    z[3] = 140
    z[4] = 236
    z[5] = 356
    z[6] = 500
    foo
    z[1] = 1
    z[2] = -1
    z[3] = -5
    z[4] = 2
    z[5] = 3
    z[6] = 2
    z[7] = -5
    z[8] = -7
    */
    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.*

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Cheers, thanks for yor help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-04-2006, 03:06 AM
  2. Rather difficult maths equation
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-21-2004, 08:31 AM
  3. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 10:46 AM
  4. Equation solving
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-24-2002, 01:13 PM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 08:21 PM

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