draw X letter shape using asterisk(*) - c++

i want to write a program to draw the shape of X letter using asterisk(*)
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
int i, j;
for(i = 1; i <= 9; i++){
for(j = 1; j <= 12; j++){
if(i == j){
cout << "***";
}else{
cout << " ";
}
}
cout<< endl;
}
return 0;
}
i am very new to programming
i only made (\) how can I make the whole X
***------***
-***----***-
--***--***--
---******---
--***--***--
-***----***-
***------***
that's what i did uptill now
include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i, d, a=1,b=12,c;
for(i = 1; i <= 6; i++)
{
for (d=1; d<i;d++) {cout <<" ";}
cout<<"***";
for (c=a+1; c<b;c++) {cout <<" ";}
{cout<<"***";}
for(b=12-i;b<i;b++)
{cout<<"***";}
cout<<endl;
a++;
}
return 0;
}
i divided the top of the (\//) to three parts
[space][][space][]

I have written the following function/method in java. You can convert it to c++;
public static void printX(int x) {
char[] chars = new char[x];
for (int i = 0; i < x; i++) {
chars[i] = '*';
chars[x - 1 - i] = '*';
for (int j = 0; j < x; j++) {
if (j == i || j == (x - 1 - i)) {
continue;
}
chars[j] = ' ';
}
System.out.println(new String(chars));
}
}
If you call the above function/method as printX(5); The output will by 5x5 sized and containing X character.
* *
* *
*
* *
* *

**** ****
*** ***
** **
* *
** **
*** ***
**** ****
Firstly, pardon my very uneven X. For you as a beginner I would give out an algo for you to ponder upon rather than spoon feeding a code.
The interpreter does not know how to come back to a line which has already been printer.
Therefore, you would have to draw both sides of the X in one iteration of the loop.
After that you decrease you star count (star--) and draw line # 2.
Repeat till the mid way mark when your stars are 0.
When your code sees that the stars are 0, then start with the same loop but this time star++ in each iteration.
This is repeated till the starting count of the star i.e 4 in my case.
If any problems you can post your code on the site :)

You shall dynamically evaluate the spacing areas in each row. Draw manually desired shape on piece of paper and try to create function, which takes as an argument the row number and returns amount of spaces required in the specific row. For example:
* *
* *
*
* *
* *
The amount of spaces in each row equals:
0 [*] 3 [*]
1 [*] 1 [*]
2 [*]
1 [*] 1 [*]
0 [*] 3 [*]
Note, that inside each row you'll need two loops: first for the initial and middle spaces.

The solution I wrote 2 decades ago (when I was still learning):
Make an line-column array, e.g. char screen[80][25];
Clear it by setting all entries to ' '
"Draw a point" at x,y by setting screen[x][y]='*';
When done, render the whole screen[80][25] by calling cout 2080 times. (2000 times for the characters and and 80 times for endl)
In your case, you know how to draw a \. You can easily adapt this. But with my method, you can then draw a / in the same screen array. And when you're done, you have an overlapping / and \ at the last step: X
I used this method since we had to draw a circle and that's really a lot harder. And yes, nowadays I'd probably use std::vector<std::string> screen but back then screens were really 80x25 :)

#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i, d, a=1,b=12,c ,e=1;
for(i = 1; i <= 6; i++)
{
for (d=1; d<i;d++) {cout <<" ";}
cout<<"***";
for (c=a+1; c<b;c++) {cout <<" ";}
{cout<<"***";}
for(b=12-i;b<i;b++)
{cout<<"***";}
cout<<endl;
a++;
}
for( i = 1; i <= 3; i++)
{
for ( d=6; d>i;d--) {cout <<" ";}
cout<<"***";
for (c=0; c<e-1;c++) {cout <<" ";}
{cout<<"***";}
cout<<endl;
e++;
}
return 0;
}

int n=11,i=0,k=0,j=0;
for(i=0;i<n;i++)
{
if(i<(n/2))
{
cout<<endl;
for(j=0;j<i;j++)
{
cout<<" ";
}
cout<<"*";
for(k=n/2;k>i;k--)
{
cout<<" ";
}
cout<<"*";
}
else
{
cout<<endl;
for(k=n-1;k>i;k--)
{
cout<<" ";
}
cout<<"*";
for(j=n/2;j<i;j++)
{
cout<<" ";
}
cout<<"*";
}
}

#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i = 1;i<= 5;i++)
{
for(j = 1;j<= 5;j++)
{
if((i == j)||(j==(5+1)-i))
{
cout << "*";
}
else{
cout << " ";
}
}
cout<< endl;
}
system("pause");
}

#include "stdafx.h"
#include <iostream>
using namespace std;;
int _tmain(int argc, _TCHAR* argv[])
{
int i,k,j;
for (i=1;i<8;i++)
{
for (int k=0;k<i;k++)
{
cout<<" ";
}
cout<<"*";
for (int k=8;k>i;k--)
{
cout<<" ";
}
cout<<"*";
cout<<endl;
}
for (i=1;i<8;i++)
{
for (int k=8;k>i;k--)
{
cout<<" ";
}
cout<<"*";
for (int k=0;k<i;k++)
{
cout<<" ";
}
cout<<" *";
cout<<endl;
}
system("Pause");
return 0;
}

Related

printing the below pattern using just one loop

ive got the below code to print a pattern (attached below). However i'd like to just use one loop
#include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<"*";
}
for(int j=1;j<=n-i;j++){
if(j%2!=0){
cout<<"_";
}else{
cout<<".";
}
}
cout<<endl;
}
for(int i=1;i<n;i++){
for(int j=1;j<=n-i;j++){
cout<<"*";
}
for(int j=1;j<=i;j++){
if(j%2==0){
cout<<".";
}else{
cout<<"_";
}
}
cout<<endl;
}
}
when n = 5, heres the output.
*_._.
**_._
***_.
****_
*****
****_
***_.
**_._
*_._.
how do i just make this into one single loop
Try this and see how it does what you want to understand the step you did not find on your own:
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n*2-1; i++) {
if (i <= n)
{
for (int j = 1; j <= i; j++) {
cout << "*";
}
for (int j = 1; j <= n - i; j++) {
if (j % 2 != 0) {
cout << "_";
}
else {
cout << ".";
}
}
cout << endl;
}
else
{
for (int j = 1; j <= n*2 - i; j++) {
cout << "*";
}
for (int j = 1; j <= i-n; j++) {
if (j % 2 == 0) {
cout << ".";
}
else {
cout << "_";
}
}
cout << endl;
}
}
}
I'd like to just use one loop.
I'll take it literally and show a starting point for a possible solution.
// Let's start by figuring out some dimensions.
int n;
std::cin >> n;
int height = 2 * n - 1;
int area = n * height;
// Now we'll print the "rectangle", one piece at a time.
for (int i = 0; i < area; ++i)
{ // ^^^^^^^^
// Extract the coordinates of the char to be printed.
int x = i % n;
int y = i / n;
// Assign a symbol, based on such coordinates.
if ( x <= y and x <= height - y - 1 )
{ // ^^^^^^ ^^^^^^^^^^^^^^^^^^^ Those are the diagonals.
std::cout << '*'; // This prints correctly the triangle on the left...
}
else
{
std::cout << '_'; // <--- But of course, something else should done here.
}
// End of row.
if ( x == n - 1 )
std::cout << '\n';
}
If you look at the pattern, then you can see a sort of "triangles". And this already gives a hint for the solution. Use a triangle function.
Please read about it here.
Then you will notice that always the "aboslute"-function, in C++ std::abs, is involved.
But first of all, it is easily visible that the number rows to print is always the width of a triangle * 2.
And the number of charcters in the pattern, can be calculated by applying the triangle function. Example for width 5:
Number of stars number of dashdot
Row width-abs(row-width) abs(row-width)
1 1 4
2 2 3
3 3 2
4 4 1
5 5 0
6 4 1
7 3 2
8 2 3
9 1 4
And this can be implemented easily now.
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std::string_literals;
int main() {
// Get the max width of the pattern and perform a short input validation
int maxWidth{};
if ((std::cin >> maxWidth) and (maxWidth > 0)) {
// The number of rows for the pattern is dependent on the width. It is a simple relation
const int numberOfRows = 2 * maxWidth;
// Show all rows
for (int row = 1; row < numberOfRows; ++row) {
// Use triangle formular to create star pattern
std::string starPattern(maxWidth - std::abs(row - maxWidth), '*');
// Create dashDot pattern
std::string ddp(std::abs(row - maxWidth), '\0');
std::generate(ddp.begin(), ddp.end(), [i = 0]() mutable { return i++ % 2 ? '.' : '_'; });
// Show output
std::cout << (starPattern+ddp) << '\n';
}
}
else std::cout << "\n*** Error: Invalid input\n\n";
}
Of course you can also create the whole pattern wit std::generate.
Maybe it is too complex for now.
And less understandable.
See:
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
int main() {
// Get the max width of the pattern and perform a short input validation
if (int width{}; (std::cin >> width) and (width > 0)) {
std::vector<std::string> row(2 * width - 1);
std::for_each(row.begin(), row.end(), [&, r = 1](std::string& s) mutable {
s = std::string(width - std::abs(r - width), '*');
std::string ddp(std::abs(r++ - width),'\0');
std::generate(ddp.begin(), ddp.end(), [&, i = 0]() mutable{ return i++ % 2 ? '.' : '_'; });
s += ddp; std::cout << s << '\n'; });
}
else std::cout << "\n*** Error: Invalid input\n\n";
}

Rectangle shape of numerics

for(int width=1; width<=5; width++) {
if(width <= 1) {
for(int width=1; width<=5; width++) {
cout<<" "<<width<<" ";
}
} else if(width<5) {
cout<< endl;
for(int width2=5; width2<=9; width2++) {
if(width2==5 || width2==9)
cout<<" "<<width2<<" ";
else
cout<< " ";
}
} else {
cout<< endl;
for(int width3=13; width3>=9; width3--) {
cout<<" "<<width3<<" ";
}
}
}
this code which I have posted above draws this shape
1 2 3 4 5
5 9
5 9
5 9
13 12 11 10 9
but I actually want my code to print it like this, I have tried a lot changing things but all in vain. so, I'm looking forward to you guys.
1 2 3 4 5
16 6
15 7
14 8
13 12 11 10 9
If you print something on the console, going back in lines and carriage returns will be very messy.
The trick is to seperate the problem in 3 stages:
stage1: print the top line, simple enough
stage2: print the largest number wrapping around, then print some empty space and finish with the number at the end, make sure to increment and decrement the numbers accordingly.
stage3: print the last line.
Here is the code for the algorithm I just described:
#include <iostream>
using namespace std;
int main()
{
const int width=6;
const int height=6;
int numberInFront=(height-1)*2 + (width-1)*2;
int numberAtTheEnd= width;
for(int i=1; i<width; ++i) cout<<i<<"\t"; //print top line
cout<<endl;
for(int i=0; i<height-1; ++i)
{
cout<<numberInFront<<"\t";
for(int j=0; j<width-3; j++) cout<<"\t"; //print inner space
cout<<numberAtTheEnd<<endl;
numberInFront--;
numberAtTheEnd++;
}
//print last line:
int counter = numberInFront;
while(counter!=numberAtTheEnd-1)
{
cout<<counter<<"\t";
counter--;
}
return 0;
}
It helps to avoid magic numbers in your code using #defines or const variables. This makes it more readable and more extensible. For example if you wanted to make a square that was 20x20, your code would require a complete rewrite!
Start from this working solution to implement this principle into your coding.
#include <iostream>
using namespace std;
#define SIDE 4
int main(){
int perimeter = SIDE * 4;
for(int width=0; width<=SIDE; width++)
{
if(width < 1) {
for(int width=0; width <= SIDE; width++) {
cout<<" "<<width + 1<<" ";
}
cout<< endl;
}
else if(width < SIDE)
{
cout<<" "<<perimeter - width + 1 << "\t\t" << (SIDE + width) + 1;
cout<< endl;
}
else
{
for(int width3 = perimeter - SIDE; width3 >= perimeter - 2 * SIDE; width3--) {
cout<<" "<<width3 + 1<<" ";
}
cout<< endl;
}
}
return 0;
}
Here is solution
int width =6;
int total = (width-1)*4;
for(int row=1; row <=width; row++)
{
if(row == 1 )
{
for(int pr=1; pr<=width; pr++)
{
cout<<" "<<pr<<" ";
}
cout<<"\n";
}
else if( row == width)
{
for(int pr=1; pr<=width; pr++)
{
cout<<" "<<(total-row-pr+3)<<" ";
}
}
else
{
for(int pr=1; pr<=width; pr++)
{
if(pr ==1 )
cout<<" "<<(total-row+2)<<" ";
else if(pr ==width)
cout<<" "<<(width+row-1)<<" ";
else
cout<<" "<<" "<<" ";
}
cout<<"\n";
}
}

Algorithm outputs correct answer but programming challenges gives me wrong answer

I'm making an algorithm that simulates a minesweeper game. You have to input the number of rows and columns, followed by the bombs (which are represented by '*') and blank spaces, which are represented by any char.
In the output, you have to print the a matrix which shows '*' where there is a bomb, and the number of bombs on the borders of each blank space. Also, the output has to contain a "Field #x" before showing the resulting matrix, where 'x' is the number of the output
On my algorithm, I'm getting a right result - but when I send it to the online judge, it says the answer is wrong, so I think it might be a formatting issue. Where did I miss? The link for the exercise is right here http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110102&format=html
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
int l;
int c;
int boleano = 0;
int cont = 1;
while (boleano == 0) {
cin >> l >> c;
if (c <= 0 || l <= 0 || l > 100 || c > 100) {
boleano = 1;
break;
}
char matriz[l][c];
char aux[c];
// ESCANEANDO MATRIZ
for (int i=0; i<l; i++) {
cin >> aux;
for (int j=0; j<c; j++){
matriz[i][j] = aux[j];
}
}
int contador[l+2][c+2];
// ZERANDO MATRIZ CONTADORA
for (int i=0; i<l+2; i++) {
for (int j=0; j<c+2; j++) {
contador[i][j] = 0;
}
}
// ACRESCENTANDO VALORES DAS BOMBAS
for (int i=1; i<l+1; i++) {
for (int j=1; j<c+1; j++) {
if (matriz[i-1][j-1] == '*') {
contador[i-1][j-1]++;
contador[i-1][j]++;
contador[i-1][j+1]++;
contador[i][j-1]++;
contador[i][j+1]++;
contador[i+1][j-1]++;
contador[i+1][j]++;
contador[i+1][j+1]++;
}
}
}
// PRINT FINAL
if (cont >1) {
cout << endl;
}
cout << "Field #" << cont << ":" << endl ;
for (int i=1; i<l+1; i++) {
for (int j=1; j<c+1; j++) {
if (matriz[i-1][j-1] == '*') {
cout << matriz[i-1][j-1];
}
else {
cout << contador[i][j];
}
}
cout << endl;
}
cont++;
}
return 0;
}
You could insert an extra introduce an extra boolean which shows whether the current loop is the first loop.
In case it isn't and the current row count isn't equal to 0, you insert the extra new line. In case the formatting issue is because the following format is required:
[Nothing]
Field #1:
*100
2210
1*10
1110
[NewLine]
Field #2:
**100
33200
1*100
[Nothing]
I used the [Nothing] to indicate where there is no (more) new line.

how to create a pyramid using for loop in c++

Hello guys I just want to ask how can I create a triangle using c++?
Actually I have my code but I don't have an idea how to center the first asterisk in the triangle. My triangle is left align. How can I make it a pyramid?
Here's my code below.
#include<iostream>
using namespace std;
int main(){
int x,y;
char star = '*';
char space = ' p ';
int temp;
for(x=1; x <= 23; x++){
if((x%2) != 0){
for(y=1; y <= x ; y++){
cout << star;
}
cout << endl;
}
}
return 0;
}
For a triangle och height Y, then first print Y-1 spaces, followed by an asterisk and a newline. Then for the next line print Y-2 spaces, followed by three asterisks (two more than previously printed) and a newline. For the third line print Y-3 spaces followed by five asterisks (again two more than previous line) and a newline. Continue until you have printed your whole triangle.
Something like the following
int asterisks = 1;
for (int y = HEIGHT; y > 0; --y, asterisks += 2)
{
for (int s = y - 1; s >= 0; --s)
std::cout << ' ';
for (int a = 0; a < asterisks; ++a)
std::cout << '*';
std::cout << '\n';
}
To calculate the number of spaces needed to center each row use this algorithm:
numSpaces = (23 - x) / 2;
and then a for loop to apply the spaces numSpaces times.
Here is the complete code:
#include<iostream>
using namespace std;
int main(){
int x,y;
char star = '*';
char space = ' p ';
int temp;
int numSpaces = 0;
for(x=1; x <= 23; x++){
if((x%2) != 0){
numSpaces = (23 - x) / 2; // Calculate number of spaces to add
for(int i = 0; i < numSpaces; i++) // Apply the spaces
{
cout << " ";
}
for(y=1; y <= x ; y++){
cout << star;
}
cout << endl;
}
}
return 0;
}
And the output:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
One way to do
this is to nest two inner loops, one to print spaces and one to print *(s), inside an outer
loop that steps down the screen from line to line.
#include <iostream>
using namespace std;
int main(){
int row = 5;
for(int i=0; i<row; i++){
for(int j=row; j>i; j--){
cout << " ";
}
for(int k=0; k<2*i+1; k++){
cout << "*";
}
cout << endl;
}
return 0;
}
Output:
*
***
*****
*******
*********
This code is in C#, but you can convert it in c++.
class Program
{
static void Main()
{
int n = 5; // Number of lines to print.
for(int i = 1; i<= n; i++){
//loop for print space in the order (4,3,2,1,0) i.e n-i;
for(int j= 1; j<= n-i; j++){
Console.Write(" ");
}
//loop for print * in the order (1,3,5,7,9..) i.e 2i-1;
for(int k= 1; k<= 2*i-1; k++){
Console.Write("*");
}
Console.WriteLine(); // Next Line.
}
}
}
Here's another solution that doesn't use division or if statements
#include <iostream.h>
int main() {
int height = 17, rowLength, i, j, k;
char symbol = '^';
// print a pyramid with a default height of 17
rowLength = 1;
for (i = height; i > 0; i--) { // print a newline
cout << endl;
for (j = 1; j <= i; j++) // print leading spaces
cout << " ";
for (k = 0; k < rowLength; k++) // print the symbol
cout << symbol;
rowLength = rowLength + 2; // for each row increase the number of symbols to print
}
cout << "\n\n ";
return 0;
}
Star pyramid using for loop only:-
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int main()
{
int n;
cout << "enter the number of rows of pyramid you want : ";
cin >> n;
"\n";
for (int i = 0; i <= n; ++i) {
cout << "\n";
for (int j = 0; j <= n - i; ++j) {
cout << " ";
}
for (int k = 1; k <= i; k++) {
cout << setw(3) << "*";
}
}
return 0;
}
I did that using two loops
here is my code
#include <iostream>
#include <string>
using namespace std;
int main() {
int rows, star, spaces;
int number_of_stars = 5;
int number_of_rows = number_of_stars;
string str1 = "*";
for (rows=1; rows <= number_of_rows; rows++) {
for (spaces=1; spaces <= number_of_stars; spaces++) {
if (spaces==number_of_stars)
{
cout<<str1;
str1+="**";
}
else
cout<<(" ");
}
cout<<("\n");
number_of_stars = number_of_stars - 1;
}
return 0;
}
and the result is
*
***
*****
*******
*********
Url of code on Online compiler
and you can solve it using only one loop, its simple and easy
#include <iostream>
#include <string>
using namespace std;
int main()
{
int numberOfLines=4;
string spaces=string( numberOfLines , ' ' );//this is 4 spaces
string stars="*";
while(spaces!="")
{
cout<<spaces<<stars<<endl;
spaces = spaces.substr(0, spaces.size()-1);
stars+="**";
}
}
Url of code on Online compiler
#include<iostream>
using namespace std;
int for1(int &row);//function declaration
int rows;//global variable
int main()
{
cout<<"enter the total number of rows : ";
cin>>rows;
for1(rows);//function calling
cout<<"just apply a space at the end of the asteric and volla ";
}
int for1(int &row)//function definition
{
for(int x=1;x<=row;x++)//for loop for the lines
{
for(int y=row;y>=x;y--) //for loop for spaces (dynamic loop)
{
cout<<" ";
}
for(int k=1;k<=x*2-x;k++)//for loop for asteric
{
cout<<"* ";/*apply a space and you can turn a reverse right angle triangle into a pyramid */
}
cout<<endl;
}
}

Printing * pattern in C++

I am trying to print a pattern like this
*******
* *
* *
* *
* *
* *
*******
In this it should look like an empty box.
but somehow I am not getting even closer
I coded this so far
#include <iostream>
using namespace std;
int main( int argc, char ** argv ) {
for(int i=1;i<=7;i++)
{
for(int j=1;j<=7;j++)
{
if(j==1||j==7)
printf("*");
else printf(" ");
}
printf("\n");
}
return 0;
}
and my output is
* *
* *
* *
* *
* *
* *
* *
it will be good to have for loop only
if(j==1||j==7)
printf("*");
else printf(" ");
This logic works for all rows except first and last one. So you have to consider row value and make special check for first and last rows. These two do not have spaces.
[Assuming it's a homework, I'm giving just a hint. You almost have done it, above hint should be enough to get this working.]
Your if condition simply needs to be:
if (i==1 || i==7 || j==1 || j==7)
That is, you need to check whether you're on either the first or last rows as well as either the first or last columns, and then print a *.
You are very close. The problem is in this line:
if(j==1||j==7)
Change it so that it also takes into account the top and bottom rows.
Good luck
You need to behave differently during first and last row:
int W = 7, H = 7;
for(int i=0;i<=H;i++)
{
for(int j=0;j<=W;j++)
{
// if row is the first or row is the last or column is the first of column is the last
if (i == 0 || i == H-1 || j == 0 || j == W-1)
printf("*");
else printf(" ");
}
printf("\n");
}
This function will work fine:
#include <iostream>
#include <string>
void printBox(int width, int height) {
if (width < 2 or height < 1) return;
for (int i = 1; i <= height; i++) {
if (i == 1 or i == height) {
std::string o(width, '*');
std::cout << o << std::endl;
} else {
std::string o(width-2, ' ');
o = '*' + o + '*';
std::cout << o << std::endl;
}
}
}
It can be used as:
printBox(2, 2);
which prints:
**
**
Or as:
printBox(6, 4);
which prints:
******
* *
* *
******
Since I'm not expert in programming, I came up with this simple code:
#include <stdio.h>
int main(void)
{
int i,j,k,n;
printf("Enter no of rows : \n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
if(i == 0 || i == (n-1))
{
for(j=0; j <n-1; j++)
printf("*");
}
else
{
for(k=0; k<n; k++)
{
if (k == 0 || k == (n-2))
printf("*");
else
printf(" ");
}
}
printf("\n");
}
return 0;
}
for(int j=1;j<=7;j++)
{
if(j==1||j==7)
printf("*******\n");
else
printf("* *\n");
}
printf("\n");
You are treating all lines equally and ignoring the fact that the first and last line must be handled differently. You need something like
if (i == 1 || i == 7)
{
for (j=1;j<7;j++) printf("*");
printf("\n");
}
else { /* your routine */ }
This is what your code should look like:
#include <iostream>
using namespace std;
int main( int argc, char ** argv ) {
for(int i=1;i<=7;i++)
{
for(int j=1;j<=7;j++)
{
if(j == 1 || j == 7)
printf("*");
else if (i == 1 || i == 7 ) //added this check
printf ("*");
else printf(" ");
}
printf("\n");
}
return 0;
}
Live example
You'll need a nested loop. Because you've two options in between their, you'll need two nested loops. One for spaces and one for the filling '*' in iteration 1 and 7 (0 and 6).
Print the line 1 and 7 with a '*' filling the boundary stars.
Using
if (i == 0 || i == 7) // in-case you're initializing i with 0
// loop for printf ("*");
As others said, you need to handle first and last row. One way to build each line is to make use of one of string constructors, so you don't need to write the inner loop.
#include <string>
#include <iostream>
using namespace std;
int main()
{
int height = 7, width = 7;
for( int i=0; i<height; i++ )
{
char c = (i == 0 || i == height-1) ? '*' : ' ';
string line(width, c);
line[0] = line[width-1] = '*';
cout << line << endl;
}
return 0;
}
#include <iostream>
#include<iomanip>
using namespace std;
main()
{
int Width;
char MyChar;
int LCV; //Loop control
int LCVC, LCVR; //Loop control for LCVC=Columns LCVR=Rows
cout<<"\nEnter Width: "; cin>>Width;
cout<<"\nEnter a Character: "; cin>>MyChar;
for (LCV=1;LCV<=Width;LCV++)
{cout<<MyChar;}
cout<<endl;
for(LCVC=1;LCVC<=Width;LCVC++)
{ cout<<MyChar<<setw(Width-1)<<MyChar<<endl;
}
for (LCV=1;LCV<=Width;LCV++)
{cout<<MyChar;}
cout<<endl;
system("pause");
}
/*
Enter Width: 6
Enter a Character: #
######
# #
# #
# #
# #
# #
# #
######
Press any key to continue . . .
*/
Print this pattern
enter a number=each number for example 23517
**
*
Then
**
*
*****
*
*******
then
** *
* *
***** *
and
*
*
*
*
*
I have come up with a simple solution. You can keep it very simple rather than using char array pointers that you used.
#include <iostream>
using namespace std;
int main() {
int rows = 7;
for(int i = 1; i<=rows;i++){
if(i == 1 || i == rows){
cout<<"*******"<<endl;
}else{
cout<<"* *"<<endl;
}
}
return 0;
} // This code outputs 7 rows with 7 characters in 1st and 7 line each and 1 each character at the start and last position of the remaining rows
But if you want a custom number of rows, you can try out the following code
#include <iostream>
using namespace std;
int main() {
int rows;
cout<<"Enter the number of rows: ";
cin>>rows; // gets input rows from the user
for(int i = 1; i<=rows;i++){
if(i == 1 || i == rows){
cout<<"*******"<<endl;
}else{
cout<<"* *"<<endl;
}
}
return 0;
}
you can add:
`if(i==0 || i==6)
{
cout<<"*";
}`