incrementing arrays

Oct 10, 2008 at 4:31am
Hi, currently writing my 4th console app and was wondering if it were possible to increment an array var? The general idea is that the app runs a function and stores the result in an array, increments a var in the function then increments the array var so that on the next parse it stores the value in the next value in the array. My programming language is terrible so i hope this wasn't too confusing :).
Oct 10, 2008 at 4:35am
Use a for loop.
Last edited on Oct 10, 2008 at 4:35am
Oct 10, 2008 at 5:14am
already doing that... here's what I've setup so far (beware my coding style is not pretty, you will probably spot a milion loose ends and errors, most can be ignored as they will be tied up later)

so here's a cleaned up version...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//idea is to setup an array that stores each value of a function after one of the terms is incremented, then performs a function to find
//the difference between each of the stored values, the difference of the resulting values and so on. these are outputted into a data tree structure
#include <cmath>
#include <iostream>
using namespace std;

double TreeBase[];
double Resolution = 0;
int Size;
double BaseResult;

int main()
{
	cout << "input interpolation resolution (must be less than or equal to 1 and above 0) \n";
	cin >> Resolution;
	cout <<"input interpolation sample size integer \n";
	cin >> Size;
	if( Resolution > 1 || Resolution <= 0) 
	{
		return 0;
	}
	for( int n = 0; n <= Size; n++ )
	{
	BaseResult = pow( 2, Resolution);
	Resolution += Resolution;
	BaseResult = TreeBase[n];
	}
}
Last edited on Oct 10, 2008 at 7:48am
Oct 10, 2008 at 10:32am
Your array size must be defined when it's declared, if you don't know the size from the start there are 2 options:
1) You create it after you've gotten the value of size
or
2) You use a vector: http://www.cplusplus.com/reference/stl/vector/
Oct 10, 2008 at 10:46pm
So I've started using vectors like suggested (what is the benefit of an array over a vector?), here's what I've got
edit: I've made it simpler so here's the new code...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//idea is to setup an array that stores each value of a function after one of the terms is incremented, then performs a function to find
//the difference between each of the stored values, the difference of the resulting values and so on. these are outputted into a data tree structure
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

double Resolution;
double BaseResult;
vector<double> TreeBase (32);

int main()
{
	cout << "input interpolation resolution (must be less than or equal to 1 and above 0) \n";
	cin >> Resolution;
	if( Resolution > 1 || Resolution <= 0) 
	{
		return 0;
	}

	for( int n = 0; n <= 32; n++ )
	{
	BaseResult = pow( 2, Resolution);
	Resolution += Resolution;
	BaseResult = TreeBase[n];
	}
}


this is crashing my app witht the following error,
'Microsoft Visual Studio C Runtime Library has detected a fatal error in Resonance Interpolation Simulator.exe.

Press Break to debug the program or Continue to terminate the program.'

edit: I've pressed Break and see that TreeBase is returning the right amount of vectors but they're all of a value 0... any help here?




Last edited on Oct 11, 2008 at 12:40am
Oct 11, 2008 at 3:22am
doesn't matter, simple problem to fix, I was asking it to loop 33 times and store only 32 vectors...
Last edited on Oct 11, 2008 at 3:49am
Topic archived. No new replies allowed.