dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: for() statement stops at 3rd interation

  1. #16
    Join Date
    Apr 1999
    Posts
    27,449

    Re: for() statement stops at 3rd interation

    Quote Originally Posted by hixidom View Post
    I tried using the debugging tool on DevC++, but it didn't get me very far since I didn't really know what I was doing.
    It is mandatory that you learn to use debuggers. What if the program were larger? You can't write all of this code, and not have the proper debugging tools to fix problems.

    If any of us were to try to solve your problem, we would be using the debugger that comes with the compiler. No one, unless they are on the savant level, could just look at your code and tell you what the problem is without debugging it.
    I still havn't implemented vectors, but only because I don't know anything about how to use them.
    Every good C++ book has chapeters on usage of std::vector. At the very least, you could have replaced those char arrays with std::string.
    I'm fairly satisfied with the program as is. I know that those of you who are much more experienced in programming would strongly disagree, but I feel as though the program is almost finished.
    Here is where you can get into a problem -- the people that are experienced in C++ are used to helping programmers who have written programs that have good structure, form, and is maintainable, but has a few errors here and there. Asking us to look at your code as it stands now is equivalent to building a house without a proper foundation, and then asking an architect that wasn't involved in the initial house-building to design the roof. There is no way an architect can design a roof if they know that the foundation is faulty.

    You made very little changes to what was pointed out before -- potential of buffer overruns, parameters that really don't do what you think they do like this:
    Code:
    void cleartemp(char temp[20]) //used to clear char array[20]
    whether your files are binary (i.e. can contain embedded nulls and control characters) and other issues.
    My main concern is the apparent limitation to image size, but I have no idea why this is occurring.
    And we won't know either, unless you attach the actual file you are trying to process that isn't working.

    Regards,

    Paul McKenzie

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: for() statement stops at 3rd interation

    I think the single change which would benefit you the most right now is learning more concise methods of string manipulation. This:
    Code:
                                char frame[17]={"PLCS1-1F"};
                                if(x<100)strcat(frame,"0");
                                if(x<10)strcat(frame,"00");
                                itoa(x+1,temp,10);
                                strcat(frame,temp);
                                cleartemp(temp);
                                strcat(frame,".ppm");
    is equivalent to
    Code:
                                char frame[17];
                                sprintf(frame, "PLCS1-1F&#37;03d.ppm", x+1);
    See http://www.cplusplus.com/reference/c...stdio/sprintf/ for details. (You could also use a stringstream with a std::string, but that isn't strictly necessary here if the value of x is always < 1000.)

  3. #18
    Join Date
    Jul 2007
    Posts
    33

    Re: for() statement stops at 3rd interation

    Thanks guys.
    You've gotten me really excited about the prospect of picking up where I left off in learning C++. Before I post my next reply, I'll have learned and implemented all of the aspects of C++ that have been recommended to me in this thread. That will take at least a few days. In the meantime, thank you very much for all of your help. I'll get back to you all as soon as possible.
    Andy

  4. #19
    Join Date
    Apr 1999
    Posts
    27,449

    Re: for() statement stops at 3rd interation

    Quote Originally Posted by hixidom View Post
    Thanks guys.
    You've gotten me really excited about the prospect of picking up where I left off in learning C++.
    That's good to here. If you learn it properly, you will see that code as you've written can be much easier to maintain.

    You can start here with the C++ FAQ:

    www.parashift.com

    Regards,

    Paul McKenzie

  5. #20
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: for() statement stops at 3rd interation

    Quote Originally Posted by hixidom View Post
    First of all, thank you Memeloo for replying just to let me know that you're too much of an asshole to help me. That's good to know.
    at this point I also have to say thank you, I've learnt that I won't do the same mistake again and replay to someone who does not respect others and pastes such a chaos and expects that I spend my time on it.
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  6. #21
    Join Date
    Jul 2007
    Posts
    33

    Re: for() statement stops at 3rd interation

    Ok, this is what I've got so far. I'd like to point out that there are roughly half as many lines in this version of the program as there were in the original (136 vs. 267). I couldn't have made so many changes without everyones help. Thanks everybody. Honestly, I don't like the effect that the program is producing, but that's just something I'll have to fiddle with until it looks good enough.
    Is this satisfying, or are there still changes to be made?

    Code:
    #include<iostream>
    #include<math.h>
    #include<fstream>
    #include<stdlib.h>
    #include<cstring>
    #include<vector>
    #define pi 3.14159265
    using namespace std;
    
    void cpt(int n)
    {
         cout<<" Checkpoint "<<n<<" ";
         system("PAUSE");
    }
    
    int pw,ph,res;
    vector<int> a;
    vector<int> b;
    
    double anglecalc(double current, double v[2]) //converts 2D flow vector to angle in radians
    {
           if(v[0]==0 && v[1]>0)return (pi/2);
           else if(v[0]==0 && v[1]<0)return (-1*pi/2);
           else if(v[0]>0)return atan(v[1]/v[0]);
           else if(v[0]<0)return pi+atan(v[1]/v[0]);
           else return 0;
    }
           
    double magcalc(double v[2]) //finds the magnitude of flow vector at current pixel
    {
           return sqrt(pow(v[0],2)+pow(v[1],2));
    }
    
    double precset(double num, int dec) //rounds num down such that it only has dec decimal places
    {
           return floor(num*pow(10,dec))/pow(10,dec);
    }
    
    void processpix()
    {
         double theta,f[2];
         int current,i,adj[2][2],v[2]; //adj[] represents offset locations of two adjascent pixels in the direction of f(t)
         for(current=0;current<pw*ph;current++)
         {
             //set x and y components based on current pixel
             v[1]=(int)((current-fmod(current,pw))/pw);
             v[0]=(int)fmod(current,pw);
             //define flow functions f(x) and f(y)
             f[0]=.707;
             f[1]=.707;
            theta=anglecalc(current,f);
            if(theta<0)theta=theta+(2*pi); //rules out negative angles
            //determine adjascent pixels to be considered
            adj[0][0]=pw+(int)(v[0]+sqrt(pow(cos(theta),2))/cos(theta)-(((int)(theta/(pi/2))&#37;2)*sqrt(pow(cos(theta),2))*(((int)(theta/(pi/4))+1)%2)/cos(theta)));
            adj[0][1]=ph+(int)(v[1]+sqrt(pow(sin(theta),2))/sin(theta)-((((int)(theta/(pi/2))+1)%2)*sqrt(pow(sin(theta),2))*(((int)(theta/(pi/4))+1)%2)/sin(theta)));
            adj[1][0]=pw+(int)(v[0]+sqrt(pow(cos(theta+(pi/4)),2))/cos(theta+(pi/4))-(((int)(theta/(pi/2))%2)*sqrt(pow(cos(theta+(pi/4)),2))*(((int)((theta+(pi/4))/(pi/4))+1)%2)/cos(theta+(pi/4))));
            adj[1][1]=ph+(int)(v[1]+sqrt(pow(sin(theta+(pi/4)),2))/sin(theta+(pi/4))-((((int)(theta/(pi/2))+1)%2)*sqrt(pow(sin(theta+(pi/4)),2))*(((int)((theta+(pi/4))/(pi/4))+1)%2)/sin(theta+(pi/4))));
            //apply modular algorithm to pixels outside the range of the image
            while(adj[0][0]>pw-1)adj[0][0]=adj[0][0]-pw;
            while(adj[0][1]>ph-1)adj[0][1]=adj[0][1]-ph;//this part might have a few bugs
            while(adj[1][0]>pw-1)adj[1][0]=adj[1][0]-pw;
            while(adj[1][1]>ph-1)adj[1][1]=adj[1][1]-ph;
            //assign the location of a given pixel
            v[0]=(adj[0][1]*pw)+adj[0][0];
            v[1]=(adj[1][1]*pw)+adj[1][0];
            for(i=0;i<3;i++)
            {
                            b.push_back((a[(current*3)+i]+((precset(fmod(theta,pi/4)/(pi/4),2)*a[(v[0]*3)+i])+((1-precset(fmod(theta,pi/4)/(pi/4),2))*a[(v[1]*3)+i]))*magcalc(f))/magcalc(f)); //defines b[current] as a weighted combination of a[current] and its adjascent pixels
            }
         }
    }
    
    void cleartemp(char temp[20]) //used to clear char array[20]
    {
         int a;
         for(a=0; a<20; a++)temp[a]='\0';
    }
    
    void getinitial()
    {
         char temp[20],file[15];
        int i,j,tempc;
        cout<<"File to upload:  LCinit#.ppm\n# = "; //file to be used must be of the given format, where # is less than 3 digits
        cin>>i;
        //assemble the char array used to access file
        sprintf(file,"LCinit%d.ppm",i);
        ifstream lcinit(file, ios::binary);
        lcinit.ignore(100,'5');
        lcinit.seekg(+1,ios::cur);
        cleartemp(temp);
        //lcinit.seekg(0,ios::cur);
        lcinit>>temp;
        pw=atoi(temp);
        cleartemp(temp);
        lcinit>>temp;
        ph=atoi(temp);
        cleartemp(temp);
        lcinit>>temp;
        res=atoi(temp);
        //download pixel color values into a
        for(i=0; i<pw*ph; i++)
        {
                            for(j=0;j<3;j++)
                            {
                                             lcinit>>tempc;
                                             a.push_back(tempc);
                            }
                             
        }
    }             
    
    int main()
    {
        int x,i,j,framequant;
        cout<<"Number of Frames:  ";
        cin>>framequant;
        getinitial();
        for(x=0;x<framequant;x++)
        {
                                //name the given frame accordingly
                                char frame[17];
                                sprintf(frame,"PLCS1-3%03d.ppm",x+1);
                                ofstream ofile (frame, ios::trunc | ios::ate);
                                ofile<<"P3# "<<pw<<" "<<ph<<" "<<res<<" \n"; //add necessary file parameters
                                processpix();
                                for(i=0;i<pw*ph*3;i++)
                                {
                                                    ofile<<b[i]<<" ";
                                }
                                ofile.close();
                                a.swap(b);
                                b.erase(b.begin(),b.end());
        }
        return 1;
    }
    Last edited by hixidom; July 13th, 2009 at 08:28 PM.

  7. #22
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: for() statement stops at 3rd interation

    I still think your tendency toward massive, incomprehensible equations on a single line is a habit that should be broken, but other than than I don't see anything obviously improvable. Little things here and there, such as:
    Code:
                                b.erase(b.begin(),b.end());
    // equivalent to
                                b.clear();

  8. #23
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,555

    Re: for() statement stops at 3rd interation

    Your code is still difficult to read, and you'll find difficult to debug. Two things that will really help are white space and one statement per line.

    Instead of
    Code:
           while(adj[0][0]>pw-1)adj[0][0]=adj[0][0]-pw;
    
    or 
           if(v[0]==0 && v[1]>0)return (pi/2);
    try
    Code:
    while(adj[0][0] > pw - 1)
        adj[0][0] = adj[0][0] - pw
    
    or 
    
    if(v[0] == 0 && v[1] > 0)
          return (pi / 2);
    It's much easier to see the different parts of the statement when you separate them with white space. It's easier to see all that's going on when you put one command per line, and when you do decide to learn the debugger, you'll find it much easier as the debugger stops on each line to know which statement it's executing.

  9. #24
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: for() statement stops at 3rd interation

    Code:
    while(adj[0][0] > pw - 1)
        adj[0][0] = adj[0][0] - pw
    Seeing this part is isolation makes me realize that it's just a modulus operation.....therefore it can be written as
    Code:
        adj[0][0] &#37;= pw;

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)