473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Displaying a number from dynamic array

11
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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <fstream.h>
  3. #include <stdlib.h> 
  4.  
  5.  
  6. main ()
  7. {
  8.  
  9.    int n =0;
  10.    int size = 0;
  11.  
  12.   float *array=new float[3000];
  13.  
  14.  
  15.  
  16.  
  17.    ifstream inf("input.txt",ios::in);
  18.  
  19.    while (inf>>*array)
  20.    {
  21.        size++;
  22.  
  23.   }
  24.    cout <<size<<endl;    //here I see the total amount of the numbers, it's good
  25.   cout <<array[2]<< endl;   //here I see -4.31602e+008 , but it's not 
  26.                                         // the right  number 
  27. delete [] array; 
  28. inf.close();
  29.  
  30.  
  31.  
  32.  
  33.    return 0;
  34.   }
  35.  
What should I do?

Thank you.
Apr 24 '07 #1
14 2503
AdrianH
1,251 Expert 1GB
Have you considered using a vector? It will handle the size and dynamic allocation for you.


Adrian
Apr 25 '07 #2
kornel
11
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.
Apr 25 '07 #3
Ganon11
3,652 Expert 2GB
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++]
Apr 25 '07 #4
kornel
11
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.
Apr 25 '07 #5
Ganon11
3,652 Expert 2GB
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.
Apr 25 '07 #6
kornel
11
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.
Apr 25 '07 #7
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. int mySimpleArray[20]; // OR
  2. double myOtherSimpleArray[MAX_SIZE]; // where MAX_SIZE is a constant
By dynamic array, I mean one using pointers:

Expand|Select|Wrap|Line Numbers
  1. int *myDynArray = new int[30]; // OR
  2. 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 **myBetterDynArray. 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.
Apr 25 '07 #8
kornel
11
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:

Expand|Select|Wrap|Line Numbers
  1. int mySimpleArray[20]; // OR
  2. double myOtherSimpleArray[MAX_SIZE]; // where MAX_SIZE is a constant
By dynamic array, I mean one using pointers:

Expand|Select|Wrap|Line Numbers
  1. int *myDynArray = new int[30]; // OR
  2. 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 **myBetterDynArray. 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.
Apr 26 '07 #9
AdrianH
1,251 Expert 1GB
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
Apr 26 '07 #10
kornel
11
Thank you for all the information. I appreciate it.
Apr 29 '07 #11
AdrianH
1,251 Expert 1GB
Thank you for all the information. I appreciate it.
No problem.


Adrian
Apr 29 '07 #12
kornel
11
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.

:)
Apr 30 '07 #13
Ganon11
3,652 Expert 2GB
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.
Apr 30 '07 #14
AdrianH
1,251 Expert 1GB
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::capacity() 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
May 1 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Lyn | last post by:
I need some guidance for a technique that will allow me to accomplish the following... I have a table in which each record contains a photograph. I would like to display in a form a thumbnail...
18
by: Vasilis Serghi | last post by:
Presently I define the number of lines to be expected in a file when defining the array size and the initialisation of this array. This works fine for now, but i'm sure that in the future this...
3
by: Brian Piotrowski | last post by:
Hi All, I've probably done this before, but for the life of me I can't remember how I did it. I need to move values from a DB table into an array to be used for other queries. The number of...
13
by: hornedw | last post by:
I have been working on a ecommerce website for myself. What I needed some assistance on was when i was trying to display the categories/subcategories for the different products. I decided to use...
1
by: mandakini | last post by:
Hello freinds I am working on this url http://72.36.156.243/compbuild.php Here I am using ifram and displaying dynamic value I don't know how to use iframe as array how to assign array and...
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
1
by: littlealex | last post by:
IE6 not displaying text correctly - IE 7 & Firefox 3 are fine! Need some help with this as fairly new to CSS! In IE6 the text for the following page doesn't display properly - rather than being...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.