ページ 11

課題について

Posted: 2022年11月15日(火) 07:37
by あったろ
x^2+y^2<=1.0となる場合の数ncountをカウントする。
このプログラミングが分かりません。よろしくお願いします。

Re: 課題について

Posted: 2022年11月15日(火) 07:41
by みけCAT
何をお願いするのですか?
課題の丸投げは禁止です。
投稿は、フォーラムルールを読み、それに沿ったものをお願いします。
オフトピック
例えば「フォーラムルールを読み、それに沿った質問をお願いします。」と書いてしまうと、
質問なんか無い投稿者に質問することを要求して余計な負担をかけてしまうかもしれない…

Re: 課題について

Posted: 2022年11月23日(水) 19:39
by あたっしゅ
東上☆海美☆「

> x^2+y^2<=1.0となる場合の数ncountをカウントする。

これは、課題を一字一句間違いなくコピペしたものでしょうか ?
課題を理解していない人が、勝手に省略したもののように感じるみみ。

で、

> x^2+y^2<=1.0

円の方程式ですか ?

コード:

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


int
main(void) try
{
    int ncount = 0;

    for (int yy = -15; yy < 15; yy++) {
        const double y = (double)yy / 10;

        for (int xx = -15; xx < 15; xx++) {
            const double x = (double)xx / 10;

            if( x*x + y*y <= 1.0 ){
                printf("@ ");
            }
            else {
                printf(". ");
            }
        }
        printf( "\n" );
    }

    return EXIT_SUCCESS;
}
catch (...) {
    return EXIT_FAILURE;
}


// end.
> 数ncountをカウントする。

マンデルブロ集合でしょうか ?

コード:

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


__forceinline unsigned char
MandelCount( const float x, const float y )
{
	float cx = x;
	float cy = y;

	float sqx, sqy;
	unsigned char n = 0;

	do
	{
		sqy = cy * cy; // y^2

		cy += cy;
		cy *= cx;
		cy += y; // 2 * x * y + y0

		sqx = cx * cx; // x^2

		cx = sqx - sqy + x;   // x^2 - y^2 + x0

		--n;
		if (n == 0) {
			return 0;
		}
	} while (sqx + sqy < 4.0f);

	return n;
}


int
main(void) try
{
    for (int yy = -15; yy < 15; yy++) {
        const float y = (float)yy / 10;

        for (int xx = -15; xx < 15; xx++) {
            const float x = (float)xx / 10;

            printf("%c ", " 123456789"[ MandelCount(x, y) % 10]);
        }
        printf( "\n" );
    }

    return EXIT_SUCCESS;
}
catch (...) {
    return EXIT_FAILURE;
}


// end.
いずれも Visual Studio 2022 で、コンソールなプログラムみみ。