Thread: Nodal points

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91

    Question Nodal points

    Hi everyone,
    I am working on a code that generates n evenly spaced nodal points from a to b. It runs but I can't get to add interval to itself every iteration, and I am not sure if I got the formula right.
    Any ideas? Thanks, I appreciate it.

    Sample output should be:
    Code:
    a = 3
    b = 5
    n = 5
    3  3.5  4  4.5  5
    Code is:
    Code:
    #include <iostream>
    using namespace std;
    
    void LineSpace(float a, float b, int n, float arrayOut[]);
    
    int main()
    {
    	float a = 0.0f, b = 0.0f;
    	int n = 0;
    	cout << "Enter an interval [a,b] and number of nodal points n. " << endl;
    	cout << "Input a = ";
    	cin >> a;
    	cout << "Input b = ";
    	cin >> b;
    	cout << "Input n = ";
    	cin >> n;
    	float* arrayOut = new float[n];
    	cout << a;
    	LineSpace(a, b, n, arrayOut);
    	cout <<  " " << b << endl;
    	delete [] arrayOut;
    	arrayOut = 0;
    }
    void LineSpace(float a, float b, int n, float* arrayOut)
    {	
    	int i = 0;
    	float interval = 0.0f;
    	for (i = 0; i < n - 2; ++i)
    	{
    		interval = (b - a)/(n - 1); //<-----formula was originally 
    		           //(b-a)/ (n-2); I experimented with (n-1) and
    		           //it worked better.. not sure why?!?		
                    arrayOut[i] = a + interval;
    		cout << " " << arrayOut[i] << " ";
    		interval += interval; // <----- doesn't work... this is
    		           //supposed to add interval to itself each iteration	
             }
    }
    Last edited by alyeska; 01-31-2008 at 10:13 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,661
    Think of a fence.
    You need N panels, and N+1 posts.

    How about
    - calculate interval just once outside the loop.
    - use something like total += interval inside the loop.
    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
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Hi Salem..
    Thanks for your help.
    I made the revisions in the code but nothing changed in the output. Here's the revised function.

    Code:
    void LineSpace(float a, float b, int n, float* arrayOut)
    {	
    	int i = 0;
    	float interval = 0.0f, totalInt = 0.0f;
    	interval = (b - a)/(n - 1); //<-----formula was originally 
    		           //(b-a)/ (n-2); I experimented with (n-1) and
    		           //it worked better.. not sure why?!?
    	for (i = 0; i < n - 2; ++i)
    	{
    		arrayOut[i] = a + interval;
    		cout << " " << arrayOut[i] << " ";
    		totalInt += interval; // <----- doesn't work... this is
    		           //supposed to add interval to itself each iteration
    	}
    }
    I am also not sure what you meant here. Can you please elaborate? I appreciate your help and by that I meant you're not just giving me the answers but helping me figure it out myself.

    Think of a fence.
    You need N panels, and N+1 posts.

  4. #4
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    Some corrections:

    Code:
    void LineSpace(float a, float b, int n, float* arrayOut)
    {	
    	int i = 0;
    	float interval = 0.0f, totalInt = 0.0f;
    	interval = (b - a)/(n - 1); //<-----formula was originally 
    		           //(b-a)/ (n-2); I experimented with (n-1) and
    		           //it worked better.. not sure why?!?
    	totalInt = interval;
    	for (i = 0; i < n - 2; ++i)
    	{
    		arrayOut[i] = a + totalInt;
    		cout << " " << arrayOut[i] << " ";
    		totalInt += interval; // <----- doesn't work... this is
    		           //supposed to add interval to itself each iteration
    	}
    }
    And what Salem meant is that if you want N intervals, you'll need N+1 points. Example: [0, 1) in 4 intervals:
    Code:
    Intervals [0,0.25), [0.25,0.5), [0.5, 0.75), [0.75, 1);
    Points 0, 0.25, 0.5, 0.75, 1;

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Jacksonville, AR
    Posts
    91
    Thanks nepper.. it's much clearer now.. and the program's completely running.
    I appreciate all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. Replies: 1
    Last Post: 11-27-2007, 07:41 AM
  4. Yahtzee C++ programme help
    By kenneth_888 in forum C++ Programming
    Replies: 13
    Last Post: 09-05-2007, 02:14 PM
  5. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 04:30 PM