samedi 16 mai 2015

Logic error, if else not matching

I've created a sorting function that arranges the coordinates of a cross in a order and prints them accordingly, for now I'm arranging it in a way that the coordinates are displayed in clockwise order.

#include <iostream>

using namespace std;

int main (int argc, char** argv)
{
    int xvalue[12];
    int yvalue[12];

    int x1 = 0;
    int y1 = 0;
    int x2 = 0; 
    int y2 = 0;
    int x3 = 0;
    int y3 = 0;
    int x4 = 0;
    int y4 = 0;
    int x5 = 0; 
    int y5 = 0;
    int x6 = 0;
    int y6 = 0;
    int x7 = 0;
    int y7 = 0;
    int x8 = 0; 
    int y8 = 0;
    int x9 = 0;
    int y9 = 0;
    int x10 = 0;
    int y10 = 0;
    int x11 = 0; 
    int y11 = 0;
    int x12 = 0;
    int y12 = 0;

    cout << "\nWelcome to Test." << endl;

    xvalue[0] = 13;
    yvalue[0] = 13;

    xvalue[1] = 13;
    yvalue[1] = 15;

    xvalue[2] = 12;
    yvalue[2] = 15;

    xvalue[3] = 12;
    yvalue[3] = 17;

    xvalue[4] = 13;
    yvalue[4] = 17;

    xvalue[5] = 13;
    yvalue[5] = 19;

    xvalue[6] = 15;
    yvalue[6] = 19;

    xvalue[7] = 15;
    yvalue[7] = 16;

    xvalue[8] = 18;
    yvalue[8] = 16;

    xvalue[9] = 18;
    yvalue[9] = 15;

    xvalue[10] = 14;
    yvalue[10] = 15;

    xvalue[11] = 14;
    yvalue[11] = 13;

    for(int i=0; i<12; i++)
    {
        cout << "X cord:" << xvalue[i] << " Y cord:" << yvalue[i] << endl;
    }

    int xmax = xvalue[1];
    int xmin = xvalue[1];

    int ymax = yvalue[1];
    int ymin = yvalue[1];

    for(int i=0; i<12; i++)
    {
        if(xvalue[i]>xmax)
        {
                xmax = xvalue[i];
        }
        else if(xvalue[i]<xmin)
        {
                xmin = xvalue[i];
        }
    }

    cout << "\nX min:" << xmin << " X max:" << xmax << endl;

    for(int i=0; i<12; i++)
    {
        if(yvalue[i]>ymax)
        {
                ymax = yvalue[i];
        }
        else if(yvalue[i]<ymin)
        {
                ymin = yvalue[i];
        }
    }

    cout << "Y min:" << ymin << " Y max:" << ymax << endl;

    for(int i=0; i<12; i++)
    {
        if (xmin == xvalue[i])
        {
            if (y1 == 0)
            {
                cout << "First if. X:" << xvalue[i] << " Y:" << yvalue[i] << endl;
                x1 = xvalue[i];
                y1 = yvalue[i];
                cout << "First if. X1:" << x1 << " Y1:" << y1 << endl;
            }
        }
        if (xmin == xvalue[i])
        {
            if (y1 != yvalue[i])
            {
                cout << "Second if. X:" << xvalue[i] << " Y:" << yvalue[i] << endl;
                x2 = xvalue[i];
                y2 = yvalue[i];
                cout << "Second if. X2:" << x2 << " Y2:" << y2 << endl;
            }
        }
        if(x2 != xvalue[i] && y2 == yvalue[i])
        {
            x3 = xvalue[i];
            y3 = yvalue[i];
        }
        if(x3 == xvalue[i] && y3 != yvalue[i])
        {
            x4 = xvalue[i];
            y4 = yvalue[i];
        }
        if(x4 != xvalue[i] && y4 == yvalue[i])
        {
            x5 = xvalue[i];
            y5 = yvalue[i];
        }
        if(x5 == xvalue[i] && y5 != yvalue[i])
        {
            x6 = xvalue[i];
            y6 = yvalue[i];
        }
        if(x6 != xvalue[i] && y6 == yvalue[i])
        {
            x7 = xvalue[i];
            y7 = yvalue[i];
        }
        if(x7 == xvalue[i] && y7 != yvalue[i])
        {
            x8 = xvalue[i];
            y8 = yvalue[i];
        }
        if(x8 != xvalue[i] && y8 == yvalue[i])
        {
            x9 = xvalue[i];
            y9 = yvalue[i];
        }
        if(x9 == xvalue[i] && y9 != yvalue[i])
        {
            x10 = xvalue[i];
            y10 = yvalue[i];
        }
        if(x10 != xvalue[i] && y10 == yvalue[i])//from here, the problem starts
        {
            x11 = xvalue[i];
            y11 = yvalue[i];
        }
        if(x11 == xvalue[i] && y11 != yvalue[i])//so this is affected as well
        {
            x12 = xvalue[i];
            y12 = yvalue[i];
        }

    }

    cout << "\n===================" << endl;
    cout << "X1: " << x1 << " Y1: " << y1 << endl;
    cout << "X2: " << x2 << " Y2: " << y2 << endl;
    cout << "X3: " << x3 << " Y3: " << y3 << endl;
    cout << "X4: " << x4 << " Y4: " << y4 << endl;
    cout << "X5: " << x5 << " Y5: " << y5 << endl;
    cout << "X6: " << x6 << " Y6: " << y6 << endl;
    cout << "X7: " << x7 << " Y7: " << y7 << endl;
    cout << "X8: " << x8 << " Y8: " << y8 << endl;
    cout << "X9: " << x9 << " Y9: " << y9 << endl;
    cout << "X10:" << x10 << "Y10:" << y10 << endl;
    cout << "X11:" << x11 << " Y11:" << y11 << endl;
    cout << "X12:" << x12 << " Y12:" << y12 << endl;
    cout << "===================" << endl;
}

My results should be: X1: 12 Y1: 15 X2: 12 Y2: 17 X3: 13 Y3: 17 X4: 13 Y4: 19 X5: 15 Y5: 19 X6: 15 Y6: 16 X7: 18 Y7: 16 X8: 18 Y8: 15 X9: 14 Y9: 15 X10:14 Y10:13 X11:13 Y11:13 X12:13 Y12:15

But my results are: X1: 12 Y1: 15 X2: 12 Y2: 17 X3: 13 Y3: 17 X4: 13 Y4: 19 X5: 15 Y5: 19 X6: 15 Y6: 16 X7: 18 Y7: 16 X8: 18 Y8: 15 X9: 14 Y9: 15 X10:14 Y10:13 X11:0 Y11:0 X12:0 Y12:0

I am not sure what is causing the variables not to match, please advice me, and if there are ways to improve my codes, please tell me as wel.

Aucun commentaire:

Enregistrer un commentaire