Thread: loop to store values

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    loop to store values

    I have a loop that performs calculations and fills an array

    double d_SPerpendicularT[j]

    I want to store this array with a systematic name, run the loop again to calculate a new d_SPerpendicularT[j] and store it with a new name and so on.

    I'm doing this so that later I can write line 'j' from every array to a corresponding line in one big file.

    I'm stuck making a new variable name in the loop and then attaching the value of d_SPerpendicularT[j] to it.
    What I've tried to do is make a new string where the name changes every time the loop runs, but then I don't know how to use this string as a variable name to set equal to an array of numbers.

    Here's what it looks like:
    Code:
    s_VariablePerp.Format("s_%i%c%i_perp",dig1,letter1,dig2);
    
    //which makes a string that looks like s_0q0_perp, where 0q0 changes depending on the values used during in the loop
    
    //The following line doesn't work but basically what i want to do is use the string associated with s_VariablePerp as a variable name
    
    double s_VariablePerp[1000];
    
    //then i want to write to my new variable name
    for (int j = 1; j <= i_Angles; j++)
    	{
    		s_VariablePerp[j] = d_SPerpendicularT[j];
    	}

    Also let me know if I'm going about this entirely wrong or if you think there could be an easier way.

    thanks
    keendoc

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Maybe, try a 2D array.

    Code:
    double d_SPerpendicularT[j][20]
    Then after you do all your calculations and fill it up, you increase the second demension by one and do it all over again.

    Or, to do as you say "attach the value of d_SPerpendicularT[j] to variables", you can make a structure containing that variable.

    Code:
     struct Sperp {
        double d_SPerpendicularT[j];
        };
    
    int main() {
        Sperp foo[10]
    
    // Then use it with foo[x].d_SPerpendicularT[j]
    
    ...
    ...or something similar.
    Last edited by SlyMaelstrom; 10-28-2005 at 02:52 PM.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extract values and store in new variable
    By cosmiccomputing in forum C Programming
    Replies: 3
    Last Post: 06-01-2008, 01:45 PM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  4. Trying to store lots of values in a vector?
    By DARKGuy in forum C++ Programming
    Replies: 6
    Last Post: 09-10-2007, 03:41 PM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 05:16 AM