# efficient array

• 08-06-2003
keithmolo
efficient array
hi, i have created an array, but i am still using it inefficiently. can anyone help me make this code more efficient?

Code:

```        fIn2.get(xCh[0]);         fIn2.get(xCh[1]);         fIn2.get(xCh[2]);         fIn2.get(xCh[3]);         fout.put(xCh[1]);         fout.put(xCh[0]);         fout.put(xCh[3]);         fout.put(xCh[2]);```
I am basically reading in four characters at a time and writing them out in different order. I know there's gotta be a way to make this shorter, but my brain is frozen right now.

thanks,
keith
• 08-06-2003
FillYourBrain
Re: efficient array
Code:

```        fIn2.get(xCh[1]);         fIn2.get(xCh[0]);         fIn2.get(xCh[3]);         fIn2.get(xCh[2]);         fout.write(xCh,4);```
• 08-06-2003
confuted
Just posting this to demonstrate using a for loop to access an array (you'll do this lots in the future)
Code:

```for(int i=0; i<4;i ++) {         fIn2.get(xCh[i]); }         fout.put(xCh[1]);         fout.put(xCh[0]);         fout.put(xCh[3]);         fout.put(xCh[2]);```
• 08-06-2003
FillYourBrain
I would definitely rather never use put or get. You make WAAAY more function calls that way and a single disk access is much better than 100 disk accesses. Disk access can be one of the slowest things you can do. Now, I will admit that fstream does buffering so it isn't as bad as I'm making it, however you still make more function calls that way. So an even better way to do this would be to read the entire buffer in and write the entire buffer out. Like so:

Code:

```          char buf[4];           fIn2.read(buf,4);           xCh[0] = buf[1];           xCh[1] = buf[0];           xCh[2] = buf[3];           xCh[3] = buf[2];           fout.write(xCh,4);```