Thread: Another math question

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Another math question

    I'm taking a different approach to my program after getting some input from some of the forum members.

    My first question is if I want to display a result that can either be negative or positive, do I declare it as a float?

    My next question is a problem with my code I believe...I'm getting a 0.00000 as a result. Could someone please take a look at my code and tell me why this is my result.

    To explain my code n is the number of elements in the array, arrayx is the x values and arrayy is the y values in the array. Here is what the formula should do:

    arrayy[i] * (x – arrayx[i])*(x – arrayx[i+1])*(x – arrayx[i+2]) and so on...

    + arrayy[i+1] * (x – arrayx[i])*(x – arrayx[i+1])*(x – arrayx[i+2]) and so on...

    I want to set (x - arrayx[i]) = 1 in the first product and (x - arrayx[i+1] ) = 1 in the second product and so on...

    I hope that I didn't confuse anyone in my explanation. Anyways, here's the code that I have so far and am wondering why it doesn't work??

    Code:
    	int n=0;
    	int i=0;
    	double arrayx[12];
    	float arrayy[12];
    	float arraytop[12];
    	int x;
    	int j;		
    
    printf("Enter the x value you wish to interpolate: ");
    		scanf("%i", &x);
    
    	//Determine formula
    	for (i=0; i<=n-1; i++)
    	{
    
    		for (j=0; j<=n-1; j++)
    		{
    			if (arrayx[j] == arrayx[i])
    			{
    				arraytop[j] = 1;
    			}
    			arraytop[j] = x - arrayx[j];
    		//printf(" * (x - %i)", arrayx[j]); 
    		}
    		arraytop[i] = arrayy[i] * arraytop[j];
    	}
    		//Display of top value of formula
    		printf("%f", arraytop[i]);

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    I forgot to mention that this is only partially the code that I have. The value of 'n' is actually initialized. Sorry about that...I should have posted everything I guess.

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    My first question is if I want to display a result that can either be negative or positive, do I declare it as a float?
    As long as you don't declare it as unsigned, an int, float, or double could display a result that can be either negative or positive. Use int when dealing with whole numbers only, use float when there can be decimals, and use double only when you need a lot of precision.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  2. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  3. More a math question than an algorithm
    By Gustaff in forum C Programming
    Replies: 1
    Last Post: 01-28-2003, 01:10 PM
  4. Stupid Math Question....really stupid
    By ToLazytoSignIn in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-16-2003, 07:36 PM
  5. Math Question
    By DarkEldar77 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 12:52 PM