C++ Star Pattern Pyramid - c++

I'm trying to print the following pattern:
*
* *
* * *
* *
*
.. but I can't figure it out.
This is the logic that I am following.
i variable is for the row number.
j variable is for the column number.
Using for loops to guide the row number and column number.
I am able to create an increasing triangle pattern using the above logic but can't figure out how to start decreasing the pattern to form a pyramid.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int i;
int j;
for (i = 1; i <= 4; i++)
{
for (j = 1; j < i; j++)
{
cout << "*";
}
cout << endl;
for (i; i <= 6; i++)
{
for (j; j <= 0; j--)
{
cout << "*";
}
cout << endl;
}
}
return 0;
}
I'd really appreciate some guidance to this.

for (i; i <= 6; i++)
It has no effect to mention a variable (i) in the init-statement of a for loop if there is wheter a declaration nor an assignment.
#include <iostream>
int main()
{
int width = 8;
// raising flank:
for (int i = 0; i < width; ++i) {
for (int k = 0; k <= i; ++k) {
std::cout << "* ";
}
std::cout.put('\n');
}
// falling flank:
for (int i = width - 1; i; --i) {
for (int k = 0; k < i; ++k) {
std::cout << "* ";
}
std::cout.put('\n');
}
}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

Related

Drawing a Circle with Class in C++

I'm currently writing a program where I have to draw three circle shapes from a class(data structure) that will output in the console.
The problem I'm having with my program is that my code compiles, however, the output goes crazy and doesn't draw the circles.
I'm still new to C++ and if anyone can help me out on how to fix this, I would appreciate it.
My Current Code:
////// Circle.h
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <time.h>
#include <cmath>
using namespace std;
class Circle
{
private:
char type;
int serialNumber = 0;
double radius = 0.0;
double density = 0.0;
public:
Circle(char, int, double);
Circle(char, int, double, double);
~Circle();
void setType(char);
void setSerialNumber(int);
void setRadius(double);
void setDensity(double);
char getType() const;
int getSerialNumber() const;
double getRadius() const;
double getDensity() const;
};
////// Circle.cpp
// #include "Circle.h"
Circle::Circle(char c, int s, double r)
{
type = c;
serialNumber = s;
radius = r;
}
Circle::Circle(char c, int s, double r, double d)
{
type = c;
serialNumber = s;
radius = r;
density = d;
}
Circle::~Circle()
{
cout << "Shapes deleted!" << endl;
}
void Circle::setType(char c)
{
if(c == 'S' || c == 'C')
type = c;
}
void Circle::setSerialNumber(int s)
{
if(s > 0)
serialNumber = s;
}
void Circle::setRadius(double r)
{
if(r > 0)
radius = r;
}
void Circle::setDensity(double d)
{
if(d > 0)
density = d;
}
char Circle::getType() const
{
return type;
}
int Circle::getSerialNumber() const
{
return serialNumber;
}
double Circle::getRadius() const
{
return radius;
}
double Circle::getDensity() const
{
return density;
}
////// main.cpp
// #include "Circle.h"
void drawAll(Circle *[], int);
void drawType(Circle *);
void drawCircle(Circle *);
void drawSpray(Circle *);
void deleteAll(Circle *[], int);
/// For 'C' type the system should display a circle.
/// For 'S' type the system displays a spray pattern just like those used in Microsoft Paint.
int main(int argc, char** argv)
{
const int SIZE = 3;
Circle * arrCircle[SIZE] = {nullptr};
arrCircle[0] = new Circle('C', 1001, 20);
/// Create a Circle whose serial number is 1001 and the radius is 20.
/// Type 'C' indicates Circle type.
arrCircle[1] = new Circle('S', 1002, 25, 30);
/// Create a Spray whose serial number is 1002, the radius is 25, and the density is 30%.
/// Type 'S' indicates Spray type.
arrCircle[2] = new Circle('S', 1003, 40, 80);
/// Create a Spray whose serial number is 1003, the radius is 40, and the density is 80%.
drawAll(arrCircle, SIZE);
/// Draw all shapes. The function uses a for loop to display the circles and sprays in arrCircle.
deleteAll(arrCircle, SIZE);
/// Delete all shapes.
return 0;
}
void drawAll(Circle *arr[], int SIZE)
{
for(int i = 0; i < SIZE; i++)
if(arr[i] != nullptr)
{
cout << "Circle #" << arr[i]->getSerialNumber() << endl;
drawType(arr[i]);
}
}
void drawType(Circle *p)
{
if(p->getType() == 'C')
drawCircle(p);
else if(p->getType() == 'S')
drawSpray(p);
}
void drawCircle(Circle *p)
{
double r = p->getRadius();
int x = 0;
int y = 0;
int rto = 2;
for(int i = 0; i <= 40; i++)
{
for(int j = 0; j <= 40; i++)
{
x = abs(i - 20);
y = abs(j - 20);
r = pow(pow(x, rto) + pow(y, rto), 0.5);
if(19.5 < r && r < 20.5)
cout << "* ";
else
cout << " ";
}
cout << endl;
}
}
void drawSpray(Circle *p)
{
double d = p->getDensity();
int x = 0;
int y = 0;
int rto = 2;
for(int i = 0; i <= 80; i++)
{
for(int j = 0; j <= 80; i++)
{
x = abs(i - 30);
y = abs(j - 30);
d = pow(pow(x, rto) + pow(y, rto), 0.5);
if(19.5 < d && d < 20.5)
cout << "* ";
else
cout << " ";
}
cout << endl;
}
}
void deleteAll(Circle *arr[], int SIZE)
{
for(int i = 0; i < SIZE; i++)
{
if(arr[i] != nullptr)
delete arr[i];
}
}
My Current Output:
Circle #1001
* * * * * * * * *
Expected Output: (example)
Circle #1001
*************
** **
** **
* *
** **
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
** **
* *
** **
** **
*************
Circle #1002
***************
*** ***
** **
* *
* *
** **
* *
* *
* *
** **
* *
* *
* *
* *
* *
* *
** **
* *
* *
* *
** **
* *
* *
** **
*** ***
***************
Circle #1003
*****************
*** ***
** **
* *
** **
* *
* *
* *
** **
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
** **
* *
* *
* *
** **
* *
** **
*** ***
*****************
Here is a simple algorithm it may be helpful with simple mathematics
#include <iostream>
#include <graphics.h>
int main() {
initgraph();
setcolorRGB(23, 143, 44);
int j = 400;
int r = 50; // this is the radius
for (int i = 200;i < 800;i++) {
for (int j = 200;j < 800;j++) {
int p = int(sqrt((pow(i-350,2))+(pow(j-350,2)))); // this is the distance between two points
if (r == p) {
putpixel(j,i);
}
}
}
}

Modify to have a configurable number of stars "*"

I manage to print out the output for the pattern of stars I wanted but now I want to have a configurable number of stars "*" and automatically calculated corresponding number of rows.
I have tried several ways but the output seems to be off. If there is better way to display the output please guide me.
int i, j, k,l;
k = 1;
l = 11;
for (i = 0; i < 6; i++)
{
for (j = 0; j < k ; j++)
{
cout << "* ";
}
cout << "- ";
k += 2;
for (j = 0; j < l; j++)
{
cout << "* ";
}
l -= 2;
cout << endl;
}
* - * * * * * * * * * * *
* * * - * * * * * * * * *
* * * * * - * * * * * * *
* * * * * * * - * * * * *
* * * * * * * * * - * * *
* * * * * * * * * * * - *
If the number of rows is given, then the resulting number of columns can easily be calculated. Look at your pattern then you will see that the number of necessary coulmns is number of rows times 2 + 1.
Here one possible solution:
#include <iostream>
int main()
{
std::cout << "Enter the number of rows for the pattern: ";
unsigned int numberOfRows{ 0 };
std::cin >> numberOfRows;
// Number of columns is always number of rows * 2 + 1
unsigned int numberOfColumns{ numberOfRows * 2 + 1 };
unsigned int positionOfDash{ 1 };
// Print the pattern
for (unsigned int row = 0; row < numberOfRows; ++row) {
for (unsigned int col = 0; col < numberOfColumns; ++col) {
// Output dash in desired column or else star
std::cout << (col == positionOfDash ? '-' : '*') << ' ';
}
positionOfDash += 2;
std::cout << '\n';
}
return 0;
}
Please note: Of course there are tons of other possible solutions . . .

if statement runtime error

I originally had 3 equations: Pu, Pm & Pd. It ran fine.
Once I introduced the if statement, with variations on the 3 equations, depending on the loop iteration, I receive a runtime error.
Any help would be appreciated.
Cheers in advance.
#include <cmath>
#include <iostream>
#include <vector>
#include <iomanip>
int Rounding(double x)
{
int Integer = (int)x;
double Decimal = x - Integer;
if (Decimal > 0.49)
{
return (Integer + 1);
}
else
{
return Integer;
}
}
int main()
{
double a = 0.1;
double sigma = 0.01;
int delta_t = 1;
double M = -a * delta_t;
double V = sigma * sigma * delta_t;
double delta_r = sqrt(3 * V);
int count;
double PuValue;
double PmValue;
double PdValue;
int j_max;
int j_min;
j_max = Rounding(-0.184 / M);
j_min = -j_max;
std::vector<std::vector<double>> Pu((20), std::vector<double>(20));
std::vector<std::vector<double>> Pm((20), std::vector<double>(20));
std::vector<std::vector<double>> Pd((20), std::vector<double>(20));
std::cout << std::setprecision(10);
for (int i = 0; i <= 2; i++)
{
count = 0;
for (int j = i; j >= -i; j--)
{
count = count + 1;
if (j = j_max) // Exhibit 1C
{
PuValue = 7.0/6.0 + (j * j * M * M + 3 * j * M)/2.0;
PmValue = -1.0/3.0 - j * j * M * M - 2 * j * M;
PdValue = 1.0/6.0 + (j * j * M * M + j * M)/2.0;
}
else if (j = j_min) // Exhibit 1B
{
PuValue = 1.0/6.0 + (j * j * M * M - j * M)/2.0;
PmValue = -1.0/3.0 - j * j * M * M + 2 * j * M;
PdValue = 7.0/6.0 + (j * j * M * M - 3 * j * M)/2.0;
}
else
{
PuValue = 1.0/6.0 + (j * j * M * M + j * M)/2.0;
PmValue = 2.0/3.0 - j * j * M * M;
PdValue = 1.0/6.0 + (j * j * M * M - j * M)/2.0;
}
Pu[count][i] = PuValue;
Pm[count][i] = PmValue;
Pd[count][i] = PdValue;
std::cout << Pu[count][i] << ", ";
}
std::cout << std::endl;
}
return 0;
}
You are assigning instead of checking for equal: j_max to j in your if statements.
if (j = j_max)
// ^
else if (j = j_min)
// ^
Change if (j = j_max) to if (j == j_max),
And else if (j = j_min) to else if (j == j_min).
Correct the following if conditional check and all other instances of an if check
if(j=j_max)
with
if (j == j_max)
you are checking for an equality not assigning.
Your code was going into an infinite loop.

Diamonds of triangle in C++

I just understood the concept of making a triangle in C++ that is made of asterisks.
Now that I tried to replace those asterisks by "diamonds of asterisks", I found a very
logic error and that is the "newline" and I can't find it anymore, can anyone help me
with my code?
I want my output to be like a triangle with asterisks, but the asterisks is substituted by asterisks of diamonds.
#include<iostream>
using namespace std;
int main()
{
int number, space1, space2, space3;
int i, j, x, y, z;
cout << "Enter any number: ";
cin >> number;
space1 = (2*number)-1;
space2 = number-1;
space3 = space1*space2;
z = number-1;
for(i = 1; i <= number; i++)
{
for(j = 1; j <= (2*i)-1; j++){
for (x = 1; x <= number; x++)
{
for(y = 1; y <= space3; y++)
{
cout << " ";
}
for(y = 1; y <= number-x; y++)
{
cout << " ";
}
for(y = 1; y <= (2*x)-1; y++)
{
cout << "*";
}
for(y = 1; y <= z; y++)
{
cout << " ";
}
z--;
if(x <= number)
{
cout << endl;
}
}
if(z >= 3)
{
z = 1;
}
for(x = 1; x <= number-1; x++)
{
for(y = 1; y <= space3; y++)
{
cout << " ";
}
for(y = 1; y <= x; y++)
{
cout << " ";
}
for(y = 2*(number-x)-1; y >= 1; y--)
{
cout << "*";
}
for(y = 1; y <= z; y++)
{
cout << " ";
}
z++;
if(x <= number)
{
cout << endl;
}
}
}
space3 -= space1;
}
}
Not sure what you’re really asking (asterisks that are diamonds?) – some example of the desired output could have helped! – but I like this program:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
for( int y = 0; y < 32; ++y )
{
cout << string( 32 - y, ' ' );
for( int x = 0; x < 32; ++x )
cout << (x & ~y? ' ' : '*') << ' ';
cout << endl;
}
}
Output:
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Outputting a Square Shape C++ using '*'

Hello I am very new to programming and my assignment is to output shapes. The first is the square:
int main(){
unsigned size;
cout <<"Size: ? ";
cin >>size;
for ( unsigned r = 0; r < size; r++ ){ // Square
for ( unsigned c = 0; c < size ; c++ )
if ( r == c )
cout <<'*';
cout <<endl;
}
cout <<endl;
}
When I input "5" after being prompted. The output results in:
5
*
*
*
*
*
Can anyone explain what is wrong with my code? I need to have both horizontal and vertical outputs. Thank you
You are only outputting a * on the diagonal, when r is the same as c. And you output nothing else but some endlines, so you end up with just a single star on each line.
#include <iostream>
using namespace std;
int main(){
unsigned size;
bool solid = true; //solid or hollow shape?
cout <<"Size: ? ";
cin >>size;
size = 5;
cout << endl;
for ( unsigned r = 0; r < size; r++ ){ // Square
for ( unsigned c = 0; c < size ; c++ ){
if(solid){
cout << " * ";
}
else{
if(r == 0 || r == size-1 || c == 0 or c == size-1){
cout << " * ";
}
else{
cout << " ";
}
}
}
cout <<endl;
}
cout <<endl;
}
Output Hollow:
* * * * *
* *
* *
* *
* * * * *
Solid:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Only time the * is printed is when r == c. What's the purpose of the if statement?
Try commenting the if statement and see the result.