How to fill in the gaps of this cosine graph - c++

#include <math.h>
#include <iostream>
using namespace std;
void graph(){
int x=140;
int y=20;
double convert = 3.141592/180;
cout<<"amplitude, frequency\n";
double amp,freq;
cin>>amp>>freq;
for(int i=0; i<y; i++){
for(int j=0; j<x; j++){
if(i==nearbyint(3*amp*cos(5*freq*j*convert))+10){
cout<<"*";
}
else if(i==y/2){
cout<<"-";
}
else if(j==x/2){
cout<<"|";
}
else{
cout<<" ";
}
}
cout<<endl;
}
}
int main(){
graph();
return 0;
}
when the code is run, the graph will do fine until you start inputting numbers above 4, where then the line will start having large spaces in between the *'s in the y axis.
What is the simplest way I can fill in the gaps between the *s
The equation is multiplied with 3 and 5 for formatting purposes.

I can think of some ways to do this.
The easiest way is just to check against the interval (j-0.5,j+0.5) by replacing the test
if(i==nearbyint(3*amp*cos(5*freq*j*convert))+10){
cout<<"*";
}
with
void graph( double amp, double freq ){
int x=140;
int y=20;
double convert = 3.141592/180;
for(int i=0; i<y; i++){
for(int j=0; j<x; j++){
double jx = j;
double v1 = 3*amp*cos(5*freq*(jx-0.5)*convert)+10;
double v2 = 3*amp*cos(5*freq*(jx+0.5)*convert)+10;
if( ((i>=v1)&&(i<=v2)) || ((i<=v1)&&(i>=v2))){
cout<<"*";
}
else if(i==y/2){
cout<<"-";
}
else if(j==x/2){
cout<<"|";
}
else{
cout<<" ";
}
}
cout<<endl;
}
}
Godbolt Link: https://godbolt.org/z/G47WjqafP
This results in
Program stdout
|
|
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * * *
* * * * * * * * * * | * * * * * * * * *
* * * * * * * * * * | * * * * * * * * *
* * * * * * * * * * | * * * * * * * * *
----*------*------*------*------*-------*------*------*------*------*-------*------*------*------*------*-------*------*------*------*------
* * * * * * * * * *| * * * * * * * * *
* * * * * * * * * *| * * * * * * * * *
* * * * * * * * * *| * * * * * * * * *
* * * * * * * * * *| * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * |* * * * * * * * * *
|
But the coolest I could find for a 10 minute Blitz code project is an approach where you can use something like the Bresenham line algorithm. Bresenham will guarantee that the space between two adjacent point while drawing a line is maximum one pixel, which is exactly what you are asking.
I could apply Bresenham straight to your algorithm but why not extend this to make it more generic? I'm already wasted so here we go...
First you need to define a viewport for your graph that will map between fictitious coordinates to screen coordinates.
struct Extents {
double x[2];
double y[2];
};
Second you will need a class to hold a virtual screen with a character buffer inside and some way to clear it up.
This map will contain characters that will be printed out later.
template< int WIDTH, int HEIGHT >
struct Graph {
Extents ext;
char map[WIDTH][HEIGHT];
void clear() {
memset( map, ' ', WIDTH*HEIGHT );
}
void print() {
for ( int i=0; i<HEIGHT; ++i ) {
for ( int j=0; j<WIDTH; ++j ) {
std::cout << map[j][HEIGHT-i-1];
}
std::cout << std::endl;
}
}
};
Then you need some methods to convert from viewport to screen and vice versa
double from_screen_x( int j ) {
return ext.x[0] + (j*(ext.x[1]-ext.x[0]))/WIDTH;
}
int from_viewport_x( double x ) {
return (x-ext.x[0])/(ext.x[1]-ext.x[0])*WIDTH;
}
int from_viewport_y( double y ) {
return HEIGHT*((y-ext.y[0])/(ext.y[1]-ext.y[0]));
}
Then once we have a way to convert from (x,y) to (i,j) screen coordinates, we can easily 'plot' a single point with a given character.
void point( int x, int y, char ch ) {
if ( (x>=0) && (x<WIDTH) ) {
if ( (y>=0) && (y<HEIGHT) ) {
map[x][y] = ch;
}
}
}
Now the workhorse of the entire class - the Bresenham algorithm which you can find on Wikipedia
void line( int x0, int y0, int x1, int y1, char ch ) {
int dx = abs(x1 - x0);
int sx = x0 < x1 ? 1 : -1;
int dy = - int( abs(y1 - y0) );
int sy = y0 < y1 ? 1 : -1;
int error = dx + dy;
while( true ) {
point(x0, y0, ch);
if ( (x0 == x1) && (y0 == y1) ) break;
int e2 = 2 * error;
if ( e2 >= dy ) {
if (x0 == x1) break;
error = error + dy;
x0 = x0 + sx;
}
if (e2 <= dx) {
if (y0 == y1) break;
error = error + dx;
y0 = y0 + sy;
}
}
}
Then we need the high level functions: one to draw the axis and another to plot the chart.
The axis is easy - just find the screen coordinates of the origin (0,0) and plot it on our 'map'.
void axis() {
int i = from_viewport_y(0);
for ( int j=0; j<WIDTH; ++j ) {
point(j,i,'-');
}
int j = from_viewport_x(0);
for ( int i=0; i<HEIGHT; ++i ) {
point(j,i,'|');
}
}
For the chart algorithm I decided to go fancy and using a templated function that will allow us to externalize the actual computation.
This algorithm will sweep the x axis in screen space, map into the viewport, compute the point in virtual coordinates and bring them back to the actual (i,j) screen coordinates. Once we have obtained the first point we can from the 2nd point call the line algorithm between the old and the new point and keep iterating until the end.
template< typename Fn >
void chart( char ch, Fn&& fn ) {
int lasti = 0;
for ( int j=0; j<WIDTH; ++j ) {
double x = from_screen_x( j );
int i = from_viewport_y( fn(x) );
if ( j>0 ) {
line( j-1,lasti, j, i, ch );
}
lasti = i;
}
}
Now we are left with just the business logic. Let's create a cosine functor that will give us a cosine function with a given amplitude and frequency.
struct CosFn {
double amp;
double freq;
double operator()( double x ) {
return amp*cos( freq*x );
}
};
Now we just need to put everything together and plot it all
int main() {
Extents ext{{-1,1},{-1,1}};
Graph<120,20> g{ext};
g.clear();
g.axis();
g.chart( '.', CosFn{1,2} );
g.chart( '^', CosFn{0.75,4} );
g.chart( '*', CosFn{0.5,8} );
g.print();
}
This will produce:
Program stdout
.............|.............
...... | ......
.... ^^^^^^^^^^^ ....
.... ^^^^ | ^^^^ ....
.... ^^^ * ^^^ ....
********* ... ^^^ ****|**** ^^^ ... *********
*** ** ... ^^ ** | ** ^^ ... ** ***
* .**. ^^ ** | ** ^^ .**. *
** ... ** ^^ ** | ** ^^ ** ... **
--*----------...--------*------------^^----------*----------|----------*----------^^------------*--------...----------*-
** ... ** ^^ ** | ** ^^ ** ... *
... * ^^ * | * ^^ * ...
... ** ^^ ** | ** ^^ ** ...
... ^** ** | ** **^ ...
^ ^^^ ********** | ********** ^^^
^^^ ^^^ | ^^^ ^^^
^^^^ ^^^^ | ^^^^ ^^^^
^^^^^^^^^^^ | ^^^^^^^^^^^
|
|
Godbolt link: https://godbolt.org/z/P3Wx1vE7s

Related

Spline Catmull-Rom for image zooming using C++ and opencv

I'm trying to implement spline Catmull-Rom for image zooming using C++ and OpenCV.
I performed two tests, the first is image zooming (X2), and the second image reconstruction (zooming image decimated).
My problem is that in the image interpolated appear some white and black pixel (image1) when I displayed the value of pixels I found that white pixels have a negative value and the black one has a value greater than 255, also the image reconstructed appear blurred (image2 and image3).
float CalCurveInt(float t, float p0, float p1, float p2, float p3)
{
float t2 = t * t;
float t3 = t2 * t;
float x = 0.5f * ((2.0f * p1) +
(-p0 + p2) * t +
(2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
return x;
}
Mat CalcCatmull(Mat &src, int zoom)
{
int v1, v2, v3, v4, Ptr, Xmax, Ymax;
float Result, t, c1, c2, c3, c4;
//------------------------------------------------------------
Xmax = src.cols;
Ymax = src.rows;
Size srcSize(zoom*Xmax, Ymax);
Mat dst(srcSize, CV_8UC1);
for (int j = 0; j < Ymax; j++)
{
Ptr = 0;
for (int i = 0; i < Xmax; i++)
{
v1 = i - 1; v2 = i; v3 = i + 1; v4 = i + 2;
if (i - 1 < 0) v1 = 0;
if (Xmax <= i + 1) v3 = Xmax - 1;
if (Xmax <= i + 2) v4 = Xmax - 1;
for (double J = 1; J <= zoom; J++)
{
t = J / zoom;
Result = 0.0;
c1 = src.at<uchar>(j, v1);
c2 = src.at<uchar>(j, v2);
c3 = src.at<uchar>(j, v3);
c4 = src.at<uchar>(j, v4);
Result = CalCurveInt(t, c1, c2, c3, c4);
dst.at<uchar>(j, Ptr) = abs(Result);
Ptr++;
}
}
}
//------------------------------------------------
Xmax = dst.cols;
Ymax = dst.rows;
Size srcSize1(Xmax, zoom*Ymax);
Mat dest(srcSize1, CV_8UC1);
for (int i = 0; i < Xmax; i++)
{
Ptr = 0;
for (int j = 0; j < Ymax; j++)
{
v1 = j - 1; v2 = j; v3 = j + 1; v4 = j + 2;
if (j - 1 < 0) v1 = 0;
if (Ymax <= j + 1) v3 = Ymax - 1;
if (Ymax <= j + 2) v4 = Ymax - 1;
for (double J = 1; J <= zoom; J++)
{
t = J / zoom;
Result = 0.0;
c1 = dst.at<uchar>(v1, i);
c2 = dst.at<uchar>(v2, i);
c3 = dst.at<uchar>(v3, i);
c4 = dst.at<uchar>(v4, i);
Result = CalCurveInt(t, c1, c2, c3, c4);
dest.at<uchar>(Ptr, i) = Result;
Ptr++;
}
}
}
return dest;
}
float zoom = 2.0;
int main()
{
Mat src = imread("fruits.png", CV_LOAD_IMAGE_GRAYSCALE);
int width = src.cols;
int hight = src.rows;
/*Image Decimation*/
Size srcdSize(int(width / zoom), int(hight / zoom));
Mat srcd;
pyrDown(src, srcd, srcdSize);
imshow("decimation", srcd);
Mat dst = CalcCatmull(srcd, zoom);
imshow("Image Source", src);
imshow("Image dest", dst);
imwrite("Image dest.png", dst);
waitKey(0);
return 0;
}
Thanks in advance.
My old implementation, seems it worked fine.
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdarg.h>
#include "opencv2/opencv.hpp"
#include "fstream"
#include "iostream"
using namespace std;
using namespace cv;
//-----------------------------------------------------------------------------------------------------
// Take 2 points, compute values between p1 and p2, p0 and p3 need for tangents computation
// on the bouunds. Parameter t - changes in range 0 to 1 (0 - we are in p1, 1 - we are in p2)
//-----------------------------------------------------------------------------------------------------
void PointOnCurve(Point2f &out, float t, Point2f p0, Point2f p1, Point2f p2, Point2f p3)
{
float t2 = t * t;
float t3 = t2 * t;
out.x = 0.5f * ( ( 2.0f * p1.x ) + ( -p0.x + p2.x ) * t +
( 2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x ) * t2 +
( -p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x ) * t3 );
out.y = 0.5f * ( ( 2.0f * p1.y ) + ( -p0.y + p2.y ) * t +
( 2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y ) * t2 +
( -p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y ) * t3 );
}
//-----------------------------------------------------------------------------------------------------
// interpolation of 4х4 patch
//
// S * S * S * S
// * * * * * * *
// S * S * S * S
// * * * * * * *
// S * S * S * S
// * * * * * * *
// S * S * S * S
//
// S- pixels of source imgage
//
// sequentially take 2 middle columns and computte D.
//
// S * 1 * 2 * S
// * * * * * * *
// S * 1 * 2 * S
// * * D * D * *
// S * 1 * 2 * S
// * * * * * * *
// S * 1 * 2 * S
//
// same for rows and we will have F
//
// S * S * S * S
// * * * * * * *
// 3 * 3 F 3 * 3
// * * D * D * *
// 4 * 4 F 4 * 4
// * * * * * * *
// S * S * S * S
//
// then compute diagonals and after averafing with neihbours will find С
//
// 1 * S * S * 2
// * * * * * * *
// S * 1 F 2 * S
// * * D C D * *
// S * 2 F 1 * S
// * * * * * * *
// 2 * S * S * 1
//-----------------------------------------------------------------------------------------------------
void PointOnSurface(Mat& src,Mat& dst)
{
float t=0.5;
Point2f out;
dst=Mat(3,3,CV_32FC1);
// Угловые точки результата совпадают с точками центральной ячейки исходного патча
dst.at<float>(0,0)=src.at<float>(1,1);
dst.at<float>(2,0)=src.at<float>(2,1);
dst.at<float>(0,2)=src.at<float>(1,2);
dst.at<float>(2,2)=src.at<float>(2,2);
Point2f p0;
Point2f p1;
Point2f p2;
Point2f p3;
p0.x=0;p0.y=src.at<float>(0,1);
p1.x=1;p1.y=src.at<float>(1,1);
p2.x=2;p2.y=src.at<float>(2,1);
p3.x=3;p3.y=src.at<float>(3,1);
PointOnCurve(out,t,p0,p1,p2,p3);
dst.at<float>(1,0)=out.y;
p0.x=0;p0.y=src.at<float>(0,2);
p1.x=1;p1.y=src.at<float>(1,2);
p2.x=2;p2.y=src.at<float>(2,2);
p3.x=3;p3.y=src.at<float>(3,2);
PointOnCurve(out,t,p0,p1,p2,p3);
dst.at<float>(1,2)=out.y;
p0.x=0;p0.y=src.at<float>(1,0);
p1.x=1;p1.y=src.at<float>(1,1);
p2.x=2;p2.y=src.at<float>(1,2);
p3.x=3;p3.y=src.at<float>(1,3);
PointOnCurve(out,t,p0,p1,p2,p3);
dst.at<float>(0,1)=out.y;
p0.x=0;p0.y=src.at<float>(2,0);
p1.x=1;p1.y=src.at<float>(2,1);
p2.x=2;p2.y=src.at<float>(2,2);
p3.x=3;p3.y=src.at<float>(2,3);
PointOnCurve(out,t,p0,p1,p2,p3);
dst.at<float>(2,1)=out.y;
// diagonals
// 1
p0.x=0;p0.y=src.at<float>(0,0);
p1.x=1;p1.y=src.at<float>(1,1);
p2.x=2;p2.y=src.at<float>(2,2);
p3.x=3;p3.y=src.at<float>(3,3);
PointOnCurve(out,t,p0,p1,p2,p3);
float d1=out.y;
// 2
p0.x=0;p0.y=src.at<float>(3,0);
p1.x=1;p1.y=src.at<float>(2,1);
p2.x=2;p2.y=src.at<float>(1,2);
p3.x=3;p3.y=src.at<float>(0,3);
PointOnCurve(out,t,p0,p1,p2,p3);
float d2=out.y;
// averaging
dst.at<float>(1,1)=1.0/6.0*(d1+d2+dst.at<float>(0,1)+dst.at<float>(1,0)+dst.at<float>(1,2)+dst.at<float>(2,1));
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------
void Scale2Times(Mat& src_img,Mat &dstImg)
{
Mat imgf,img;
Mat dst;
Mat src;
src_img.convertTo(imgf,CV_32FC1,1.0/255.0);
cv::copyMakeBorder(imgf,img,1,1,1,1,cv::BORDER_REFLECT);
dstImg=Mat(src_img.rows*2,src_img.cols*2,CV_32FC1);
for(int i=0;i<img.rows-4;i++)
{
for(int j=0;j<img.cols-4;j++)
{
img(Rect(j,i,4,4)).copyTo(src);
PointOnSurface(src,dst);
dst.copyTo(dstImg(Rect(2*j+1,2*i+1,3,3)));
}
}
dstImg=dstImg(Rect(0,0,dstImg.cols-2,dstImg.rows-2)).clone();
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
int main( int argc, char** argv )
{
namedWindow("Src");
namedWindow("cvResize");
namedWindow("Catmul-Rom");
Mat Img=imread("C:\\ImagesForTest\\1.tiff",0);
imshow("Src",Img);
Mat dstImg;
Scale2Times(Img,dstImg);
imshow("Catmul-Rom",dstImg);
Mat ImgLin(Img.rows*2,Img.cols*2,CV_8UC1);
cv::resize(Img,ImgLin,Size(Img.cols*2,Img.rows*2),INTER_CUBIC);
imshow("cvResize",ImgLin);
waitKey(0);
//getchar();
return 0;
}

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

format properly output using setw()

Is there any other way of doing this? if section of this code make it work but is there any way of using setw() to organize (*) properly?
void showTheater(char theater[][20],int row,int seat)
{
cout << "Seats: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19" << endl;
for (int i = 0; i <= row; i++)
{
for (int j = 0; j <= seat; j++)
{
if (j == 0) {
cout << "Row " << i;
}
else if (i < 10) {
cout << setw(3) << theater[row][seat];
}
else {
cout <<" " << theater[row][seat]<<" ";
}
}
cout << "\n";
}
}
output without writing if section:
Seats: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Row 0 * * * * * * * * * * * * * * * * * * * *
Row 1 * * * * * * * * * * * * * * * * * * * *
Row 2 * * * * * * * * * * * * * * * * * * * *
Row 3 * * * * * * * * * * * * * * * * * * * *
Row 4 * * * * * * * * * * * * * * * * * * * *
Row 5 * * * * * * * * * * * * * * * * * * * *
Row 6 * * * * * * * * * * * * * * * * * * * *
Row 7 * * * * * * * * * * * * * * * * * * * *
Row 8 * * * * * * * * * * * * * * * * * * * *
Row 9 * * * * * * * * * * * * * * * * * * * *
Row 10 * * * * * * * * * * * * * * * * * * * *
Row 11 * * * * * * * * * * * * * * * * * * * *
Row 12 * * * * * * * * * * * * * * * * * * * *
Row 13 * * * * * * * * * * * * * * * * * * * *
Row 14 * * * * * * * * * * * * * * * * * * * *
Row 15 * * * * * * * * * * * * * * * * * * * *
output with if section:
Seats: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Row 0 * * * * * * * * * * * * * * * * * * * *
Row 1 * * * * * * * * * * * * * * * * * * * *
Row 2 * * * * * * * * * * * * * * * * * * * *
Row 3 * * * * * * * * * * * * * * * * * * * *
Row 4 * * * * * * * * * * * * * * * * * * * *
Row 5 * * * * * * * * * * * * * * * * * * * *
Row 6 * * * * * * * * * * * * * * * * * * * *
Row 7 * * * * * * * * * * * * * * * * * * * *
Row 8 * * * * * * * * * * * * * * * * * * * *
Row 9 * * * * * * * * * * * * * * * * * * * *
Row 10 * * * * * * * * * * * * * * * * * * * *
Row 11 * * * * * * * * * * * * * * * * * * * *
Row 12 * * * * * * * * * * * * * * * * * * * *
Row 13 * * * * * * * * * * * * * * * * * * * *
Row 14 * * * * * * * * * * * * * * * * * * * *
Row 15 * * * * * * * * * * * * * * * * * * * *
The key is to use std::setw (in conjunction with std::left) for the index of the row. Here is the code. Note that now the stars are also aligned with the headers. In the posted code, there was also an error on the indices of theater.
void showTheater(char theater[][20], int row, int seat) {
std::vector<int> Seats (seat);
for (int i = 0; i < seat; i++) Seats[i] = i;
std::cout << "Seats:";
for (int i = 0; i < seat; i++) std::cout << std::setw(3) << Seats[i] << " ";
std::cout << "\n";
for (int i = 0; i < row; i++) {
std::cout << "Row " << std::setw(2) << std::left << i << " ";
for (int j = 0; j < seat; j++) {
std::cout << " " << theater[i][i] << " ";
}
std::cout << "\n";
}
}
EDIT: I just discovered that HolyBlackCat already mentioned the way to align the stars. Sorry. At least I also aligned the Headers and suppressed the useless setwfor printing the chars.

C++ for loop stops before reaching end and restarts

I've been poking my nose into working with arrays in c++ and I've been writing a 1D Euler solver code that I wrote in matlab and converting it to c++ as a practice exercise.
This issue is that this for loop is supposed to run until the counter i reaches N_cells-1 but no matter how high I set the number, it always gets to 57, then restarts from 2 and continues doing this until I click on the output screen. I also ran the code with an N_cells number less than 57 and I get an error code which I've included below.
I'm pretty new to arrays and header files in c++ so I'm sure it's something simple, but I just can't find it. I know it's related to the fqL array but I don't know what.
Error when number <57 is used:
#include "stdafx.h"
#include "Flux.h"
#include <iostream>
#include <chrono>
using namespace std;
void Flux(double * q, double y, double R, int N_cells,double * Flux)
{
double qL[3];
double qR[3];
for (int i = 0; i < N_cells - 1; i++) {
//Initialize left and right sides
//-------------------
qL[0] = q[0, i];
qL[1] = q[1, i];
qL[2] = q[2, i];
qR[0] = q[0, i + 1];
qR[1] = q[1, i + 1];
qR[2] = q[2, i + 1];
//-------------------
//Calculate Left side Parameters
//-------------------
double PL;
//double fqL[3];
double cL2;
double HL;
double uL;
PL = (y - 1)*(qL[2] - 0.5 / qL[0] * (qL[1] * qL[1]));
double fqL[3] = { qL[1],
(3 - y)*0.5*(qL[1] * qL[1]) / qL[0] + (y - 1)*qL[2],
y*qL[1] * qL[2] / qL[0] - (y - 1)*0.5*(qL[1] * qL[1] * qL[1]) / (qL[0] * qL[0]) };
cL2 = y * (y - 1)*(qL[2] / qL[0] - 0.5*(qL[1] / qL[0])*(qL[1] / qL[0]));
HL = 0.5*(qL[1] / qL[0])*(qL[1] / qL[0]) + cL2 / (y - 1);
uL = qL[1] / qL[0];
//Calculate Right side Parameters
//-------------------
double PR;
//double fqR[3];
double cR2;
double HR;
double uR;
PR = (y - 1)*(qR[2] - 0.5 / qR[0] * (qR[1] * qR[1]));
double fqR[3] = { qR[1],
(3 - y)*0.5*(qR[1] * qR[1]) / qR[0] + (y - 1)*qR[2],
y*qR[1] * qR[2] / qR[0] - (y - 1)*0.5*(qR[1] * qR[1] * qR[1]) / (qR[0] * qR[0]) };
cR2 = y * (y - 1)*(qR[2] / qR[0] - 0.5*(qR[1] / qR[0])*(qR[1] / qR[0]));
HR = 0.5*(qR[1] / qR[0])*(qR[1] / qR[0]) + cR2 / (y - 1);
uR = qR[1] / qR[0];
//-------------------
//Calculate Roe's Variables
//-------------------------------- -
double u;
double H;
double c;
double rho;
u = (sqrt(qL[1])*qL[2] / qL[1] + sqrt(qR[1])*qR[2] / qR[1]) / (sqrt(qL[1]) + sqrt(qR[1]));
H = (sqrt(qL[1])*HL + sqrt(qR[1])*HR) / (sqrt(qL[1]) + sqrt(qR[1]));
c = sqrt((y - 1)*(H - 0.5*u *u));
rho = sqrt(qL[1] * qR[1]);
//-------------------------------- -
//-------------------------------- -
double g[3] = { u - c, u, u + c };
double v[3][3] = { {1, u - c, H - u * c},
{1, u, 0.5*u*u},
{1, u + c, H + u * c } };
double a[3] = { 0.5 / (c*c)*((PR - PL) - c * rho*(uR - uL)),
(qR[0] - qL[0]) - 1 * (PR - PL) / (c*c),
0.5 / (c*c)*((PR - PL) + c * rho*(uR - uL)) };
double SUM[3];
SUM[0] = g[0] * a[0] * v[0][0] + g[1] * a[1] * v[1][0] + g[2] * a[2] * v[2][0];
SUM[1] = g[0] * a[0] * v[0][1] + g[1] * a[1] * v[1][1] + g[2] * a[2] * v[2][1];
SUM[2] = g[0] * a[0] * v[0][2] + g[1] * a[1] * v[1][2] + g[2] * a[2] * v[2][2];
double Flux[3];
Flux[0,i] = 0.5*(fqL[0] + fqR[0]) - 0.5*SUM[0];
Flux[1,i] = 0.5*(fqL[1] + fqR[1]) - 0.5*SUM[1];
Flux[2,i] = 0.5*(fqL[2] + fqR[2]) - 0.5*SUM[2];
std::cout << i << endl;
}
}

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:
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *