looping all number combinations

对着背影说爱祢 提交于 2019-12-12 04:56:55

问题


I'm working on a test function that is supposed to test values automaticly to see if there is an unexpected error.

There are six "holders" that is supposed to have a value range between -999 to 1000

This is how I tried it:

    #include <stdlib.h>
#include <stdio.h>

int inuti(double x, double y, double x1, double y1, double x2, double y2) {
    int x_inuti;
    int y_inuti;

    if (x1 < x2)
        x_inuti = x > x1 && x < x2;
    else
        x_inuti = x > x2 && x < x1;
    if (y1 < y2)
        y_inuti = y > y1 && y < y2;
    else
        y_inuti = y > y2 && y < y1;
    return x_inuti && y_inuti;
}

int main(void) {
    double x, y, x1, y1, x2, y2;
    int resultat;
    x = -999;
    y = 1000;
    x1 = -999;
    y1 = 1000;
    x2 = -999;
    y2 = 1000;

    while (x<1000 && y>-999 && x1<1000 && y2 >-999 && x2<1000 && y2>-999) {
    x++;
    y--;
    y1++;
    x1--;
    x2++;
    y2--;


            printf("point x-value: %.1f \n", x);

            printf("point y-value: %.1f \n", y);

            printf("\n");

            printf("corner side x-value: %.1f \n", x1);


            printf("corner side y-value: %.1f \n", y1);

            printf("\n");

            printf("other corner side x-value%.1f \n", x2);


            printf("other corner side y-value%.1f \n", y2);

            printf("\n");


        resultat = inuti(x, y, x1, y1, x2, y2);

        if (resultat == 1)
            printf("point was inside rectangle.\n");
        else
            printf("point was outside rectangle.\n");

        printf("\n");
        printf("\n");
    }
        getchar();
    return 0;
}

However it doesn't include all combinations and it even exceeds the maximum and minimum number.

Does anyone know a better way to loop all combinations?

I appreciate all help

Thanks


回答1:


You're going to need at 6 different loops to test all different possible combinations.

for (x = -999; x <= 1000; x++) {
    for (y = -999; y < 1000; y++) {
        for (x1 = -999; x1 <= 1000; x1++) {
            // etc...
        }
    }
}

However be aware that there are 20006 = 64,000,000,000,000,000,000 different combinations, meaning the code will pretty much never finish executing (if each test takes 1 microsecond, the code will finish executing after about 2,028,080 years). A better option is to try testing specific inputs where you know the output. Make sure you test the edge cases too.



来源:https://stackoverflow.com/questions/28311310/looping-all-number-combinations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!