# algorithm problem

• 02-10-2005
cdonlan
algorithm problem
Hey guys Im having a probelm with my algorithm . Im trying to copy data from a asci ppm file(which is just a text file really) into a 2-d array of structs. The order it should go in is plot[1][1],plot[1][2].....plot[2][1]..and so on.
Here is the code
Code:

```//the class class graphics { public:     void loadPPM(char* filename);     graphics(void);     ~graphics(void); private:     int xsize;     int ysize;     struct RGB     {         int red;         int green;         int blue;     }plot[100][100]; };```
Code:

```//the algorithm for(i=1;i<=xsize;i++)     {                for(j=1;j<=ysize;j++)         {             for(k=1;k<=3;k++)             {                 inFile>>plot[i][j].red;                 inFile>>plot[i][j].green;                 inFile>>plot[i][j].blue;             }         }     }     cout<<filename<<endl;        cout<<plot[1][1].red<<endl;     cout<<plot[1][1].green<<endl;     cout<<plot[1][1].blue<<endl;```
Code:

```.... //the text file im reading 255 255 254 251 255 255 //there are over 4400 lines like but its not relevent```
Im not getting the currect output when I run the program. My results are 251,255,254. It should be 255,255,254. I think I have a problem with the nested for loops. If you would to see more code. Like the function call, I will post it.

Thanks
Chris
• 02-10-2005
andyhunter
Code:

``` for(k=1;k<=3;k++)             {                 inFile>>plot[i][j].red;                 inFile>>plot[i][j].green;                 inFile>>plot[i][j].blue;             }```
You are reading in information into the same array location 3 times. Is this intended? aka the code is equivalent to:
Code:

```inFile>>plot[1][1].red; //k = 1                 inFile>>plot[1][1].green;                 inFile>>plot[1][1].blue; inFile>>plot[1][1].red; //k = 2                 inFile>>plot[1][1].green;                 inFile>>plot[1][1].blue; inFile>>plot[1][1].red; // k = 3                 inFile>>plot[1][1].green;                 inFile>>plot[1][1].blue;```
• 02-10-2005
swoopy
Code:

```            for(k=1;k<=3;k++)             {                 inFile>>plot[i][j].red;                 inFile>>plot[i][j].green;                 inFile>>plot[i][j].blue;             }```
As Andy suggested, you don't need this third loop, just:
Code:

```                inFile>>plot[i][j].red;                 inFile>>plot[i][j].green;                 inFile>>plot[i][j].blue;```
Also remember arrays start at index 0, which is no problem, you are just skipping the first element. Just make sure your array is one element bigger.
• 02-10-2005
cdonlan
OK, that makes since. I dont know what I was thinking.
Again everyone thanks for the quick reply