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.
Related
#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
My code compiles and runs without error in Visual Studio, however I need to to run in CodeLite on Linux and it gives me a segmentation fault for the same code.
For reference this is my code:
#include <string>
#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <tuple>
using namespace std;
tuple<vector<double>, vector<double>, vector<double>> RK4() {
//open parameters.txt, put data into a vector
ifstream fin("parameters.txt");
vector<double> data;
data.reserve(8);
double element;
while (fin >> element) {
data.push_back(element);
}
//define tspan
vector<double> tspan(2);
tspan[0] = 0.0;
tspan[1] = data[7];
//define y0
vector<double> y0(4);
//CHANGE TO DATA[4], DATA[5]
const double a = 3.141592653589793238462643383279;
y0[0] = data[4];
y0[1] = data[5];
y0[2] = 0.0;
y0[3] = 0.0;
double theta1 = y0[0];
double theta2 = y0[1];
double omega1 = y0[2];
double omega2 = y0[3];
//define stepSize
double stepSize;
stepSize = data[6];
//define range
int range = int(tspan[1] / stepSize);
//define other constants
double m1, m2, l1, l2;
m1 = data[0];
m2 = data[1];
l1 = data[2];
l2 = data[3];
double g = 9.81;
//define y, t vectors
vector<double> y1(range);
vector<double> y2(range);
vector<double> y3(range);
vector<double> y4(range);
vector<double> t(range);
for (double i = 0.0; i < 1.0 * range; i++) {
t[i] = i * stepSize;
}
//enter y0 into first value
y1[0] = theta1;
y2[0] = theta2;
y3[0] = omega1;
y4[0] = omega2;
//loop to find y, t vectors
for (int i = 0; i < range - 1; i++) {
//finding all k values:
//k1
double dTheta1_1 = y3[i];
double dOmega1_1 = (-g * (2 * m1 + m2) * sin(y1[i]) - m2 * g * sin(y1[i] - 2 * y2[i]) - 2 * sin(y1[i] - y2[i]) * m2 * (pow(y4[i], 2) * l2 + pow(y3[i], 2) * l1 * cos(y1[i] - y2[i]))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * y1[i] - 2 * y2[i])));
double dTheta2_1 = y4[i];
double dOmega2_1 = (2 * sin(y1[i] - y2[i]) * (pow(y3[i], 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i]) + pow(y4[i], 2) * l2 * m2 * cos(y1[i] - y2[i]))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * y1[i] - 2 * y2[i])));
//k2
double dTheta1_2 = y3[i] + 0.5 * stepSize * dTheta1_1;
double dOmega1_2 = (-g * (2 * m1 + m2) * sin(y1[i] + 0.5 * stepSize * dTheta1_1) - m2 * g * sin((y1[i] + 0.5 * stepSize * dTheta1_1) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_1)) - 2 * sin((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)) * m2 * (pow(y4[i] + 0.5 * stepSize * dOmega2_1, 2) * l2 + pow(y3[i] + 0.5 * stepSize * dOmega1_1, 2) * l1 * cos((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_1) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_1))));
double dTheta2_2 = y4[i] + 0.5 * stepSize * dTheta2_1;
double dOmega2_2 = (2 * sin((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)) * (pow(y3[i] + 0.5 * stepSize * dOmega1_1, 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i] + 0.5 * stepSize * dTheta1_1) + pow(y4[i] + 0.5 * stepSize * dOmega2_1, 2) * l2 * m2 * cos((y1[i] + 0.5 * stepSize * dTheta1_1) - (y2[i] + 0.5 * stepSize * dTheta2_1)))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_1) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_1))));
//k3
double dTheta1_3 = y3[i] + 0.5 * stepSize * dTheta1_2;
double dOmega1_3 = (-g * (2 * m1 + m2) * sin(y1[i] + 0.5 * stepSize * dTheta1_2) - m2 * g * sin((y1[i] + 0.5 * stepSize * dTheta1_2) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_2)) - 2 * sin((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)) * m2 * (pow(y4[i] + 0.5 * stepSize * dOmega2_2, 2) * l2 + pow(y3[i] + 0.5 * stepSize * dOmega1_2, 2) * l1 * cos((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_2) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_2))));
double dTheta2_3 = y4[i] + 0.5 * stepSize * dTheta2_2;
double dOmega2_3 = (2 * sin((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)) * (pow(y3[i] + 0.5 * stepSize * dOmega1_2, 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i] + 0.5 * stepSize * dTheta1_2) + pow(y4[i] + 0.5 * stepSize * dOmega2_2, 2) * l2 * m2 * cos((y1[i] + 0.5 * stepSize * dTheta1_2) - (y2[i] + 0.5 * stepSize * dTheta2_2)))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + 0.5 * stepSize * dTheta1_2) - 2 * (y2[i] + 0.5 * stepSize * dTheta2_2))));
//k4
double dTheta1_4 = y3[i] + stepSize * dTheta1_3;
double dOmega1_4 = (-g * (2 * m1 + m2) * sin(y1[i] + stepSize * dTheta1_3) - m2 * g * sin((y1[i] + stepSize * dTheta1_3) - 2 * (y2[i] + stepSize * dTheta2_3)) - 2 * sin((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)) * m2 * (pow(y4[i] + stepSize * dOmega2_3, 2) * l2 + pow(y3[i] + stepSize * dOmega1_3, 2) * l1 * cos((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)))) / (l1 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + stepSize * dTheta1_3) - 2 * (y2[i] + stepSize * dTheta2_3))));
double dTheta2_4 = y4[i] + stepSize * dTheta2_3;
double dOmega2_4 = (2 * sin((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)) * (pow(y3[i] + stepSize * dOmega1_3, 2) * l1 * (m1 + m2) + g * (m1 + m2) * cos(y1[i] + stepSize * dTheta1_3) + pow(y4[i] + stepSize * dOmega2_3, 2) * l2 * m2 * cos((y1[i] + stepSize * dTheta1_3) - (y2[i] + stepSize * dTheta2_3)))) / (l2 * (2 * m1 + m2 - m2 * cos(2 * (y1[i] + stepSize * dTheta1_3) - 2 * (y2[i] + stepSize * dTheta2_3))));
double theta1New = y1[i] + (stepSize / 6.0) * (dTheta1_1 + 2 * dTheta1_2 + 2 * dTheta1_3 + dTheta1_4);
double omega1New = y3[i] + (stepSize / 6.0) * (dOmega1_1 + 2 * dOmega1_2 + 2 * dOmega1_3 + dOmega1_4);
double theta2New = y2[i] + (stepSize / 6.0) * (dTheta2_1 + 2 * dTheta2_2 + 2 * dTheta2_3 + dTheta2_4);
double omega2New = y4[i] + (stepSize / 6.0) * (dOmega2_1 + 2 * dOmega2_2 + 2 * dOmega2_3 + dOmega2_4);
// updating y arrays
y1[i + 1] = theta1New;
y2[i + 1] = theta2New;
y3[i + 1] = omega1New;
y4[i + 1] = omega2New;
}
return make_tuple(y1, y2, t);
}
int main() {
//open parameters.txt, put data into a vector
ifstream fin("parameters.txt");
vector<double> data;
data.reserve(8);
double element;
while (fin >> element) {
data.push_back(element);
}
//define tspan
vector<double> tspan(2);
tspan[0] = 0.0;
tspan[1] = data[7];
//define stepSize
double stepSize = data[6];
//define other constants
double l1 = data[2];
double l2 = data[3];
//get y1, y2, t from RK4 function
auto temp = RK4();
vector<double> y1 = get<0>(temp);
vector<double> y2 = get<1>(temp);
vector<double> t = get<2>(temp);
//define range
int const range = static_cast<int>(y1.size());
vector<double> x_1(range), y_1(range), x_2(range), y_2(range);
//define x_1, x_2, y_1, y_2
for (int i = 0; i < range; i++) {
x_1[i] = { sin(y1[i]) * l1 };
y_1[i] = { -cos(y1[i]) * l1 };
x_2[i] = { sin(y1[i]) * l1 + sin(y2[i]) * l2 };
y_2[i] = { -cos(y1[i]) * l1 - cos(y2[i]) * l2 };
}
//writing x,y positions at time t to output.txt
ofstream myfile;
myfile.open("output.txt");
if (myfile.is_open()) {
myfile << "t: " << endl;
for (int i = 0; i < range; i++) {
myfile << t[i] << " ";
}
cout << endl;
myfile << "x_1: " << endl;
for (int i = 0; i < range; i++) {
myfile << x_1[i] << " ";
}
cout << endl;
myfile << "y_1: " << endl;
for (int i = 0; i < range; i++) {
myfile << y_1[i] << " ";
}
cout << endl;
myfile << "x_2: " << endl;
for (int i = 0; i < range; i++) {
myfile << x_2[i] << " ";
}
cout << endl;
myfile << "y_2: " << endl;
for (int i = 0; i < range; i++) {
myfile << y_2[i] << " ";
}
cout << endl;
myfile.close();
}
else { cout << "Unable to open file"; }
return 0;
}
In both cases "parameters.txt" is in the working directory. Why does the operating system/compiler I use affect the outcome? What is the problem?
I am trying to save the floats from v1X, v1Y to v10X,v10Y in a textfile called verticest.txt and load them back in, but visual studio says that they are already defined including the starX and starY arrays, t and ti in the graphics2dassignment.cpp file
example errors:
error LNK1169: one or more mutliple defined symbols found
error LNK2005: "float * starX" (?starX##3PAMA)already defined in Graphics2DAssignment.obj
the same errors occur for starY, from v1X, v1Y to v10X, V10Y
primitive.cpp
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <gl\GL.h>
#include <gl\GLU.h>
#include "glut.h"
#include <fstream>
#include <math.h>
#include "primitive.h"
using namespace std;
GLuint listname = 1;
float vertices[10][2];
//saves the coordinates in a text file
void saveCoordinates()
{
ofstream outfile("vertices.txt");
v1X = 0;
v1Y = 1;
outfile << v1X << " " << v1Y << endl;;
v2X = v1X * cos(3.1415 * 72 / 180) - v1Y * sin(3.1415 * 72 / 180);
v2Y = v1Y * cos(3.1415 * 72 / 180) + v1X * sin(3.1415 * 72 / 180);
outfile << v2X << " " << v2Y << endl;;
v3X = v2X * cos(3.1415 * 72 / 180) - v2Y * sin(3.1415 * 72 / 180);
v3Y = v2Y * cos(3.1415 * 72 / 180) + v2X * sin(3.1415 * 72 / 180);
outfile << v3X << " " << v3Y << endl;;
v4X = v3X * cos(3.1415 * 72 / 180) - v3Y * sin(3.1415 * 72 / 180);
v4Y = v3Y * cos(3.1415 * 72 / 180) + v3X * sin(3.1415 * 72 / 180);
outfile << v4X << " " << v4Y << endl;;
v5X = v4X * cos(3.1415 * 72 / 180) - v4Y * sin(3.1415 * 72 / 180);
v5Y = v4Y * cos(3.1415 * 72 / 180) + v4X * sin(3.1415 * 72 / 180);
outfile << v5X << " " << v5Y << endl;;
v6X = (v1X * cos(3.1415 * 36 / 180) - v1Y * sin(3.1315 * 36 / 180)) / 2;
v6Y = (v1Y * cos(3.1415 * 36 / 180) + v1X * sin(3.1315 * 36 / 180)) / 2;
outfile << v6X << " " << v6Y << endl;;
v7X = (v2X * cos(3.1415 * 36 / 180) - v2Y * sin(3.1315 * 36 / 180)) / 2;
v7Y = (v2Y * cos(3.1415 * 36 / 180) + v2X * sin(3.1315 * 36 / 180)) / 2;
outfile << v7X << " " << v7Y << endl;;
v8X = (v3X * cos(3.1415 * 36 / 180) - v3Y * sin(3.1315 * 36 / 180)) / 2;
v8Y = (v3Y * cos(3.1415 * 36 / 180) + v3X * sin(3.1315 * 36 / 180)) / 2;
outfile << v8X << " " << v8Y << endl;;
v9X = (v4X * cos(3.1415 * 36 / 180) - v4Y * sin(3.1315 * 36 / 180)) / 2;
v9Y = (v4Y * cos(3.1415 * 36 / 180) + v4X * sin(3.1315 * 36 / 180)) / 2;
outfile << v9X << " " << v9Y << endl;;
v10X = (v5X * cos(3.1415 * 36 / 180) - v5Y * sin(3.1315 * 36 / 180)) / 2;
v10Y = (v5Y * cos(3.1415 * 36 / 180) + v5X * sin(3.1315 * 36 / 180)) / 2;
outfile << v10X << " " << v10Y << endl;;
outfile.close();
}
void loadCoordinates()
{
// loads the vertices from the text file
ifstream infile("vertices.txt");
for (int i = 0; i < 10; i++){
infile >> vertices[i][0];
infile >> vertices[i][1];
cout << vertices[i][0] << " " << vertices[i][1] << endl;
}
}
void interpolation()
{
//glEnable(GL_DEPTH);
saveCoordinates();
loadCoordinates();
glNewList(listname, GL_COMPILE);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 5; i++){
glVertex3f(vertices[i][0], vertices[i][1], 0.0);
}
glEnd();
glEndList();
starX = new float[10];
starY = new float[10];
t = 0.0;
ti = 0.0005;
starX[0] = v1X;
starY[0] = v1Y;
starX[2] = v2X;
starY[2] = v2Y;
starX[4] = v3X;
starY[4] = v3Y;
starX[6] = v4X;
starY[6] = v4Y;
starX[8] = v5X;
starY[8] = v5Y;
starX[1] = (1.0 - t) * v1X + t * v6X;
starY[1] = (1.0 - t) * v1Y + t * v6Y;
starX[3] = (1.0 - t) * v2X + t * v7X;
starY[3] = (1.0 - t) * v2Y + t * v7Y;
starX[5] = (1.0 - t) * v3X + t * v8X;
starY[5] = (1.0 - t) * v3Y + t * v8Y;
starX[7] = (1.0 - t) * v4X + t * v9X;
starY[7] = (1.0 - t) * v4Y + t * v9Y;
starX[9] = (1.0 - t) * v5X + t * v10X;
starY[9] = (1.0 - t) * v5Y + t * v10Y;
}
primitive.h
#ifndef PRIMITIVES_H
#define PRIMITIVES_H
extern float vertices[10][2];
float* starX;
float* starY;
float t; //clock
float ti; //time interval
float v1X;
float v1Y;
float v2X;
float v2Y;
float v3X;
float v3Y;
float v4X;
float v4Y;
float v5X;
float v5Y;
float v6X;
float v6Y;
float v7X;
float v7Y;
float v8X;
float v8Y;
float v9X;
float v9Y;
float v10X;
float v10Y;
void interpolation();
void saveCoordinates();
void loadCoordinates();
#endif
Graphics2DAssignment.cpp (this is the only bit that uses those floats and arrays
if (drawStar == true)
{
interpolation();
glPushMatrix(); // pushes the current matrix stack down by one, duplicating the current matrix.
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 10; i++)
glVertex2f(starX[i], starY[i]);
glEnd();
//glPopMatrix pops the current matrix stack, replacing the current matrix with the one below it on the stack.
}
float* starX; and the others in primitive.h are definitions. If primitive.h is included in several TUs, you get a multiple definition error.
Just declare them in the header and define them in a single TU, just like you did for vertices.
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:
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I am trying to set the price of each array value by row.
The price of each row has to be entered every time the program starts.
the program complies but the output is incorrect, here is what is displayed:
Please enter price for row 0 = 66
please enter row number 0
please enter seat number 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
---------------------------------------------
# * * * * * * * * * * * * * * |0
* * * * * * * * * * * * * * * |1
* * * * * * * * * * * * * * * |2
* * * * * * * * * * * * * * * |3
* * * * * * * * * * * * * * * |4
* * * * * * * * * * * * * * * |5
* * * * * * * * * * * * * * * |6
* * * * * * * * * * * * * * * |7
* * * * * * * * * * * * * * * |8
* * * * * * * * * * * * * * * |9
-858993460
Press any key to continue . . .
what is incorrect is "-858993460", it should display 66. Someone help me
my code looks like
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void drawgrid(int ai[10][15], int ji, int ii); // draw grid proto
int numb(int num[10], int tps)
// this function returns back the array possition
// that matchest "tps" ' number
{
return num[0];
}
void numbee(int nu[10])
// this function prompts the user for the price of each row
{
for (int i = 0; i < 10; i++)
{
cout << " Please enter price for row " << i << endl;
cin >> nu[i];
}
return;
}
int printArray(int a[10][15],int tp,int pp)
// this function asks the user what seat to buy
{
int num[10];
cout << "please enter row number ";
cin >> tp;
cout << "please enter seat number";
cin >> pp;
a[tp][pp] = numb(num, tp);
return 0;
}
int main() // this is the main
// calls numbee, drawgrid, and printArray
// sets all to 0 using memset
{
int n[10];
numbee(n);
int love;
int a[10][15];
int i = 0, j = 0;
memset(a, 0, sizeof(a[10][15]) * 10 * 15); // set everything in gridto 0
drawgrid(a, i, j);
love = printArray(a, i, j);
numb(n, love);
drawgrid(a, i, j);
cout << a[0][0];
system("PAUSE");
return 0;
}
void drawgrid(int ai[10][15], int ji, int ii)
// this function draws the gridd
{
ii = 0; ji = 0;
cout << "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ";
std::cout << std::endl;
cout << "---------------------------------------------";
std::cout << std::endl;
for (int ii = 0; ii < 10; ii++) //This loops on the rows.
{
for (int ji = 0; ji < 15; ji++) //This loops on the columns
{
if (ai[ii][ji] == 0)
{
cout << "*" << " ";
}
else
{
cout << "#" << " ";
}
}
cout << "|" << ii;
cout << endl;
}
}
In this function:
int printArray(int a[10][15],int tp,int pp)
// this function asks the user what seat to buy
{
int num[10];
cout << "please enter row number ";
cin >> tp;
cout << "please enter seat number";
cin >> pp;
a[tp][pp] = numb(num, tp);
return 0;
}
you delcare num[10] and you left it uninitialized. Then in this line:
a[tp][pp] = numb(num, tp);
through a function:
int numb(int num[10], int tps)
// this function returns back the array possition
// that matchest "tps" ' number
{
return num[0];
}
you write uninitialized num[0] into a[0][0]. That's why you get garbage.
Your code is quite confusing because of the naming - "numb" and "numbee" are nonsense, and the function that asks for a seat is called "printArray", but doesn't ever print an array. You should probably work on your naming a bit.
On to the problem:
printArray has a local array variable, num.
This array is uninitialized, so can contain any random data.
You then assign the value of the first element of this array (returned from numb(num, tp)) to a[tp][pp];, and this is where the strange data is coming from.
It's difficult to advise on a remedy because it's a bit unclear what you intend for numb and code such as
love = printArray(a, i, j);
numb(n, love);
to accomplish.