Coding in C: help with if statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MarioNintendo
    Expect delays.
    FFR Simfile Author
    FFR Music Producer
    • Mar 2008
    • 4177

    #16
    Re: Coding in C: help with if statements

    Originally posted by Shouka
    Change "x[i] " and "f[i]" to "x[M]" and "f[M]". Then, add the line "#define M 10000" to the top of your program. Let me know if that fixes your issues.

    What's happening is that you have only allocated space for 8 doubles in your arrays. In your for loop, the variable 'k' can range between 0 and 9999. So, when you execute the following lines:

    x[k]=xi+k*(xo-xi)/(N[j]-1);
    f[k]=sqrt(1-pow(x[k],2));

    you are writing to memory that was not allocated for those arrays.
    Holy cow, it worked beautifully! I set I to 100000 and manually set j<=8 in the while loop. It's working perfectly, now. Thank you so much! So, just to understand what I should do in the future if something similar happens: I set the value of my "define" variable equal to the maximum time I can accomplish one loop in my program?

    Also, arrays are zero indexed. So in the following lines:

    N[1]=3; /* I manually assignate a value for each N */
    N[2]=10;
    N[3]=30;
    N[4]=100;
    N[5]=300;
    N[6]=1000;
    N[7]=3000;
    N[8]=10000;

    you should start counting from 0 and not 1. The element N[8] is actually the 9th element and is not in the space you have allocated for the array.
    Oh, I was actually aware of that, this is why the first action I do in my "while" loop is j++, so j begins at 1. Although, you're right: if I set my j++ action at the end of the while loop, I can start counting at 0. My while condition became j<=7.



    thank you soooooooo much.

    Here's the final code:

    Code:
    #include <stdlib.h>
    #include <math.h>
    #define i 100000 /* We make i into a big number so we have a lot of reserved memory to make our calculations */
    int main(void)
    {
    	/* Declarations ------------------------------------------*/
    	double x[i]; 
    	double xi, xo; /* The ends of our interval */
    	int k, j;
    	double f[i];   /* This function will become f(x)=(1-x^2)^(1/2) */
    	double sum;
    	double error;
    	int N[i];
    	double pi;
    	pi=M_PI;   /* pi is really equal to pi */
    	
    	/* Executable --------------------------------------------*/
    	xi=0;
    	xo=1;
    	N[0]=3;             /* I manually assignate a value for each N */
    	N[1]=10;
    	N[2]=30;
    	N[3]=100;
    	N[4]=300;
    	N[5]=1000;
    	N[6]=3000;
    	N[7]=10000;
    	
    	j=0;
    	while (j<=7)           /* I do a loop of the precedent code for each different value of N already pre-defined */
    	{
    
    		sum = 0;      /* Accumulation variable */
    		for (k=0; k<N[j]; k++) 
    		{
    			x[k]=xi+k*(xo-xi)/(N[j]-1); /* Divide the line going from x=0 to x=1 in N-1 increments and go an increment farther each time this loop restarts */
    			f[k]=sqrt(1-pow(x[k],2));   /* The function on which we integrate */
    			sum+=2*(f[k]+f[k-1])*(x[k]-x[k-1]); /* Area of each trapeze that we sum with the accumulation variable named 'sum'  */
    		}
    		error=fabs(pi-sum);
    		printf("For N=%d,    Error= fabs( %f - %f ) = %f\n", N[j],  sum, pi, error);
    		j++;
    	}
    	/* printf("sum = %2.10f\n", sum); */
    	printf("\nEnd of line.\n\n");
    	getchar();
    }
    Last edited by MarioNintendo; 09-14-2013, 02:16 PM.

    Comment

    • Fission
      no
      FFR Simfile Author
      • Jan 2004
      • 1850

      #17
      Re: Coding in C: help with if statements

      why not just use a for loop? also, the general convention is to use "<" for counting loops, so you would just use j < 8 to accomplish the same thing.
      Last edited by Fission; 09-14-2013, 11:00 PM.

      Comment

      • dAnceguy117
        new hand moves = dab
        FFR Simfile Author
        • Dec 2002
        • 10097

        #18
        Re: Coding in C: help with if statements

        Originally posted by MarioNintendo
        So, just to understand what I should do in the future if something similar happens: I set the value of my "define" variable equal to the maximum time I can accomplish one loop in my program?
        The issue you encountered was related to arrays. When you declare an array, you give that array a certain size. The size of the array specifies how many elements the array can have.

        In particular, you ran into trouble with these lines.

        Code:
        for (k=0; k<N[j]; k++) 
        {
        	x[k]=xi+k*(xo-xi)/(N[j]-1);
        	f[k]=sqrt(1-pow(x[k],2));
        	sum+=2*(f[k]+f[k-1])*(x[k]-x[k-1]);
        }
        As you know, you set N[8] to 10000. When j eventually reaches a value of 8, k will then work its way up to a value of 9999 in this for loop (unless the program crashes sooner). Trying to index into x[9999] and f[9999] won't work unless those arrays are of at least size 10000.

        Shouka's suggestion fixed the problem because, by changing that line with #define in it, you also changed the sizes of your arrays. Your x and f arrays now have enough spots for x[k] and f[k] to be valid for all values which will be assigned to k in your program.

        Originally posted by MarioNintendo
        #define i 100000
        This line defines an identifier, i, as holding a value of 100000. i is not a variable; you cannot change its value after this line. I wanna say these identifiers are conventionally in all caps and are spelled out to describe what the value represents. So for this one, just to help make the code more readable, you could use something like

        Code:
        #define MAX_NUM_ITERATIONS 10000
        Last edited by dAnceguy117; 09-15-2013, 01:40 AM.

        Comment

        Working...