C++ I\'m trying to make a diamond, but I can\'t get the code for the diamond rig
ID: 3677049 • Letter: C
Question
C++ I'm trying to make a diamond, but I can't get the code for the diamond right. What am I doing wrong?
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main(){
int number;
cin >> number;
//Hourglass
for (i = number; i > 0; i--)
{
cout << endl;
for (int j = number; i < j; j--)
{
cout << " ";
}
for (int j = 0; i > j; j++)
{
cout << "*";
}
for (int j = 0; i > j; j++)
{
cout << "*";
}
}
for (int i = 0; i < number; i++)
{
for (int j = 0; j < (number - (i + 1)); j++)
{
cout << " ";
}
for (int j = 0; j < (2 * i + 1); j++)
{
cout << "*";
}
for (int j = 0; j < (number - (i + 1)); j++)
{
cout << " ";
}
cout << endl;
}
}
Explanation / Answer
/* C++ Program - Print Diamond Pattern */
#include<iostream>
using namespace std;
int main()
{
int n, c, k, space=1;
cout<<"Enter number of rows (for diamond dimension) : ";
cin>>n;
space=n-1;
for (k=1; k<=n; k++)
{
for(c=1; c<=space; c++)
{
cout<<" ";
}
space--;
for(c=1; c<=(2*k-1); c++)
{
cout<<"*";
}
cout<<" ";
}
space=1;
for(k=1; k<=(n-1); k++)
{
for(c=1; c<=space; c++)
{
cout<<" ";
}
space++;
for(c=1 ; c<=(2*(n-k)-1); c++)
{
cout<<"*";
}
cout<<" ";
}
return 0;
}
/*
Output:
Enter number of rows (for diamond dimension) : 12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.