Thread: algorithm problem

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    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

  2. #2
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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;
    Last edited by andyhunter; 02-10-2005 at 08:52 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  4. #4
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49
    OK, that makes since. I dont know what I was thinking.
    Again everyone thanks for the quick reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Dijkstra algorithm problem.
    By apacz in forum C++ Programming
    Replies: 5
    Last Post: 05-28-2005, 04:16 PM
  3. Simple algorithm problem
    By stodd04 in forum C Programming
    Replies: 11
    Last Post: 03-04-2005, 11:08 AM
  4. Algorithm question
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 10:52 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM