Texture.update() throwing exceptions - c++

It says
Assertion failed: x + window.getSize().x <= m_size.x
Size and x\y are correct.
It all breaks on last line. I need it for selection and copy/paste tool.
bufferRect.width = abs(curPos.x - point1.x);
bufferRect.height = abs(curPos.y - point1.y);
int width = bufferRect.width;
int height = bufferRect.height;
int x = bufferRect.left;
int y = bufferRect.top;
cout << "width is "<< bufferRect.width << endl;
cout << "height is " << bufferRect.height << endl;
cout << "left and top are " << bufferRect.left << " + " << bufferRect.top << endl;
bufferTexture.create(width, height);
bufferTexture.update(mainWindow, x, y);

Related

Football tournament with matrix

I'm trying to make a football tournament in C++, in which the user inputs the name of the teams(8 teams) and then each team has to play with the other one 1 time. Firstly, I don't know how to read the team names, I mean I tried to use .getline or just cin of a char array, but then I need to put the teams into the matrix and after the final game my program should print the table. So there's the first question: how to read the names and basically make the program think they are numbers or does it work with just with names, no need to use int? And then the users inputs the result for every game, but here comes the hard part. After all the results have been introduced, the matrix rotates cyclic and then the result stored in the variables(you will see in code victory/losses) overwrites themselves, so at the end, I cannot print the right table. So that's the second question: How can I make them store to the right 'team' while they rotate? Sorry if I didn't quite explain very well how it works, hope you understand it. Cheers!
// FOOTBALL TOURNAMENT
int map[2][4];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << "map[" << i << "][" << j << "]= ";
cin >> map[i][j];
}
}
cout << "The map looks like this:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
map[0][0] = 1;
int temp = 0, temp2 = 0, temp3 = 0, temp4 = 0, temp5 = 0, temp6 = 0;
int a, b, c, d, e, f, g, h, round = 0;
int victory_m00(0), losses_m10(0), victory_m10(0), losses_m00(0), victory_m01(0), losses_m11(0), victory_m11(0), losses_m01(0);
int victory_m02(0), losses_m12(0), victory_m12(0), losses_m02(0), victory_m03(0), losses_m13(0), victory_m13(0), losses_m03(0);
do
{
// Insert result for every game
cout << "Enter the result of the first game between " << map[0][0] << " vs. " << map[1][0] << endl;
cin >> a >> b;
if (a > b) {
victory_m00++;
losses_m10++;
}
else if (a < b)
{
victory_m10++;
losses_m00++;
}
cout << "Enter the result of the first game between: " << map[0][1] << " vs. " << map[1][1] << endl;
cin >> c >> d;
if (c > d) {
victory_m01++;
losses_m11++;
}
else if (c < d)
{
victory_m11++;
losses_m01++;
}
cout << "Enter the result of the first game between: " << map[0][2] << " vs. " << map[1][2] << endl;
cin >> e >> f;
if (e > f) {
victory_m02++;
losses_m12++;
}
else if (e < f)
{
victory_m12++;
losses_m02++;
}
cout << "Enter the result of the first game between: " << map[0][3] << " vs. " << map[1][3] << endl;
cin >> g >> h;
if (g > h) {
victory_m03++;
losses_m13++;
}
else if (g < h)
{
victory_m13++;
losses_m03++;
}
round++;
// Map switching
temp = map[1][0];
map[1][0] = map[0][1];
temp2 = map[1][1];
map[1][1] = temp;
temp3 = map[1][2];
map[1][2] = temp2;
temp4 = map[1][3];
map[1][3] = temp3;
temp5 = map[0][3];
map[0][3] = temp4;
temp6 = map[0][2];
map[0][2] = temp5;
map[0][1] = temp6;
// Table calculating and printing ~ also this has to be outside the loop (but at first i wanted to print the table after every 'round'
cout << "This is how the table looks like after the " << round << " round: \n";
cout << map[0][0] << " has: " << victory_m00 << " victory(-ies) and " << losses_m00 << " loss-es!\n";
cout << map[0][1] << " has: " << victory_m01 << " victory(-ies) and " << losses_m01 << " loss-es!\n";
cout << map[0][2] << " has: " << victory_m02 << " victory(-ies) and " << losses_m02 << " loss-es!\n";
cout << map[0][3] << " has: " << victory_m03 << " victory(-ies) and " << losses_m03 << " loss-es!\n";
cout << map[1][0] << " has: " << victory_m10 << " victory(-ies) and " << losses_m10 << " loss-es!\n";
cout << map[1][1] << " has: " << victory_m11 << " victory(-ies) and " << losses_m11 << " loss-es!\n";
cout << map[1][2] << " has: " << victory_m12 << " victory(-ies) and " << losses_m12 << " loss-es!\n";
cout << map[1][3] << " has: " << victory_m13 << " victory(-ies) and " << losses_m13 << " loss-es!\n";
cout << endl;
cout << endl;
} while (map[0][1] != 2);
```

Runge Kutta and PGPLOT

Hi guys when I use this code (makefile not included) I am supposed to be getting two sinusoidal outputs graphed on the same set of axes but for some reason although the output data is accurate the graph is not. In face its not even a smooth curve, it has holes and cusps. Anyone have an idea as to why? Does it have something to do this the array I have written for the time axis? (Note beginner at coding)
#include <iostream>
#include <cmath>
#include <stdio.h>
#include "cpgplot.h"
//d2xdt2 = -(g/L)*sin(x)
//dwdt = -d2xdt2
//coeff = -(g/L)
//dxdt= w
float dxdt(float w)
{
return w;
}
float dwdt(float x, float coeff)
{
return coeff*sin(x);
}
int main(){
// Standard Variables
float wARR[5];
float xARR[5];
float tARR[5];
float w; //w = angular velocity
float x; //x = theta used in second order equation
float xo; //xo = theta initial
float wo = 0; //wo = initial angular velocity
float dt = 1; //h = step value
float g=9.8; //g = gravity
float L; //L = length of pendulum
float to = 0;
float t;
float time;
float kx1, kw1;
float kx2, kw2;
float kx3, kw3;
float kx4, kw4;
// Input Printing
std::cout << "\n";
std::cout << "Click ENTER key after each value. Please input: \n The initial angle (in decimal radians): "<<"\n";
std::cin >> xo;
std::cout << "The length of the pendulum (in meters)."<<"\n";
std::cin >> L;
std::cout << "\n";
// Specific Variable Declarations
float coeff=-(g/L);
//Checkpoint values
std::cout << "CHECKPOINT VALUES: ";
std::cout << "\n";
std::cout << "Confirming your initial angle is: " << xo << " radian(s)";
std::cout << "\n";
std::cout << "Confirming the length is: " << L << " meters(s)";
std::cout << "\n";
std::cout << "Confirming the gravity constant is: " << g << " meters/sec";
std::cout << "\n";
std::cout << "Coeff (-g/L): " << coeff;
std::cout << "\n";
std::cout << "\n";
std::cout << "Insert the time (in seconds) you want to learn some cool stuff about your pendulum: "<<"\n";
std::cin >> time;
std::cout << "\n";
//Array info and Runge Kutta
xARR[0] = xo;
wARR[0] = 0;
tARR[0] = 0;
time=time+1;
for (int i = 0; i < time ; i++){
x = xARR[i];
w = wARR[i];
t = tARR[i];
kx1=dt*dxdt(w);
kw1=dt*dwdt(x,coeff);
kx2=dt*dxdt(w+kw1*0.5);
kw2=dt*dwdt(x+kx1*0.5, coeff);
kx3=dt*dxdt(w+kw2*0.5);
kw3=dt*dwdt(x+kx2*0.5, coeff);
kx4=dt*dxdt(w+kw3);
kw4=dt*dwdt(x+kx3, coeff);
std::cout << "\n";
std::cout << "RUNGE KUTTA VALUES: at " << i << " second(s)" << "\n";
std::cout << "kx1: " << kx1 << "\n";
std::cout << "kx2: " << kx2 << "\n";
std::cout << "kx3: " << kx3 << "\n";
std::cout << "kx4: " << kx4 << "\n";
std::cout << "kw1: " << kw1 << "\n";
std::cout << "kw2: " << kw2 << "\n";
std::cout << "kw3: " << kw3 << "\n";
std::cout << "kw4: " << kw4 << "\n";
xARR[i+1] = xo + (1.0/6.0)*(kx1 + 2*kx2 + 2*kx3 + kx4);
wARR[i+1] = wo + (1.0/6.0)*(kw1 + 2*kw2 + 2*kw3 + kw4);
std::cout << "FINAL VALUES: at " << i << " second(s)" << "\n";
std::cout << "The angle is: " << x << " radian(s)";
std::cout << "\n";
std::cout << "The velocity is: " << w << " radians/s";
std::cout << "\n";
std::cout << "\n";
std::cout << "-----------------------------------------------------\n";
std::cout << "\n";
}
// Graphing with arrays
//To see the time dependence, let's suppose you have two arrays: one for w and one for x. Then the values x[i] and w[i] entry describe the position and velocity of your pendulum at time t_i.
// Open a plot window
if (!cpgopen("/XWINDOW")) return 1;
// Set-up plot axes
// M_PI is defined in cmath
cpgenv(0,time,-1,1,0,1);
// Label axes
cpglab("time", "angle", "RED = Angle BLUE = Velocity");
cpgsci(2); //RED
cpgline(10,tARR, xARR);
cpgsci(4); //BLUE
cpgline(10,tARR, wARR);
cpgclos();
}

How to search for individual rows in multidimensional array then perform calculation for each individual element in certain rows

I am currently using a table to find corresponding fuel levels in individual tanks in relation to the total amount of fuel the vessel is holding. The vessel currently has 8 tanks and the fuel is distributed accordingly to the amount of total fuel the vessel is carrying. When given a total fuel amount I need to find or populate approximately where the fuel is at in the 8 tanks at that total fuel amount.
I have tried searching through the 2d array to find the corresponding fuel tanks at a given time but with the chart (2d array), I realized I need to use an index for the left most column, (which is the total fuel) and then find the lower bound row and upper bound row. Afterwards I need to find the average of the two rows each individual elements (fuel tanks). I was able to make if statements and hardcore the math, but I know there is a recursive way to search the left column, find the upper and lower bond row of a given fuel amount and find the amount in each fuel tank. So if I was given a target amount of fuel of ie. 194568, I need to find approximately where the fuel is at in the 8 tanks.
int main()
const int NUM_ROWS = 22;
const int NUM_COLUMNS = 9;
int arr[NUM_ROWS][NUM_COLUMNS] = {
{80000,4500,7500,11700,11700,0,0,0,9200},
{90000,4500,7500,13800,13800,0,0,0,10800 },
{100000,4500,7500,15900,15900,0,0,0,12400},
{110000,4500,7500,18200,18200,0,0,0,13200},
{120000,4500,7500,20400,20400,0,0,0,14400},
{130000,4500,7500,22000,23000,0,0,0,16000},
{140000,4500,7500,22000,26200,0,0,0,19600},
{150000,4500,7500,22000,29400,0,0,0,23200},
{160000,4500,7500,22000,32700,0,0,0,26600},
{170000,4500,7500,23200,35400,0,0,0,28800},
{180000,4500,7500,25300,37500,0,0,0,30400},
{190000,4500,7500,27400,39600,0,0,0,32000},
{200000,4500,7500,29500,41700,0,0,0,33600},
{210000,4500,7500,31800,44300,0,0,0,33800},
{220000,4500,7500,31800,44300,0,0,10000,33800},
{230000,4500,7500,31800,44300,0,3000,15000,35800},
{240000,4500,7500,31800,44300,0,8000,15000,40800},
{250000,4500,7500,31800,44300,0,8000,25000,40800},
{260000,4500,7500,31800,44300,0,8000,35000,40800},
{270000,4500,7500,31800,44300,0,8000,45000,40800},
{280000,4500,7500,31800,44300,0,14000,45000,44800},
{290000,4500,7500,31800,44300,0,18800,46000,49000}, };
int key;
cout << "Enter Fuel Amount:";
cin >> key;
if (key >= 80000 && key < 90000)
{
cout << "L/R_Ext = " << (arr[0][1] + arr[1][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[0][2] + arr[1][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[0][3] + arr[1][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[0][4] + arr[1][4]) / 2 << endl;
cout << "Forward = " << (arr[0][5] + arr[1][5]) / 2 << endl;
cout << "Center = " << (arr[0][6] + arr[1][6]) / 2 << endl;
cout << "Mid = " << (arr[0][7] + arr[1][7]) / 2 << endl;
cout << "Aft = " << (arr[0][8] + arr[1][8]) / 2 << endl;
}
else if (key >= 90000 && key < 100000)
{
cout << "L/R_Ext = " << (arr[1][1] + arr[2][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[1][2] + arr[2][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[1][3] + arr[2][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[1][4] + arr[2][4]) / 2 << endl;
cout << "Forward = " << (arr[1][5] + arr[2][5]) / 2 << endl;
cout << "Center = " << (arr[1][6] + arr[2][6]) / 2 << endl;
cout << "Mid = " << (arr[1][7] + arr[2][7]) / 2 << endl;
cout << "Aft = " << (arr[1][8] + arr[2][8]) / 2 << endl;
}
else if (key >= 100000 && key < 110000)
{
cout << "L/R_Ext = " << (arr[2][1] + arr[3][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[2][2] + arr[3][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[2][3] + arr[3][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[2][4] + arr[3][4]) / 2 << endl;
cout << "Forward = " << (arr[2][5] + arr[3][5]) / 2 << endl;
cout << "Center = " << (arr[2][6] + arr[3][6]) / 2 << endl;
cout << "Mid = " << (arr[2][7] + arr[3][7]) / 2 << endl;
cout << "Aft = " << (arr[2][8] + arr[3][8]) / 2 << endl;
}
cout << "First row:\n";
for (col = 0; col < NUM_COLUMNS; col++) {
cout << arr[0][col] << '\t';
}
cout << "Second row:\n";
for (col = 0; col < NUM_COLUMNS; col++) {
cout << arr[1][col] << '\t';
}
cout << "\n\nSecond column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][1] << '\n';
}
cout << "\n\nThird column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][2] << '\n';
}
cout << "\n\nForth column:\n";
for (row = 0; row < NUM_ROWS; row++) {
cout << arr[row][3] << '\n';
}
/*etc....
I expect the outcome when given a target of 125648 it will search the array and return the fuel in each tank for
int LR_Ext;
int LR_Outbrd;
int L1R4;
int L2R3;
int Forward;
int Center;
int Mid;
int Aft;
and perhaps assign them to a variable.
Instead of repeating the entire cout statements in each if case, you could first determine an index which you can use in display code:
int index = 0;
if (key >= 80000 && key < 90000)
index = 0;
else if (key >= 90000 && key < 100000)
index = 1;
else if (key >= 100000 && key < 110000)
index = 2;
else
// You should do some check if an invalid key is provided
// Now display
cout << "L/R_Ext = " << (arr[index][1] + arr[index+1][1]) / 2 << endl;
cout << "L/R_ Outbrd = " << (arr[index][2] + arr[index+1][2]) / 2 << endl;
cout << "L1 R4 = " << (arr[index][3] + arr[index+1][3]) / 2 << endl;
cout << "L2 R3 = " << (arr[index][4] + arr[index+1][4]) / 2 << endl;
cout << "Forward = " << (arr[index][5] + arr[index+1][5]) / 2 << endl;
cout << "Center = " << (arr[index][6] + arr[index+1][6]) / 2 << endl;
cout << "Mid = " << (arr[index][7] + arr[index+1][7]) / 2 << endl;
cout << "Aft = " << (arr[index][8] + arr[index+1][8]) / 2 << endl;

Finding largest 2x2 average in a 2D Array

I'm trying to find the largest 2x2 average in a matrix. I have found a person who shares a similar problem to mine however, their solution does not work.
Below is my code:
//Part B
//Finding deepest 2x2 area
int rowX, colX, ac1Row, ac1Col, ac2Row, ac2Col, ac3Row, ac3Col, ac4Row, ac4Col;
float largestArea = 0;
for(rowX = 0; rowX < dataRow - 1; rowX++){
for(colX = 0; colX < dataCol - 1; colX++){
float area = (oceanData[rowX][colX] + oceanData[rowX][colX + 1] + oceanData[rowX + 1][colX] + oceanData[rowX + 1][colX + 1]) / 4;
if(largestArea < area){
largestArea = area;
int ac1Row = rowX; int ac1Col = colX;
int ac2Row = rowX; int ac2Col = colX + 1;
int ac3Row = rowX + 1; int ac3Col = colX;
int ac4Row = rowX + 1; int ac4Col = colX + 1;
}
}
}
//Display results
cout << endl << "The deepest 2x2 area is: " << largestArea << " m" << endl;
cout << endl << "The coordinates are: (" << ac1Row << "," << ac1Col << ")" << " " << "(" << ac2Row << "," << ac2Col << ")";
cout << "(" << ac3Row << "," << ac3Col << ")" << "(" << ac4Row << "," << ac4Col << ")" << endl;
I'm using a .txt file with my data which is 6 by 6 (Data stored in oceanData, size stored in dataRow and dataCol) My output gives me the average (supposedly), however, when I try to output the coordinates I get strange numbers:
ex.
The coordinates are: (4356788,0),(0,0),(0,0),(8,0)
Anyone see where my problem is?
Thanks!
You are redefining the variables on each loop:
int ac1Row = rowX; int ac1Col = colX;
But you don't even need all 8 variables, you can save only the coordinates of a single corner and calculate the others.
Consider this
int acRow, acCol;
float largestArea = 0;
for(int rowX = 0; rowX < dataRow - 1; rowX++){
for(int colX = 0; colX < dataCol - 1; colX++){
float area = (oceanData[rowX][colX] + oceanData[rowX][colX + 1] + oceanData[rowX + 1][colX] + oceanData[rowX + 1][colX + 1]) / 4;
if(largestArea < area){
largestArea = area;
acRow = rowX;
acCol = colX;
}
}
}
cout << endl << "The deepest 2x2 area is: " << largestArea << " m" << endl;
cout << endl << "The coordinates are: (" << acRow << "," << acCol << ")" << " " << "(" << acRow << "," << acCol + 1 << ")";
cout << "(" << acRow + 1 << "," << acCol << ")" << "(" << acRow + 1 << "," << acCol + 1 << ")" << endl;

c++ infile extraction checking if int

I'm fairly new to C++ and have no idea where to start with this or if its even possible, but i'm trying to check to see if length, width, radius, etc are actually doubles and if they are not return 0.
int main() {
ifstream inFile;
ofstream outFile;
string in;
double length, width, tLength = 0, tWidth = 0, areaRec, tAreaRec = 0, parameter, tParameter = 0;
double radius, tRadius = 0, areaCirc, tAreaCirc = 0, circumference, tCircumference = 0;
double savings, tSavings = 0;
int people = 0, age, tAge = 0;
string name1, name2;
inFile.open("inData_with_error.txt");
outFile.open("outData_Normal.txt");
outFile << fixed << showpoint << setprecision(2);
if (!inFile) {
cout << "ERROR: Unable to open file!" << endl;
inFile.close();
}
else {
cout << "Caculating..." << endl;
while (inFile >> length >> width >> radius >> name1 >> name2 >> age >> savings) {
cout << length << ", " << width << ", " << radius << ", " << name1 << ", " << name2 << ", " << age << ", " << savings << endl;
tLength = tLength + length;
tWidth = tWidth + width;
parameter = length * 2 + width * 2;
areaRec = length * width;
tParameter = tParameter + parameter;
tAreaRec = tAreaRec + areaRec;
tRadius = tRadius + radius;
areaCirc = pow(radius, 2) * PI;
circumference = (radius * 2) * PI;
tAreaCirc = tAreaCirc + areaCirc;
tCircumference = tCircumference + circumference;
people = people + 1;
tAge = tAge + age;
tSavings = tSavings + savings;
}
}
cout << "Done" << endl;
outFile << "Rectangle:" << endl << "Total length = " << tLength << ", " << "Total width = " << tWidth << ", " << "Total area = " << tAreaRec << ", " << "Total parameter = " << tParameter << endl << endl
<< "Circle:" << endl << "Total radius = " << tRadius << ", " << "Total area = " << tAreaCirc << ", " << "Total circumference = " << tCircumference << endl << endl
<< "Person: " << endl << "Total number of persons = " << people << endl << "Total age = " << tAge << endl << "Total savings = " << tSavings << endl;
inFile.close();
outFile.close();
system("pause");
return 0;
}
For example if my data with error is the below i want to catch the char and strings and return a 0 but have no idea how to tackle this question. Can someone lead me into the right direction?
10.45 aaaa
13.78
Jake Melon 45
7600
128 76.9
;
Mike Sander 23
800
15.9 43
w
David James i
87000.54
After the end of the while loop, you can add a test to check to whether the while loop got terminated due to EOF or due to an error in reading the data.
while (inFile >> length >> width >> radius >> name1 >> name2 >> age >> savings) { ... }
if ( inFile.eof() )
{
// No errors in reading data
}
else
{
// Error in reading data.
// Deal with error
}