471,047 Members | 1,216 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,047 software developers and data experts.

generating an array of arrays

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>
  10. using namespace std;
  12. int main (){
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.     }
  15.   return 0;
  16. }
Jul 18 '06 #1
2 3810
293 100+
In the second for loop, you should be testing i != nsteps instead of assigning i = nsteps.
Jul 18 '06 #2
9,065 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

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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.