私が受けている C++ クラスのプログラムを設計しています。教授は、"*" を使用して菱形を出力するプログラムを作成してほしいと言っています。私が立ち往生しているのは、私のプログラムが出力する行が多すぎることです。
それ以外の
*
***
*
出力しています
*
***
*****
***
*
これを機能させるには、コードをどのように変更しますか? 午前中ずっとオンラインで答えを探していましたが、運が悪かったです。
これが私のコードです:
#include <iostream>
using namespace std;
int main () {
//Define Varaible
int N;
cout << "Please enter a postitive integer: ";
cin >> N;
cout << "Here is your diamond." << endl << endl;
for (int i = 0; i <= 2 * N; i++) {
for (int j = 0; j <= 2 * N; j++) {
if (i <= N) {
if (j < N - i || j > N + i) {
cout << ' ';
}
else {
cout << '*';
}
}
else {
if (j < i - N || j > 3 * N - i) {
cout << ' ';
}
else {
cout << '*';
}
}
}
cout << endl;
}
return 0;
}