Computing.Net > Forums > Programming > array manipulation

array manipulation

Reply to Message Icon

Original Message
Name: enogogo
Date: January 23, 2004 at 07:12:20 Pacific
Subject: array manipulation
OS: unix
CPU/Ram: 2
Comment:

i need a c++ function such that:

i have an array of integers
eno[] =[1,3,0,4,5,0,0,8,3,4,0] something and i want to pull out the zeros and set up an other array
eno2[]=[1,3,4,5,8,4], can anyone help me?


Report Offensive Message For Removal


Response Number 1
Name: enogogo
Date: January 23, 2004 at 07:19:23 Pacific
Reply: (edit)

correction: eno2[]=[1,3,4,5,8,3,4]


Report Offensive Follow Up For Removal

Response Number 2
Name: gpp
Date: January 23, 2004 at 08:02:35 Pacific
Reply: (edit)

This shouldnt be too hard.. You just need to loop through the array(eno[]) and every time you find a non-zero, add it to the end of eno2[]. eno2[] should probably be dynamic so you dont waste a lot of memory.. only problem is I'm not sure how to resize the array in C++. I used C back when I wrote this, and had to use realloc to change the size of the array... guess you'll have to do some searching.


Report Offensive Follow Up For Removal

Response Number 3
Name: Infinite Recursion
Date: January 23, 2004 at 10:40:14 Pacific
Reply: (edit)

GPP's method is probably how you need to approach this problem. Take the initial array, loop through it and only extract the ones you need and assign them into another array.

If you really want "ease" then, use STL (SGI's standard template library). I believe the VECTOR will allow you to do just this, and the size does not need to be defined (if it attempts to go out of bounds, it doubles in size).

If you don't want to use STL... search google for 'dynamic arrays C++' and you should find what you need.

IR


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: array manipulation

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge