Cで中実および中空の正方形パターンを印刷するプログラム
幾何学では、正方形は通常の四辺形です。つまり、4つの等しい辺と4つの等しい角度があります。
下図のように中実と中空の四角が表示されます
アルゴリズム
ソリッドスクエアの場合-
Accept the Number of Rows from the user to draw the Solid Square For each Row, Print * for each Column to draw the Solid Square
歩兵方陣の場合-
Accept the Number of Rows from the user to draw the Hollow Square For the First and Last Row, Print * for each Column For the Remaining Rows, Print * for the first and Last Column.
例
/* Program to print hollow and Solid Square pattern */
#include <stdio.h>
int main(){
int r, c, rows; //Hollow Rhombus
int r1,c1, rows1; //Solid Rhombus
clrscr();
/* Hollow Square */
printf("Enter the Number of rows for Hollow Square: ");
scanf("%d", &rows);
printf("\n");
for (r=1; r<=rows; r++){
if (r==1 || r==rows){
for (c=1; c<=rows; c++){
printf("*");
}
}
else{
for (c=1; c<=rows; c++){
if (c==1 || c==rows){
printf("*");
}
else{
printf(" ");
}
}
}
printf("\n");
}
printf("\n");
/* Solid Square */
printf("Enter the Number of rows for Solid Square: ");
scanf("%d", &rows1);
printf("\n");
for (r1=1; r1<=rows1; r1++){
for (c1=1; c1<=rows1; c1++){
printf("*");
}
printf("\n");
}
getch();
return 0;
} 出力
-
配列の下三角行列と上三角行列をCで出力するプログラム
プログラムの説明 配列の下三角行列と上三角行列を印刷するプログラムを作成します。 三角行列 三角行列は、下三角または上三角のいずれかです。 下三角行列 主対角線より上のすべてのエントリがゼロの場合、正方行列は下三角行列と呼ばれます。 上三角行列 主対角線の下のすべてのエントリがゼロの場合、正方行列は上三角行列と呼ばれます。 フォームのマトリックス $$ {\ displaystyle L ={\ begin {bmatrix} \ ell _ {1,1} &&&&0 \\\ ell _ {2,1}&\ ell _ {2,2} &&&\\\ ell _ {3、 1}&\
-
中空のピラミッドとダイヤモンドのパターンをC++で印刷するプログラム
ここでは、C++を使用して中空のピラミッドとダイヤモンドのパターンを生成する方法を説明します。ソリッドピラミッドパターンを非常に簡単に生成できます。中空にするには、いくつかのトリックを追加する必要があります。 中空ピラミッド 最初の行のピラミッドには1つの星が印刷され、最後の行にはn個の星が印刷されます。他の行の場合、行の開始と終了に正確に2つの星が印刷され、これら2つの開始の間に空白があります。 サンプルコード #include <iostream> using namespace std; int main() { int n, i, j; &nbs