Triangle pattern - c++

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.

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.

How to use "*" to make letters using rows and columns

Here is the code of which I'm writing to create the letter "Z" for example just cant figure out how to connect the rows and columns. Also don't know why its re-writing the code as many times by the user input. ex: user inputs 5 rows, it produces the "letter" 5 times over. if anyone is able to help me resolve this issue, it would be greatly appreciated!
#include <iostream>
#include <string>
#include <cmath>
#include <random>
#include <time.h>
using namespace std; //when compiler sees a name it does not recognize, assume std;
//Program 2 by anon
int main() {
cout<<"This program draws characters. Select character, then height(s)\n";
const string PROMPT{"how many rows tall? (0 to quit): "};
bool running{true};
while (running) {
cout << "\nOption: a)Z b)H /)/ \\)\\ q)quit? (q to quit): ";
char option{}; cin>>option;
if (option=='a') { // box
while (true) {
cout << "([z]) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int col{1}; col<=rows; ++col) { // for each column
for (int row{1}; row<=rows; ++row)
cout << string(rows-row, ' ') << "*\n";
cout << "*";
}
cout<<endl;
}
}
if (option=='b') { // forward slash
while (true) {
cout << "(H) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) {
cout << string(row-row, ' ') << "*\n";
for (int col{1}; col<=rows; ++col);
}
cout<<endl;
}
cout<<endl;
}
if (option=='/') { // forward slash
while (true) {
cout << "(/) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) {
cout << string(rows-row, ' ') << "*\n";
}
cout<<endl;
}
}
else if (option=='\\') { // back slash
while (true) {
cout << "(\\) " << PROMPT;
int rows=0;
cin >> rows;
if (rows<=0) break;
for (int row{1}; row<=rows; ++row) { // backslash
cout << string(row-1, ' ') << "*\n";
}
cout<<endl;
}
}
else if (option=='q') {
break;
}
else {
cout<<" Invalid option, try again.\n";
}
}
cout<<"Goodbye\n";
return 0;
}
desired output:
a) Z b) H /)/ \\)\\ q) quit:a
How many rows tall? (0 to quit): 8
********
*
*
*
*
*
*
*
*
********
how many rows tall? (0 to quit): // loop reruns.
In order to draw the Z you need to add the horizontal "*" line at the beginning and the end. In addition you have to remove the outer loop, to avoid the letter to be drawn multiple times. This loop needs to be seperate at the beginning and the end, to draw the horizontal line:
if (option == 'a')
{ // box
while (true)
{
cout << "([z]) " << PROMPT;
int rows = 0;
cin >> rows;
if (rows <= 0) break;
for (unsigned int i = 0; i < rows; ++i)
cout << "*";
cout << endl;
for (int row{ 1 }; row <= rows; ++row)
cout << string(rows - row, ' ') << "*\n";
for (unsigned int i = 0; i < rows; ++i)
cout << "*";
cout << endl;
}
}

center the 1 at the top

#include <iostream>
using namespace std;
int main()
{
int rows, count, num, space;//creating four variables to use for the loops
cout << "Enter number of rows: ";//prompting the user to input the number of rows
cin >> rows;
while (rows < 1 || rows>9)
{
cout << "Entry must be between 1 and 9. Please Re-enter the number of rows" << endl; //Asking the user to re-enter the number of rows if it was an invalid input
cin >> rows;
}
for (count = 1; count <= rows; count++) //for function that loops from 1-how many rows the user puts in
{
for (space= 1; space < rows; space++) //inner loop that loops from 1 to how many rows the user put in and outputs a space
{
cout << " ";
}
for (num = 1; num <= (2*count-1); num++)//the last row of the pyramid has one less than two time sthe number the user input so this loops unitl that is hit
{
cout <<count << " ";
}
cout << "\n"; //outputs a new line
}
system("pause");
return 0;
}
Looking at the code and you are making a right Angled Triangle of numbers and guessing you need an Isosceles Pyramid of numbers
Refer the code below:
#include <iostream>
#include <string>
bool makeIsoscelesPyramid(const unsigned int& size)
{
if( size > 9 )
{
std::cout<<"Please Enter a positive number less than 9"<<std::endl;
return false;
}
for (int i = 0; i < size; i++)
{
//Number of Space to input
auto nSpace = size - ( i );
std::cout << std::string( nSpace, ' ');
//Times the number is repeated 1, 3, 5 ...
auto nTimesNumber = (i * 2) + 1;
//'1'+i could only print till 9
std::cout << std::string( nTimesNumber, '1'+i) << std::endl;
}
return true;
}
int main()
{
unsigned int nRows = 0;
bool bReturn = false;
do
{
std::cout << "Enter number of rows: \t";
std::cin>>nRows;
std::cout<<std::endl;
bReturn = makeIsoscelesPyramid(nRows);
} while( !bReturn );
return 0;
}
Output:
Enter number of rows: 5
1
222
33333
4444444
555555555

Nested For Loops and outputting asterisks

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

When displaying an array using a loop, nothing works

Here's my code
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
int row, col;
//this code below is to loop it so it displays all the values
/* for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++)
{
cout << matrix[row][col];
cout << endl;
}
*/
int looper = 0;
// THIS IS THE FORMAT FOR DISPLAYING ONE LINE MAN
//cout << matrix[0][0];
//cout << matrix[0][1];
//this is just some test code to see if it can output certain values right
// cout << matrix[0][0];
// cout << matrix[0][1];
// cout << matrix[0][2];
// cout << matrix[0][3];
}
I have tried everything. I have tried for loops and I've tried while loops. I don't understand why it won't work. It either displays nothing and immediately crashes, or it just keeps "scrolling" down, like it's repeating infinitely but not displaying any text.
There was a while loop I tried that went like this
int looper;
int looper = NUMBER_OF_STUDENTS;
//in this instance, NUMBER_OF_STUDENTS was equal to 28
while (looper > 0)
{
cout << matrix[looper][0];
cout << matrix[looper][1];
looper--;
//i have tried both looper-- and --looper
}
I don't understand why it isn't working at all; it's incredibly frustrating. This is the same program I'm working on as How do I skip the first line of an array when reading from a file?
I feel very guilty asking you guys for so much help, but I'm seriously about to snap here.
EDIT: Here's my entire code.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int NUMBER_OF_STUDENTS = 28;
const int NUMBER_OF_SCORES = 28;
void getData (ifstream& infile, string matrix[][NUMBER_OF_SCORES + 1 ],
int NUMBER_OF_STUDENTS) ;
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS);
int main()
{
string birdarray [NUMBER_OF_STUDENTS][NUMBER_OF_SCORES +1 ] ;
// column 0 will hold the student ID.
// row n has the ID and birdarray for student n.
ifstream inData; //input file stream variable
inData.open("one.txt");
if ( !inData)
{
cout << "invalid file name \n";
return 1;
}
// input the birdarray into two-D array birdarray
getData ( inData , birdarray, NUMBER_OF_STUDENTS );
printMatrix(birdarray, NUMBER_OF_STUDENTS);
// return the row number of a searchItem in a particular column.
// if not found, return -1
}
void getData (ifstream& infile,string chart[][NUMBER_OF_SCORES + 1 ],
int student_count)
{
int row, col;
string dummyLine;
getline(infile, dummyLine);
for ( row = 0; row < student_count; row++)
for (col =0; col < NUMBER_OF_SCORES +1 ; col++)
infile >> chart [row] [col] ;
}
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
int row, col;
//this code below is to loop it so it displays all the values
/* for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++)
{
cout << matrix[row][col];
cout << endl;
}
*/
int looper = 0;
// THIS IS THE FORMAT FOR DISPLAYING ONE LINE MAN
//cout << matrix[0][0];
//cout << matrix[0][1];
//this is just some test code to see if it can output certain values right
// cout << matrix[0][0];
// cout << matrix[0][1];
// cout << matrix[0][2];
// cout << matrix[0][3];
}
//prints a labeled listing of students' scores
Here's the normal way to print a table of values
void printMatrix(string matrix[][NUMBER_OF_SCORES + 1], int NUMBER_OF_STUDENTS)
{
for (int row = 0; row < NUMBER_OF_STUDENTS + 1; ++row)
{
for (int col = 0; col < NUMBER_OF_SCORES; ++col)
{
cout << matrix[row][col] << ' ';
}
cout << '\n';
}
}
BUT there are lots of things about your code I don't like. So whether this is right I cannot say. In particular I'm dubious about
1) Why do you have NUMBER_OF_STUDENTS + 1? No obvious need for the + 1, you are probably trying to compensate for a mistake you made elsewhere.
2) Why do you have a matrix of strings? Scores would normally be a number.
I guess the main lesson to learn is to think about exactly what the code you write does. Code isn't a mysterious magic spell, it's a precise series of instructions to the computer. If you had thought about exactly what your code does, followed it through step by step, you would have seen the errors you'd made, and hopefully been able to fix them. You've got to get into that way of thinking.
Your attempts so far would not actually print the entire matrix even if they did not loop continuously. First for the while loop, it's very likely you should be initializing looper at NUMBER_OF_STUDENTS - 1 because if there are NUMBER_OF_STUDENTS rows in the array then the highest element would be NUMBER_OF_STUDENTS - 1. It's possible that you're saying NUMBER_OF_STUDENTS + 1 because of a coding problem elsewhere however. If that's the case I'd try to take care of that issue first for simplicity's sake.
Regarding your for loop
for (row = 0; row < NUMBER_OF_STUDENTS + 1; col++) {
cout << matrix[row][col];
cout << endl;
}
this will loop forever because your stop condition is dependent on row, but row never changes, only col does.
To print all the elements in your matrix you need two loops. Something like this:
for(int row=0; row < numOfRows; row++)
for(int col=0; col < numOfCols; col++)
cout << matrix[row][col];