Nested For Loops and outputting asterisks - c++

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;
}

Related

Reverse right angle triangle pattern with numbers and asterisks using for-loops in C++

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.

C++ matrix problem : incorrect output for transpose matrix

I have to write a program to allow a user to input the number of rows and the number of columns of a matrix. The program will then construct the transpose of the matrix and display the transpose to the user, it is also supposed to display the trace and determinant of the matrix. I am having a problem with the output for the transpose, whenever the matrix is not a perfect square I get random numbers.
This is my code:
int main()
{
int matrix[10][10];
int row, column, i, j;
int temp = 0;
//size of matrix
cout << "Enter number of rows: ";
cin >> row;
cout << "Enter number of columns: ";
cin >> column;
// read the matrix values ( original matrix )
cout << "Enter the elements: \n";
for (i = 0; i < row; i++)
for (j = 0; j < column; j++)
cin >> matrix[i][j];
cout << "Matrix: \n";
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
cout << matrix[i][j] << ' ';
cout << endl;
}
// transpose the matrix values ( the matrix transposed )
cout << "Transpose of a Matrix: \n";
for (j = 0; j < row; j++)
{
for (i = 0; i < column; i++)
cout << matrix[i][j] << ' ';
cout << endl;
}
// trace of a matrix
int trace = 0;
if (row == column)
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
trace += matrix[i][i];
cout << "Trace of a Matrix: \n";
cout << trace << endl;
}
else
{
cout << "The trace will not be computed \n"
<< "The given matrix is not a perfect square" << endl;
}
// determinant of Matrix
int det = 0;
if (row == 2 && column == 2)
{
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
for (i = 0; i < row; i++)
cout << "Determinant of the Matrix: \n";
det = ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
cout << det << endl;
}
}
else
{
cout << "The determinant will not be computed \n"
<< "The given matrix is not a square" << endl;
}
return 0;
}
If I were to enter 3 rows and 2 columns and random elements my output looks like this:
Enter number of rows: 3
Enter number of columns : 2
Enter the elements:
1 5 6 -4 2 3
Matrix:
1 5
6 -4
2 3
Transpose of a Matrix:
1 6
5 -4
-858993460 -858993460
The trace will not be computed
The given matrix is not a perfect square
The determinant will not be computed
The given matrix is not a square
Is there something wrong in my code that causes -858993460 and -858993460 to come up?
Try this when printing the transpose (note the upper bounds for the loops have been swapped compared to your code) :
for (j = 0; j < column; j++)
{
for (i = 0; i < row; i++)
cout << matrix[i][j] << ' ';
cout << endl;
}
The reason you got "random elements", is because your code was reading and printing the contents of memory that hadn't been initialized yet.
Eg. in your example output, the first value -858993460 was obtained from matrix[2][0] (with j == 2 and i == 0), but that slot hadn't been filled with anything (the original matrix only occupies 0 <= i <= 2 and 0 <= j <= 1). Similarly, the second value -858993460 was obtained from matrix[2][1].
You are accessing parts of the memory, that have not been written to while reading in the matrix elements.
Instead of changing what i and j represent when printing the matrix, just change the order of the for loops from the order in that you were reading the elements.
cout << "Transpose of a Matrix: \n";
for (j = 0; j < column; j++)
{
for (i = 0; i < row; i++)
cout << matrix[i][j] << ' ';
cout << endl;
}

C++ print a reverse heart shape

enter image description here
Hello everyone, I am trying to print a reverse heart shape in c++. While user will input a integer, and the program will display the reverse heart according to the value.
in the photo, the input value are 1,3,4
Now my code can only show the triangle but still missing the two small triangle, so hoping can get some guide here.
int size,space;
cout << "Input size: ";
cin >> size;
int rows = (size * 2)+1;
for (int i = 1, k = 0; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
cout << " ";
}
while (k != 2 * i - 1)
{
cout << "* ";
++k;
}
cout << endl;
}
{
for (int i = size; i >= 1; --i)
{
for (int space = 0; space < size - i; ++space)
cout << " ";
for (int j = i; j <= 2 * i - 1; ++j)
cout << "* ";
for (int j = 0; j < i - 1; ++j)
cout << "* ";
cout << endl;
}
}
I try to print a Inverted first, but I fail to print two at the same time.

Triangle pattern

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.

Even-numbered rows not displaying properly, while odd-numbered rows do

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
}
}