I want to create a project that will print the '|' character as 4 layers going 1 3 5 7 something like
|
|||
|||||
|||||||
I wrote a for loop for this and the code is here:
for (int i = 1; i <= 4; i ) {
//for loop for displaying space
for (int s = i; s < 4; s ) {
cout << " ";
}
//for loop to display star equal to row number
for (int j = 1; j <= (2 * i - 1); j ) {
cout << "|";
}
// ending line after each row
cout << "\n";
}
So how can I make a code that will take user input like
cout << "Please enter a row number \n" << "Please enter a column number" << endl;
and let say the user entered 2 as row number 2 as column number I want the output to be something like
|
|
|||||
|||||||
Deletes 2 '|' character from the 2nd row First I think putting every character in a array like char arr[] = { '|' , '||' , '|||', '||||'} and deleting according to user input but I failed. Any help?
CodePudding user response:
Limiting your pile of bars to 4 levels, this should work:
- You basically just want a fixed size string of bars, '|'.
- Then remove
n
consecutive characters from that string. - The only thing you have to calculate is the starting index to start removing from, then replace
n
characters with blanks. - You can add some checks for
row
andcol
boundaries.
#include <iostream> // cout
#include <string>
int main()
{
std::string bars(16, '|');
auto get_start_deleting_pos = [](int row, int col) {
if (row == 1) { if (col > 1) { return -1; } return 0; }
else if (row == 2) { if (col > 3) { return -1; } return col; }
else if (row == 3) { if (col > 5) { return -1; } return 3 col; }
else if (row == 4) { if (col > 7) { return -1; } return 8 col; }
else return -1;
};
auto print_bars = [&bars]() {
std::cout << " " << bars[0] << "\n";
std::cout << " " << bars.substr(1, 3) << "\n";
std::cout << " " << bars.substr(4, 5) << "\n";
std::cout << bars.substr(9) << "\n";
};
auto start_deleting_from_row{4};
auto start_deleting_from_col{1};
auto num_chars_to_delete{4};
auto pos{ get_start_deleting_pos(start_deleting_from_row, start_deleting_from_col) };
if (pos != -1)
{
bars.replace(pos, num_chars_to_delete, num_chars_to_delete, ' ');
}
print_bars();
}
And if you want a more generic solution, where the user inputs the level
, the row
and col
to start deleting from, and the number of characters to delete:
#include <iostream> // cout
#include <string>
auto get_size_for_levels(int l) { return l*l; }
auto get_index_for_row_and_col(int row, int col) { return (row - 1) * (row - 1) - 1 col; }
auto get_num_cols_for_row (int row) { return row * 2 - 1; }
auto check_row_and_col(int levels, int row, int col) {
if (row < 1 or levels < row) { return false; }
if (col < 1 or get_num_cols_for_row(row) < col) { return false; }
return true;
}
int main()
{
auto levels{7}; // levels start at 1
auto start_deleting_from_row{4}; // rows start at 1
auto start_deleting_from_col{5}; // cols start at 1
auto num_chars_to_delete{6};
std::string bars(get_size_for_levels(levels), '|');
if (check_row_and_col(levels, start_deleting_from_row, start_deleting_from_col))
{
bars.replace(
get_index_for_row_and_col(start_deleting_from_row, start_deleting_from_col),
num_chars_to_delete,
num_chars_to_delete,
' ');
}
for (int l{1}; l <= levels; l)
{
std::cout
<< std::string(levels - l, ' ')
<< bars.substr(get_index_for_row_and_col(l, 1), get_num_cols_for_row(l))
<< "\n";
}
}
CodePudding user response:
Here is a solution:
#include <iostream>
#include <vector>
std::size_t getLayerCount( )
{
std::cout << "How many layers to print: ";
std::size_t layerCount { };
std::cin >> layerCount;
return layerCount;
}
std::vector< std::vector<char> > generateShape( const std::size_t layerCount )
{
const std::size_t MAX_CHAR_COUNT_IN_A_ROW { layerCount * 2 };
constexpr char spaceChar { ' ' };
std::vector< std::vector<char> > shape( layerCount, std::vector<char>( MAX_CHAR_COUNT_IN_A_ROW, spaceChar ) );
for ( std::size_t row { }; row < layerCount; row )
{
for ( std::size_t offset { layerCount - row - 1 }; offset < layerCount row; offset )
{
shape[ row ][ offset ] = '|';
}
shape[ row ][ MAX_CHAR_COUNT_IN_A_ROW - 1 ] = '\0';
}
return shape;
}
void printShape( const std::vector< std::vector<char> >& shape )
{
for ( const auto& row : shape )
{
std::cout.write( row.data( ), row.size( ) ).write( "\n", 1 );
}
}
void deleteSpecificChars( std::vector< std::vector<char> >& shape )
{
std::cout << "Please enter a row number: ";
std::size_t rowNumber { };
std::cin >> rowNumber;
std::cout << "Please enter a column number: ";
std::size_t colNumber { };
std::cin >> colNumber;
--rowNumber;
--colNumber;
const std::size_t layerCount { shape.size( ) };
const std::size_t posOfFirstCharInRow { layerCount - rowNumber - 1 };
const std::size_t posOfTargetCharInRow { posOfFirstCharInRow colNumber };
const std::size_t posOfLastCharInRow { posOfFirstCharInRow ( 2 * rowNumber ) };
for ( std::size_t idx { posOfTargetCharInRow }; idx <= posOfLastCharInRow; idx )
{
shape[ rowNumber ][ idx ] = ' ';
}
}
int main( )
{
const std::size_t layerCount { getLayerCount( ) };
std::vector< std::vector<char> > shape { generateShape( layerCount ) };
printShape( shape );
deleteSpecificChars( shape );
printShape( shape );
return 0;
}
Sample input/output:
How many layers to print: 4
|
|||
|||||
|||||||
Please enter a row number: 2
Please enter a column number: 2
|
|
|||||
|||||||
Another one:
How many layers to print: 5
|
|||
|||||
|||||||
|||||||||
Please enter a row number: 4
Please enter a column number: 4
|
|||
|||||
|||
|||||||||