468,512 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

generating an array of arrays

1
Good day,

I am attempting to generate an array of arrays. The code compiles but hangs when I run it (suggesting an infinite loop perhaps?). I am unsure how code would be necessary for a wise and kind :) person to help me, but I have included what I deemed necessary.

Thank you

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <cmath>
  6. #include <stdio.h>
  7. #include <cstdlib>
  8. #include <ctype.h>
  9.  
  10. using namespace std;
  11.  
  12. int main (){
  13.  
Skip opening and reading in initial data and finding distances between points (sab_array and tab_array).
These sections work correctly.

nsteps and narray are previously entered constants.

Here is where I am trying to generate the array of arrays.

Expand|Select|Wrap|Line Numbers
  1.   vector<double> tsab_array;
  2.   for(int j=0; j<narray;j++)
  3.     {
  4.       for(int i=1; i=nsteps; i++)
  5.     {
  6.     double tsab = (1-1/i)*sab_array[j] - i*(tab_array[j]);
  7.     tsab_array.push_back(tsab);
  8.     }
  9.     }
  10.   for(vector<double>::iterator v=tsab_array.begin(); v != tsab_array.end(); v++)
  11.     {
  12.       cout<<*v<<"\n";
  13.     }
  14.  
  15.   return 0;
  16. }
  17.  
Jul 18 '06 #1
2 3775
D_C
293 100+
In the second for loop, you should be testing i != nsteps instead of assigning i = nsteps.
Jul 18 '06 #2
Banfa
9,056 Expert Mod 8TB
D_C is right also even if you correct that unless you are sure that nsteps >= 1 then you might get what appears to be an infinite loop because it can take a long time to count to 2^32 if you have 32 bit ints.

This is why it is often better not to compare to a single value in a for loop but to compare to the end of a range, i.e. rather than i != nsteps use i < nsteps.

Finally I see no construct in your code that could be interpreted as an array of arrays, tsab_array is just an array of double


This calculation

(1-1/i)*sab_array[j] - i*(tab_array[j])

has a slightly odd effect, for any given j if i ranges between 1 and 4 then I think it will issue the values

-tab_array[j]
sab_array[j] - 2*tab_array[j]
sab_array[j] - 3*tab_array[j]
sab_array[j] - 4*tab_array[j]

is this the output you required?
Jul 19 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Leon | last post: by
21 posts views Thread by yeti349 | last post: by
104 posts views Thread by Leszek | last post: by
24 posts views Thread by Michael | last post: by
28 posts views Thread by Nutkin | last post: by
57 posts views Thread by buuuuuum | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.