Displaying a number from dynamic array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kornel
    New Member
    • Apr 2007
    • 11

    Displaying a number from dynamic array

    Hi everyone,

    I' ve got a problem. Let's say, I've got a file full of data (numbers) and I want to read them into a dynamic array. After doing it, I want to see, for instance, the third element of the array and the whole amount of numbers in the screen. How could I do that?
    Here's the code
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h> 
    
     
    main ()
    {
    
       int n =0;
       int size = 0;
      
      float *array=new float[3000];
       
       
       
    
       ifstream inf("input.txt",ios::in);
       
       while (inf>>*array)
       {
    	   size++;
    		  
      }
       cout <<size<<endl;    //here I see the total amount of the numbers, it's good
      cout <<array[2]<< endl;   //here I see -4.31602e+008 , but it's not 
                                            // the right  number 
    delete [] array; 
    inf.close();
       
       
       
      
       return 0;
      }
    What should I do?

    Thank you.
    Last edited by AdrianH; Apr 25 '07, 02:10 AM. Reason: Please use [code][/code] tags and format your code!
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    #2
    Have you considered using a vector? It will handle the size and dynamic allocation for you.


    Adrian

    Comment

    • kornel
      New Member
      • Apr 2007
      • 11

      #3
      Originally posted by AdrianH
      Have you considered using a vector? It will handle the size and dynamic allocation for you.


      Adrian
      Thanks man. Now I'm working with vectors.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        The problem in your code is that you are only inputting into *array, or the first element of the array. This first element is overwritten every time there is a number in your file. Now, the third element (array[2]) has never been initialized and still holds garbarge values - like the one you got. You should use size as the index of the array: inf >> array[size++]

        Comment

        • kornel
          New Member
          • Apr 2007
          • 11

          #5
          Originally posted by Ganon11
          The problem in your code is that you are only inputting into *array, or the first element of the array. This first element is overwritten every time there is a number in your file. Now, the third element (array[2]) has never been initialized and still holds garbarge values - like the one you got. You should use size as the index of the array: inf >> array[size++]
          But if I use it, would the array be dynamic? The problem was that simple array was not able to have huge amount of numbers. But now the problem is that I can't access the numbers in dynamic array.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Since you are using a pointer, yes, the array is dynamic, but not in the way you mean. If I understand you correctly, you want the array to correctly size itself to fit all the numbers in the file. To do this, you need a vector. Alternatively, you can open the file, count how many numbers there are in the file, close the file, create an array of that many numbers, re-open the file, and read in the numbers using a for loop.

            Comment

            • kornel
              New Member
              • Apr 2007
              • 11

              #7
              Originally posted by Ganon11
              Since you are using a pointer, yes, the array is dynamic, but not in the way you mean. If I understand you correctly, you want the array to correctly size itself to fit all the numbers in the file. To do this, you need a vector. Alternatively, you can open the file, count how many numbers there are in the file, close the file, create an array of that many numbers, re-open the file, and read in the numbers using a for loop.
              Yes, this is exactly what I want. Simple array can carry "not huge" amount of numbers and that's the problem. As far as I understand, dynamic array is actually only useful for counting the total amount of numbers. Still I also need this feature.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Originally posted by kornel
                Yes, this is exactly what I want. Simple array can carry "not huge" amount of numbers and that's the problem. As far as I understand, dynamic array is actually only useful for counting the total amount of numbers. Still I also need this feature.
                Technically, dynamic arrays will run into the same problem as simple arrays for holding large amounts of numbers.

                Before I go any further, I just want to clarify my thoughts and terms:

                By simple array, I assume you mean something declared without pointers, such as:

                Code:
                int mySimpleArray[20]; // OR
                double myOtherSimpleArray[MAX_SIZE]; // where MAX_SIZE is a constant
                By dynamic array, I mean one using pointers:

                Code:
                int *myDynArray = new int[30]; // OR
                double *myOtherDynArray = new int[x]; // where x can be a constant or variable
                Both of these, when implemented, need a contiguous block of memory to store the numbers. This is where size issues come into play. When you need a simple or dynamic array of, say, 16000 integers, that's a lot of memory (Usually 32000 or 64000 bytes, depending on how your compiler stores an int). And because this all has to be in a continuous block, finding the available memory space is difficult, if not impossible. This is true for both dynamic and simple arrays.

                To get around this, you can use a vector of pointers. The vector itself will require a contiguous block of memory, but it will need that block for the pointers only. The pointers themselves will be pointing to different locations in memory; no contiguous block needed.

                You can also use a double pointer, like int **myBetterDynAr ray. Initialize it to a dynamic array of int* pointers with size equal to however many integers you need. Each of these int*s will be set to a new int; not an array! Like the vector idea, the first pointer will need a contiguous block of memory to store the int*s, but the int*s will be pointing to integers all over the place.

                Comment

                • kornel
                  New Member
                  • Apr 2007
                  • 11

                  #9
                  Originally posted by Ganon11
                  Technically, dynamic arrays will run into the same problem as simple arrays for holding large amounts of numbers.

                  Before I go any further, I just want to clarify my thoughts and terms:

                  By simple array, I assume you mean something declared without pointers, such as:

                  Code:
                  int mySimpleArray[20]; // OR
                  double myOtherSimpleArray[MAX_SIZE]; // where MAX_SIZE is a constant
                  By dynamic array, I mean one using pointers:

                  Code:
                  int *myDynArray = new int[30]; // OR
                  double *myOtherDynArray = new int[x]; // where x can be a constant or variable
                  Both of these, when implemented, need a contiguous block of memory to store the numbers. This is where size issues come into play. When you need a simple or dynamic array of, say, 16000 integers, that's a lot of memory (Usually 32000 or 64000 bytes, depending on how your compiler stores an int). And because this all has to be in a continuous block, finding the available memory space is difficult, if not impossible. This is true for both dynamic and simple arrays.

                  To get around this, you can use a vector of pointers. The vector itself will require a contiguous block of memory, but it will need that block for the pointers only. The pointers themselves will be pointing to different locations in memory; no contiguous block needed.

                  You can also use a double pointer, like int **myBetterDynAr ray. Initialize it to a dynamic array of int* pointers with size equal to however many integers you need. Each of these int*s will be set to a new int; not an array! Like the vector idea, the first pointer will need a contiguous block of memory to store the int*s, but the int*s will be pointing to integers all over the place.
                  OK, I got it. In my case it is important the place of number in the file (actually it is the matrix [n X 8]). As I've tried, simple 2D array can only handle with approximately 256000 numbers. The idea of working with 1D vector is good, because this vector can handle 8 times greater (or much more) amount of numbers. But 1D vector doesn't work with data as a matrix and in my case it is an important drawback.
                  Would it be possible to work with 2D dynamic array? I mean is it possible to do the whole stuff as in simple 2D array case (cycles if, searching elements, comparing them, dispalying in the screen, etc.).
                  I am thinking of the idea of using 2D vector, but I find really not much information about it.

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by kornel
                    OK, I got it. In my case it is important the place of number in the file (actually it is the matrix [n X 8]). As I've tried, simple 2D array can only handle with approximately 256000 numbers. The idea of working with 1D vector is good, because this vector can handle 8 times greater (or much more) amount of numbers. But 1D vector doesn't work with data as a matrix and in my case it is an important drawback.
                    Would it be possible to work with 2D dynamic array? I mean is it possible to do the whole stuff as in simple 2D array case (cycles if, searching elements, comparing them, dispalying in the screen, etc.).
                    I am thinking of the idea of using 2D vector, but I find really not much information about it.
                    Well, if one of your indicies are fixed, you could make an array of vectors. This will act like a 2D array. If both are not fixed, then make a vector of vectors.


                    Adrian

                    Comment

                    • kornel
                      New Member
                      • Apr 2007
                      • 11

                      #11
                      Thank you for all the information. I appreciate it.

                      Comment

                      • AdrianH
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1251

                        #12
                        Originally posted by kornel
                        Thank you for all the information. I appreciate it.
                        No problem.


                        Adrian

                        Comment

                        • kornel
                          New Member
                          • Apr 2007
                          • 11

                          #13
                          You know, now I have two versions of the code - and they both work. 2D dynamic array and 2D vector. I don't know why, but 2D vector is much slower than 2D array. For example, the 1.600.000 floats take the vector for about 6 hours of working, while array is 10 times faster. I am not an experienced programmer, so maybe there is a way of optimizing the whole stuff.
                          Anyway, I am really happy I finally wrote the code.

                          :)

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            Since a vector is a class, there is probably a lot of work going on 'behind the scenes', especially when calling the push_back() method. Using native arrays is faster because there's no added functionality. If efficiency is your number 1 concern, the array solution is probably your best bet, but if flexibility is more important, stick with the vectors.

                            Comment

                            • AdrianH
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1251

                              #15
                              Originally posted by kornel
                              You know, now I have two versions of the code - and they both work. 2D dynamic array and 2D vector. I don't know why, but 2D vector is much slower than 2D array. For example, the 1.600.000 floats take the vector for about 6 hours of working, while array is 10 times faster. I am not an experienced programmer, so maybe there is a way of optimizing the whole stuff.
                              Anyway, I am really happy I finally wrote the code.

                              :)
                              Vectors do dynamic allocation. When the vector becomes too small to fit another element, it will allocate new memory, copy all the elements from the old array to the new array and then destroy the old array. It is this overhead that is causing the slowdown you are referring to.

                              To keep this from happening to a minimum, pre-allocate the memory used by the vector to something that you think is going to be reasonable upper limit. This is done by calling the vector::reserve () function.

                              If you surpass your defined pre-allocation limit, it will reallocate as I previously described. How many extra elements it will reallocate is implementation defined, but in a simple implementation it may only reallocate only that which is needed. So you push_back() one element past the currently allocated limit, it will probably only reallocate one. Do that a million and a half times and you can see the bottleneck stand out.

                              If you cannot tell how many elements you will have, but you do have an idea of possible growth and you do not want to depend on an efficient implementation of the vector class, check vector::capacit y() against vector::size() to see if you have enough room to push_back another element. If you do not have space, do a vector::resize( ) to a number large enough to hold the current size() plus 5, 10, 100 or more as you see fit, so that the vector doesn't reallocate too many times.

                              IIRC though, if you feel you have over allocated and want to reclaim the allocated memory, you cannot do it with vector::resize( ). To 'shrink wrap' the allocated memory to current contents of the vector , you will have to do a vector::swap() with another vector that has a current capacity less than or equal to the one you want. This again is an expensive task as the programme must copy all the data from one vector to the other.

                              The STL library is very powerful, and has many containers used to optimise many operations, but may not be optimal for other operations. It is up to you to know what you need and use the container best suited for the task.

                              Enjoy,


                              Adrian

                              Comment

                              Working...