An unusual way to declare an array?
-
Hello everyone Traditionally, unidymensional arrays are declared by providing the size of the array with a constant number as argument (e.g.
myarray[size]
where size is usually aconst int
). I'm working building a model that needs arrays with one value per year (e.g. 6789 in 1989, 3992 in 1990 and so forth). In other words, I need to access values in the array when I have the year of interest as the index to identify the elements of the array. How can I declare the array asmyarray(firstyear,lastyear)
so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.Can u explain with some detail example i cant get it :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com
-
Can u explain with some detail example i cant get it :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com
Hi Consider an array that could be indexed as:
int firstyear = 1980; int lastyear = 1985; /* where both firstyear and lastyear are not constants*/ int myarray(fistyear,lastyear) /* I know that this is wrong in the regular way to declare an array but this is what I'm looking for. So that I can access it in any of the following ways */ myarray[1981] = 6789; myarray[1983] = 3482;
// or work with a loop such asfor(int i = firstyear; i <= lastyear; i++) { myarray[i]; }
This contrasts with the usual:const int size = 6; int myarray[6]; myarray[1] = 6789; myarray[3] = 3842; // or for(int i = 0; i < size; i++) { myarray[i]; }
Hope this makes it clearer. Thanks for your help C. -
Can u explain with some detail example i cant get it :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com
-
Hi Consider an array that could be indexed as:
int firstyear = 1980; int lastyear = 1985; /* where both firstyear and lastyear are not constants*/ int myarray(fistyear,lastyear) /* I know that this is wrong in the regular way to declare an array but this is what I'm looking for. So that I can access it in any of the following ways */ myarray[1981] = 6789; myarray[1983] = 3482;
// or work with a loop such asfor(int i = firstyear; i <= lastyear; i++) { myarray[i]; }
This contrasts with the usual:const int size = 6; int myarray[6]; myarray[1] = 6789; myarray[3] = 3842; // or for(int i = 0; i < size; i++) { myarray[i]; }
Hope this makes it clearer. Thanks for your help C.You could just do it like:
int i; for (i = 1980; i < 1985; i++) { array[i-1980] = 6370; //assigns 6370 to all elements. }
Just subtract the firstyear from the index, and you are there.i = 1981; array[i-1980] = 200;//puts value at index 1 i = 1985; array[i-1980] = 220;//puts value at index 5
this is this. -
Hi Consider an array that could be indexed as:
int firstyear = 1980; int lastyear = 1985; /* where both firstyear and lastyear are not constants*/ int myarray(fistyear,lastyear) /* I know that this is wrong in the regular way to declare an array but this is what I'm looking for. So that I can access it in any of the following ways */ myarray[1981] = 6789; myarray[1983] = 3482;
// or work with a loop such asfor(int i = firstyear; i <= lastyear; i++) { myarray[i]; }
This contrasts with the usual:const int size = 6; int myarray[6]; myarray[1] = 6789; myarray[3] = 3842; // or for(int i = 0; i < size; i++) { myarray[i]; }
Hope this makes it clearer. Thanks for your help C. -
You could just do it like:
int i; for (i = 1980; i < 1985; i++) { array[i-1980] = 6370; //assigns 6370 to all elements. }
Just subtract the firstyear from the index, and you are there.i = 1981; array[i-1980] = 200;//puts value at index 1 i = 1985; array[i-1980] = 220;//puts value at index 5
this is this. -
Hi Consider an array that could be indexed as:
int firstyear = 1980; int lastyear = 1985; /* where both firstyear and lastyear are not constants*/ int myarray(fistyear,lastyear) /* I know that this is wrong in the regular way to declare an array but this is what I'm looking for. So that I can access it in any of the following ways */ myarray[1981] = 6789; myarray[1983] = 3482;
// or work with a loop such asfor(int i = firstyear; i <= lastyear; i++) { myarray[i]; }
This contrasts with the usual:const int size = 6; int myarray[6]; myarray[1] = 6789; myarray[3] = 3842; // or for(int i = 0; i < size; i++) { myarray[i]; }
Hope this makes it clearer. Thanks for your help C.Have you ever considered using the STL? And STL map would do it:
// define the container -- key is int, value is int: std::map<int,int> myArray; // you can make arbitrary assignments: myArray[1981] = 6789; myArray[1983] = 3482; // or work in a loop, such as: for ( std::map<int,int>::iterator iter = myArray.begin(); iter != myArray.end(); ++iter ) { int key = (*iter).first; int value = (*iter).second; printf( "myArray[%d] = %d\n"), key, value ); }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc.](http://www.soonr.com)
-- modified at 3:36 Thursday 8th December, 2005 -
Hello everyone Traditionally, unidymensional arrays are declared by providing the size of the array with a constant number as argument (e.g.
myarray[size]
where size is usually aconst int
). I'm working building a model that needs arrays with one value per year (e.g. 6789 in 1989, 3992 in 1990 and so forth). In other words, I need to access values in the array when I have the year of interest as the index to identify the elements of the array. How can I declare the array asmyarray(firstyear,lastyear)
so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.as i understand it, you have several arrays, one per year, and want to find them by the year number ; for this, the standard library implements the
std::map<>
class. as I imagine, the arrays may not have the same size each, so you could be using such a map :std::map< int, std::vector< int > > mapYearWithVector;
is this true ?
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20] | soon : [VisualCalc 3.0] -
Hello everyone Traditionally, unidymensional arrays are declared by providing the size of the array with a constant number as argument (e.g.
myarray[size]
where size is usually aconst int
). I'm working building a model that needs arrays with one value per year (e.g. 6789 in 1989, 3992 in 1990 and so forth). In other words, I need to access values in the array when I have the year of interest as the index to identify the elements of the array. How can I declare the array asmyarray(firstyear,lastyear)
so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.I'm sure that this cannot be done using conventional arrays. The way I would do it would be to use a linked list. That way, the years could be in any order, and years could be missed out if required. class node { private : int year; int value; node * pointToPrevious; public : node() { } node(int y, int v, node * p) { year = y; value = v; pointToPrevious = p; } }; For fuller notes on linked lists as I teach it, please email me : simon.cornish@tesco.net Regards Simon