Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. An unusual way to declare an array?

An unusual way to declare an array?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasedata-structures
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    knapak
    wrote on last edited by
    #1

    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 a const 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 as myarray(firstyear,lastyear) so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.

    V T S 3 Replies Last reply
    0
    • K knapak

      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 a const 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 as myarray(firstyear,lastyear) so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.

      V Offline
      V Offline
      vikas amin
      wrote on last edited by
      #2

      Can u explain with some detail example i cant get it :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

      K 2 Replies Last reply
      0
      • V vikas amin

        Can u explain with some detail example i cant get it :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

        K Offline
        K Offline
        knapak
        wrote on last edited by
        #3

        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 as for(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.

        K T P 3 Replies Last reply
        0
        • V vikas amin

          Can u explain with some detail example i cant get it :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

          K Offline
          K Offline
          knapak
          wrote on last edited by
          #4

          I meant to say: myarray[size]

          1 Reply Last reply
          0
          • K knapak

            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 as for(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.

            K Offline
            K Offline
            khan
            wrote on last edited by
            #5

            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.

            K 1 Reply Last reply
            0
            • K knapak

              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 as for(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.

              T Offline
              T Offline
              tranglt
              wrote on last edited by
              #6

              I thing you can use map to implement your function! have a lucky day.

              K 1 Reply Last reply
              0
              • K khan

                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.

                K Offline
                K Offline
                knapak
                wrote on last edited by
                #7

                Thank you, this certainly allows me to do what I need... although I was expecting a new way to declare the array instead of a clever way to work with what we already have... but hey, it works for me! Cheers

                1 Reply Last reply
                0
                • T tranglt

                  I thing you can use map to implement your function! have a lucky day.

                  K Offline
                  K Offline
                  knapak
                  wrote on last edited by
                  #8

                  Thanks for your suggestion. I was trying to avoid using containers. However, Mr. khan++ gave me an idea that solves my problem. Cheers

                  1 Reply Last reply
                  0
                  • K knapak

                    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 as for(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.

                    P Offline
                    P Offline
                    Peter Weyzen
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • K knapak

                      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 a const 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 as myarray(firstyear,lastyear) so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      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]

                      1 Reply Last reply
                      0
                      • K knapak

                        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 a const 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 as myarray(firstyear,lastyear) so I can access its elements the way I need and both firstyear and lastyear are read from a file? Thanks C.

                        S Offline
                        S Offline
                        Simon Cornish
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups