Background and issue:
I have created a diamond shape program for class, but I'm having issues with an even amount of rows for the diamond. I'm thinking there may be something wrong with my logic. It seems to be whenever I use an even amount of rows for the diamond shape, it does not show up as an even amount of rows but odd.
I've tried different possible "solutions", but they didn't work.
For example, I changed for (int b = 0; b < asterisk; b++) to for (int b = 0; b <= asterisk; b++) It displayed the correct number of rows, however it was no longer much of a proper diamond shape. It also (obviously) affected the odd-numbered row diamonds so they don't look like proper diamonds either.
I'm completely stuck and would definitely appreciate a nudge in the right direction.
Here is my code:
#include <iostream>
using namespace std;
int main() {
int i, j, space, asterisk;
do
{
cout << "Enter the number of rows desired to make a diamond pattern (0 to quit): ";
cin >> i;
j = (i - 1) / 2;
for (int z = 0; z < i; z++)
{
space = abs(j - z);
asterisk = i - 2 * space;
for (int a = 0; a < space; a++)
cout << " ";
for (int b = 0; b < asterisk; b++)
cout << "*";
for (int c = 0; c < space; c++)
cout << " ";
cout << endl;
}
} while (i > 0);
cout << "Goodbye!" << endl;
}
Thank you very much!
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
int i, j, space, asterisk, is_even;
do
{
cout << "Enter the number of rows desired to make a diamond pattern (0 to quit): ";
cin >> i;
is_even = (i % 2 == 0) ? 1 : 0;
//Above line uses ternary operator to assign is_even flag to 1 if the number is even and 0 if it is not.
j = (i - 1) / 2;
for (int z = 0; z < i; z++)
{
space = abs(j - z);
asterisk = (is_even) ? i - 2 * space - 1 : i - 2 * space; //Change 1
for (int a = 0; a < space; a++)
cout << " ";
//Change 2.STARTS
if(space == 0 && is_even ){
for (int b = 0; b < asterisk; b++)
cout << "*";
cout<<endl;
}
//Change 2.ENDS
for (int b = 0; b < asterisk; b++)
cout << "*";
//for (int c = 0; c < space; c++)
// cout << " ";
//You dont need to add the spaces at the end of each line.
cout << endl;
}
} while (i > 0);
cout << "Goodbye!" << endl;
}
Output ::
Enter the number of rows desired to make a diamond pattern (0 to quit): 1
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 2
*
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 3
*
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 4
*
***
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 5
*
***
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 6
*
***
*****
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 7
*
***
*****
*******
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 8
*
***
*****
*******
*******
*****
***
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 0
Goodbye!
You could add an if else statement after getting the input from the user. Then you can make a decision based on that input for how you would want to display your diamond.
Pseudo code:
{
if ( odd ) {
// do it this way
} else { // even
// do it this way
}
}
Related
first time posting here, I'd like help with getting the output of the code upside down while still using the for commands, below is the best I can put in a clarification.
Desired Output: Actual Output:
123456 1*****
12345* 12****
1234** 123***
123*** 1234**
12**** 12345*
1***** 123456
Code:
#include <iostream>
using namespace std;
int main() {
int num, row, integ;
cout << "Please enter size: ";
cin >> integ;
for (row = 1; row <= integ; row++) {
for (num = 1; num <= row; num++) {
cout << num;
}
for (; num <= integ; num++) {
cout << "*";
}
cout << endl;
}
}
first time answering here :).
change num <= row to num <= integ - row + 1
Let's try to understand what we require.
We want to display number followed by stars. The numbers should decrease in each iteration and stars should increase.
Iteration-1: display 1 to integ - 0 and 0 asterisk
Iteration-2: display 1 to integ - 1 and 1 asterisk
Iteration-3: display 1 to integ - 2 and 2 asterisk
Iteration-4: display 1 to integ - 3 and 3 asterisk
...
and so on.
As you can see, in each iteration we need to display value from 1 to (integ - x) and x asterisk, where x will starts from zero and keep on increasing.
To put this logic in code:
int main() {
int integ;
cout << "Please enter size: ";
cin >> integ;
for(int i = 0; i < integ; ++i) {
for(int j = 0; j < integ; ++j) {
if(j < (integ-i)) {
cout << j+1;
} else {
cout << '*';
}
}
std::cout << '\n';
}
}
And here is the output:
123456
12345*
1234**
123***
12****
1*****
Hope it helps.
I need to produce following pattern using two for loops.
a
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
I have try this:
#include <iostream>
using namespace std;
int main()
{
int a;
a = 7;
for (int i = 1;i <= a;i++) {
cout << "" << endl;
for (int i = 1;i <= a;i++) {
cout << "*";
}
}
}
But the result is 7 characters in seven rows :(
For starters the task can be done using only one loop. For example
#include <iostream>
#include <iomanip>
int main()
{
while ( true )
{
const char c = '*';
std::cout << "Enter a non-negative number (0 - exit): ";
unsigned int n;
if ( not ( std::cin >> n ) or ( n == 0 ) ) break;
std::cout << '\n';
for ( unsigned int i = 0; i < n; i++ )
{
std::cout << std::setw( i + 2 ) << std::setfill( c ) << '\n';
}
std::cout << '\n';
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 7
*
**
***
****
*****
******
*******
Enter a non-negative number (0 - exit): 6
*
**
***
****
*****
******
Enter a non-negative number (0 - exit): 5
*
**
***
****
*****
Enter a non-negative number (0 - exit): 4
*
**
***
****
Enter a non-negative number (0 - exit): 3
*
**
***
Enter a non-negative number (0 - exit): 2
*
**
Enter a non-negative number (0 - exit): 1
*
Enter a non-negative number (0 - exit): 0
As for your code then the inner loop outputs exactly 7 characters '*'
for (int i = 1;i <= a;i++) {
cout << "*";
}
So what you do is what you get.
You could write the inner loop for example the following way
for (int j = 0;j < i; j++) {
cout << "*";
}
I am trying to get output like
*****
****
***
**
*
But my code is giving the wrong output.
My output is:
Please enter the size: 9
*********
********
*******
******
*****
****
***
**
*
and my code is:
#include<iostream>
using namespace std;
int main (void)
{
int row, column, size;
cout << "Please enter the size: ";
cin >> size;
cin.ignore(999,'\n');
for (row = size; row <= size; row--)
{
if (row <= 0)
{
break;
}
else if (row == size)
{
for (column = 1; column <= size; column++)
{
cout << "*";
}
}
else
{
for (column = row; column <= row; column--)
{
if (column <= 0)
{
break;
}
cout << ' ';
}
for (column = row; column <= row; column--)
{
if (column <= 0)
{
break;
}
cout << "*";
}
}
cout << endl;
}
cout << endl;
cout << "Please press enter to finish...";
cin.ignore(999,'\n');
return 0;
}
I don't know what's wrong and where it is but I am thinking the problem might be in the else loop.
What you are doing here is :
print *s for first line (in amount of size)
if not first line print size - loopCounter spaces then print size - loopCounter *s
As you can see this algorithm can't get you the shape you want. Also why you loop backward and check for none negative value? you don't need it. What you actually want is :
an outer loop to generate columns (prints new line)
an inner loop to generate data of each row
The only thing that important here is how to generate data of each row. as you can see the count of spaces in each row is equal to index of column (starting with 0).
Here's what you can try (I broke inner loop into two loops):
#include<iostream>
using namespace std;
int main(void)
{
int size;
cout << "Please enter the size: ";
cin >> size;
cin.ignore(999, '\n');
for(int column = 0; column < size; ++column)
{
for(int spaces = 0; spaces < column; ++spaces)
{
cout << " ";
}
for(int starts = 0; starts < size - column; ++starts)
{
cout << "*";
}
cout << endl;
}
cout << "Please press enter to finish...";
cin.ignore(999, '\n');
return 0;
}
Try to re-think your problem. You have a lot of complex code to achieve something simple. Your output should look like this:
*****
****
***
**
*
So some things to note about this:
The rows are all the same length!
The only difference is the number of spaces. If we have row, 0 we have 0 spaces, row 4 has 4 spaces.
So with this your code should be simple:
// PSEUDO CODE
for row = 0 to max_rows
for i = 0 to max_rows
if (i < row)
print a space
else
print a *
And that should do it.
This question already has an answer here:
Printing a “triangle” of asterisks, in c++
(1 answer)
Closed 4 years ago.
I am trying to print out the shape of a triangle but I am kinda lost...
this is what I have so far:
#include <iostream>
using namespace std;
int main()
{
int i, k, n;
cout << "Please enter number of rows you want to see: \n";
cin >> n;
for (k = 1; k <= n; k++)
{
for (i = 1; i <= k; i++)
cout << '*';
cout << endl;
}
getchar();
getchar();
return 0;
}
This code works fine for a right angled triangle -
*
**
***
But I guess you want a triangle like this -
*
***
*****
Try this -
#include <iostream>
using namespace std;
int main()
{
int i, j, k, n;
cout << "Please enter number of rows you want to see: \n";
cin >> n;
for (k = 1; k <= n; k++)
{
for(j = 1; j <= n-k; j++)
cout << ' ';
for (i = 1; i <= 2*k-1; i++)
cout << '*';
cout << endl;
}
return 0;
}
I think the code is pretty straightforward to understand. The first inner for loop is to print the spaces and the second inner for loop is to print the *
This does print the shape of a triangle. For example, when you put in 5, the program outputs
*
**
***
****
*****
If your computer isn't printing this output, it's not a problem with your code.
i have to write a c++ program that asks the user to enter an integer k and then outputs k lines of asterisks with the first line starting at 1 asterisk and the last line finishing with k asterisks.
i can get the program to output a square of asterisks such as:
(k=5)
*****
*****
*****
*****
*****
when it should look like this:
*
**
***
****
*****
how should i adjust my program to accomplish this?
(Note: i have to use two for loops.)
int main() {
int k, cols, rows;
cout << " Please enter a number: ";
cin >> k;
for (cols = 1; cols < k + 1; cols++) {
for (rows = 1; rows < k + 1; rows++)
cout << "*";
cout << endl;
}
getchar();
getchar();
return 0;
}
The inner loop should run col+1 times. So you need change the condition in the inner loop to rows < cols and have rows be one less than cols by changing its starting value to 0:
for (cols = 1; cols < k+1; cols++) {
for (rows = 0; rows < cols; rows++)
cout << "*";
cout << endl;
}