I am trying to draw a heart shape with words inside as a surprise for a friend tomorrow but i cant figure out how to put the words inside the heart . I am only able to draw the heart shape
Code to draw Heart
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x,y;
double size=10;
for (x=0;x<size;x++)
{
for (y=0;y<=4*size;y++)
{
double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
if (dist1 < size + 0.5 || dist2 < size + 0.5 )
cout<<"V";
else
cout<<" ";
}
cout<<endl;
}
for ( x=1;x<2*size;x++)
{
for(y=0;y<x;y++)
cout<<" ";
for (y=0; y<4*size + 1 - 2*x; y++)
cout<<"V";
cout<<endl;
}
system("PAUSE");
}
I need help putting words inside the heart shape
Pretty much the same as the other answer but I had already started so I thought I may as well finish. As a bonus you can specify what line of the "V" shape it prints on.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x, y, size=10;
string message(" hello there ");
int print_line = 4;
if (message.length() % 2 != 0) message += " ";
for (x=0;x<size;x++)
{
for (y=0;y<=4*size;y++)
{
double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );
if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
cout << "V";
}
else cout << " ";
}
cout<<"\n";
}
for (x=1;x<2*size;x++)
{
for(y=0;y<x;y++) cout << " ";
for (y=0; y<4*size + 1 - 2*x; y++)
{
if (x >= print_line - 1 && x <= print_line + 1) {
int idx = y - (4*size - 2*x - message.length()) / 2;
if (idx < message.length() && idx >= 0) {
if (x == print_line) cout<<message[idx];
else cout << " ";
}
else cout << "V";
}
else cout << "V";
}
cout<<endl;
}
}
You could just wait until you get to a pre-specified position in the heart and print out a message instead of the "V"s, like this:
char message[] = " MY MESSAGE ";
for ( x=1;x<2*size;x++)
{
for(y=0;y<x;y++)
cout<<" ";
for (y=0; y<4*size + 1 - 2*x; y++)
{
if (x == 1 && y == (2*size - strlen(message)/2))
{
cout << message;
y += strlen(message)-1;
}
else
cout<<"V";
}
cout<<endl;
}
The y += strlen(message)-1; is to advance the column index according to the number of characters printed. (2*size - strlen(message)/2) is a position which will center the string.
If you want to obfuscate the code as much as possible (so you don't know what the message is until the code runs), you could use a hash table to map positions to letters or something like that.
Try this code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout<<"Print Heart....C++\n";
int n=7; //size of heart
for(int i=-3*n/2;i<=n;i++){
for(int j=-3*n/2;j<=3*n/2;j++){
/* inside either diamond or two circles */
if((abs(i)+abs(j)<n)||((-n/2-i)*(-n/2-i)+(n/2-j)*(n/2-j)<=n*n/2)||((-n/2-i)*(-n/2-i)+(-n/2-j)*(-n/2-j)<=n*n/2)){
cout<<"v ";
}
else{
cout<<" ";
}
}
cout<<"\n";
}
cout<<"\n\n\nPlease don\'t forget to like.... :) :) :) \n";
cout<<"Credit: Heart Of Java : https://code.sololearn.com/caiu85u6tr30/#java";
return 0;
}
Demo here: https://code.sololearn.com/cuXe0axsK8R2/#cpp
Related
Link of Question : https://www.codechef.com/JULY20B/problems/PTMSSNG
Question Statement
Chef has N axis-parallel rectangles in a 2D Cartesian coordinate system. These rectangles may intersect, but it is guaranteed that all their 4N vertices are pairwise distinct.
Unfortunately, Chef lost one vertex, and up until now, none of his fixes have worked (although putting an image of a point on a milk carton might not have been the greatest idea after all…). Therefore, he gave you the task of finding it! You are given the remaining 4N−1 points and you should find the missing one.
Can anyone suggest where I'm going wrong or update my code or share a few test cases.
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#define ll long long
using namespace std;
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++)
{
vector<pair<ll, ll>> v;
ll n, m, a;
bool checkx = false;
cin >> n;
m = 4 * n - 1;
ll x[m], y[m];
ll c, d;
a = (m - 1) / 2;
for (ll i = 0; i < m; i++)
{
cin >> x[i] >> y[i];
v.push_back(make_pair(x[i], y[i]));
}
sort(v.begin(), v.end());
for (ll i = a; i >= 1; --i)
{
if (v[2 * i].first != v[2 * i - 1].first)
{
c = v[2 * i].first;
checkx = true;
if ((2 * i) % 4 == 0 && i >= 2)
{
if (v[2 * i].second == v[2 * i + 1].second)
{
d = v[2 * i + 2].second;
}
else
{
d = v[2 * i + 1].second;
}
}
else
{
if (v[2 * i].second != v[2 * i - 1].second)
{
d = v[2 * i - 1].second;
}
else
{
d = v[2 * i - 2].second;
}
}
break;
}
}
if (checkx)
{
cout << c << " " << d;
}
else
{
if (v[0].second == v[1].second)
{
d = v[2].second;
}
else
{
d = v[1].second;
}
cout << v[0].first << " " << d;
}
cout << endl;
}
return 0;
}
You don't need to do such complex things. Just input your x and y vectors and xor every element of each vector. The final value will be the required answer.
LOGIC :
(a,b)------------------(c,b)
| |
| |
| |
| |
(a,d)------------------(c,d)
See by this figure, each variable (a, b, c, d) occurs even number of times. This "even thing" will also be true for the N rectangles. Hence, you have to find the values of x and y which are occurring odd number of times.
To find the odd one out in such cases, the best trick is to xor every element of the vector. This works because of these properties of xor : k xor k = 0 and k xor 0 = k.
CODE:
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
signed main() {
std::size_t t, n;
std::cin >> t;
while (t--) {
std::cin >> n;
n = 4 * n - 1;
std::vector<int> x(n), y(n);
for (std::size_t i = 0; i < n; ++i)
std::cin >> x.at(i) >> y.at(i);
std::cout << std::accumulate(x.begin(), x.end(), 0L, std::bit_xor<int>()) << ' '
<< std::accumulate(y.begin(), y.end(), 0L, std::bit_xor<int>()) << '\n';
}
return 0;
}
here is a test case that your code doesn't work:
1
2
1 1
1 4
4 6
6 1
9 6
9 3
4 3
the output of your code is (6,3),but it should be (6,4).
I guess you can check more cases where the rectangles intersects.
from functools import reduce
for _ in range(int(input())):
n=int(input())
li=[]
li1=[]
for i in range(4*n-1):
m,n=map(int,input().split())
li.append(m)
li1.append(n)
r =reduce(lambda x, y: x ^ y,li)
print(r,end=' ')
r =reduce(lambda x, y: x ^ y,li1)
print(r,end=' ')
print()
Here is the question:
There is a house with a backyard which is square bounded by
coordinates (0,0) in the southwest to (1000,1000) in
the northeast. In this yard there are a number of water sprinklers
placed to keep the lawn soaked in the middle of the summer. However,
it might or might not be possible to cross the yard without getting
soaked and without leaving the yard?
Input The input starts with a line containing an integer 1≤n≤1000, the
number of water sprinklers. A line follows for each sprinkler,
containing three integers: the (x,y)(x,y) location of the sprinkler
(0≤x,y,≤10000) and its range r (1≤r≤1000). The sprinklers will soak
anybody passing strictly within the range of the sprinkler (i.e.,
within distance strictly less than r).
The house is located on the west side (x=0) and a path is needed to
the east side of the yard (x=1000).
Output If you can find a path through the yard, output four real
numbers, separated by spaces, rounded to two digits after the decimal
place. These should be the coordinates at which you may enter and
leave the yard, respectively. If you can enter and leave at several
places, give the results with the highest y. If there is no way to get
through the yard without getting soaked, print a line containing
“IMPOSSIBLE”.
Sample Input
3
500 500 499
0 0 999
1000 1000 200
Sample output
0.00 1000.00 1000.00 800.00
Here is my thought process:
Define circle objects with x,y,r and write a function to determine if a given point is wet or not(inside the circle or not) on the circumference is not wet btw.
class circle {
int h;
int k;
int r;
public:
circle();
circle(int h, int k, int r){
this->h = h;
this->k = k;
this->r = r;
};
bool iswet(pair<int,int>* p){
if (pow(this->r - 0.001, 2) > (pow(p->first - this->h, 2) +
pow(p->second - this->k, 2) ) ) {
return true;
}
else
return false;
};
Then implement a depth first search, prioritizing to go up and right whenever possible.
However since circles are not guaranteed to be pass on integer coordinates an the result is expected in floats with double precision (xxx.xx). So if we keep everything in integers the grid suddenly becomes 100,000 x 100,000 which is way too big. Also the time limit is 1 sec.
So I thought ok lets stick to 1000x1000 and work with floats instead. Loop over int coordinates and whenever I hit a sprinkle just snap in the perimeter of the circle since we are safe in the perimeter. But in that case could not figure out how DFS work.
Here is the latest trial
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <deque>
#include <utility>
#include <unordered_set>
#include <iomanip>
using namespace std;
const int MAXY = 1e3;
const int MAXX = 1e3;
const int MINY = 0;
const int MINX = 0;
struct pair_hash {
inline std::size_t operator()(const std::pair<int,int> & v) const {
return v.first*31+v.second;
}
};
class circle {
int h;
int k;
int r;
public:
circle();
circle(int h, int k, int r){
this->h = h;
this->k = k;
this->r = r;
};
bool iswet(pair<float,float>* p){
if (pow(this->r - 0.001, 2) > (pow(p->first - this->h, 2) + pow(p->second - this->k, 2) ) ) {
this->closest_pair(p);
return true;
}
else
return false;
};
void closest_pair(pair<float,float>* p){
float vx = p->first - this->h;
float vy = p->second - this->k;
float magv = sqrt(vx * vx + vy * vy);
p->first = this->h + vx / magv * this->r;
p->second = this->k + vy / magv * this->r;
}
};
static bool test_sprinkles(vector<circle> &sprinkles, pair<float,float>* p){
for (int k = 0; k < sprinkles.size(); k++)
if (sprinkles[k].iswet(p)) return false;
return true;
}
int main(){
int n; // number of sprinkles
while (cin >> n){
vector<circle> sprinkles_array;
sprinkles_array.reserve(n);
int h, k, r;
while (n--){
cin >> h >> k >> r;
sprinkles_array.push_back(circle(h, k, r));
}/* code */
pair<float,float> enter = make_pair(0, MAXY);
deque<pair<float,float>> mystack;
mystack.push_back(enter);
pair<float,float>* cp;
bool found = false;
unordered_set<pair<float, float>, pair_hash> visited;
while (!mystack.empty()){
cp = &mystack.back();
if (cp->first == MAXX) {
found = true;
break;
}
visited.insert(*cp);
if (cp->second > MAXY || cp->second < MINY || cp ->first < MINX ) {
visited.insert(*cp);
mystack.pop_back();
continue;
}
if (!test_sprinkles(sprinkles_array,cp)) {
continue;
}
pair<int,int> newpair = make_pair(cp->first, cp->second + 1);
if (visited.find(newpair) == visited.end()) {
mystack.push_back(newpair);
continue;
}
else visited.insert(newpair);
newpair = make_pair(cp->first + 1 , cp->second);
if (visited.find(newpair) == visited.end()) {
mystack.push_back(newpair);
continue;
}
else visited.insert(newpair);
newpair = make_pair(cp->first, cp->second - 1);
if (visited.find(newpair) == visited.end()) {
mystack.push_back(newpair);
continue;
}
else visited.insert(newpair);
newpair = make_pair(cp->first - 1, cp->second);
if (visited.find(newpair) == visited.end()) {
mystack.push_back(newpair);
continue;
}
else visited.insert(newpair);
mystack.pop_back();
}
cout << setprecision(2);
cout << fixed;
if (found){
double xin = mystack.front().first;
double yin = mystack.front().second;
pair <float, float> p = mystack.back();
p.second++;
for (int k = 0; k < sprinkles_array.size(); k++)
if (sprinkles_array[k].iswet(&p)) break;
double xout = p.first;
double yout = p.second;
cout << xin << " " << yin << " " << xout << " " << yout << endl;
}
else
{
cout << "IMPOSSIBLE" << endl;
}
}
}
Yes #JosephIreland is right. Solved it with grouping intersecting (not touching) circles. Then these groups have maxy and min y coordinates. If it exceeds the yard miny and maxy the way is blocked.
Then these groups also have upper and lower intersection points with x=0 and x=1000 lines. If the upper points are larger than the yard maxy then the maximum entry/exit points are lower entery points.
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <iomanip>
using namespace std;
const int MAXY = 1e3;
const int MAXX = 1e3;
const int MINY = 0;
const int MINX = 0;
struct circle {
int h;
int k;
int r;
float maxy;
float miny;
circle();
circle(int h, int k, int r){
this->h = h;
this->k = k;
this->r = r;
this->miny = this->k - r;
this->maxy = this->k + r;
};
};
struct group {
float maxy = -1;
float miny = -1;
vector<circle*> circles;
float upper_endy = -1;
float upper_starty = -1;
float lower_endy = -1;
float lower_starty = -1;
void add_circle(circle& c){
if ((c.maxy > this->maxy) || this->circles.empty() ) this->maxy = c.maxy;
if ((c.miny < this->miny) || this->circles.empty() ) this->miny = c.miny;
this->circles.push_back(&c);
// find where it crosses x=minx and x= maxx
float root = sqrt(pow(c.r, 2) - pow(MINX - c.h, 2));
float y1 = root + c.k;
float y2 = -root + c.k;
if (y1 > this->upper_starty) this->upper_starty = y1;
if (y2 > this->lower_starty) this->lower_starty = y2;
root = sqrt(pow(c.r, 2) - pow(MAXX - c.h, 2));
y1 = root + c.k;
y2 = -root + c.k;
if (y1 > this->upper_endy) this->upper_endy = y1;
if (y2 > this->lower_endy) this->lower_endy = y2;
};
bool does_intersect(circle& c1){
for(circle* c2 : circles){
float dist = sqrt(pow(c1.h - c2->h,2)) + sqrt(pow(c1.k - c2->k,2));
(dist < (c1.r + c2->r)) ? true : false;
};
};
};
int main(){
int n; // number of sprinkles
while (cin >> n){
vector<circle> sprinkles_array;
sprinkles_array.reserve(n);
int h, k, r;
while (n--){
cin >> h >> k >> r;
sprinkles_array.push_back(circle(h, k, r));
}/* code */
vector<group> groups;
group newgroup;
newgroup.add_circle(sprinkles_array[0]);
groups.push_back(newgroup);
for (int i = 1; i < sprinkles_array.size(); i++){
bool no_group = true;
for (group g:groups){
if (g.does_intersect(sprinkles_array[i])){
g.add_circle(sprinkles_array[i]);
no_group = false;
break;
}
}
if (no_group) {
group newgroup;
newgroup.add_circle(sprinkles_array[i]);
groups.push_back(newgroup);
}
}
float entery = MAXY;
float exity = MAXY;
bool found = true;
for (group g : groups){
if ((g.miny < MINY) && (g.maxy > MAXY)){
found = false;
break;
}
if (g.upper_starty > entery)
entery = g.lower_starty;
if (g.upper_endy > exity)
exity = g.lower_endy;
}
cout << setprecision(2);
cout << fixed;
if (found){
cout << float(MINX) << " " << entery << " " << float(MAXX) << " " << exity << endl;
}
else
{
cout << "IMPOSSIBLE" << endl;
}
}
}
I am doing a former assignment in C++ and trying to figure out how to get correct ASCII X-Y plot.
So what's really happening is that user is given a menu which allows them to choose a particular trig function.All of the functions have range between [-4..6] for X and [-12...5] for Y.Next user will be allowed to select amount of graduations(or values between the restricted x and y range] and if they want to see resultant values or Bitmap.The final output will be in values/bitmap.I have pasted wolfram alpha link for the functions in comments.
What I have done is incremented each column in 2D output by product of 1/(graduation-1)(i.e. 1/3 if graduated value is 4) and column #.Once that product reaches 1,I am not getting correct output for the values.
#include <iostream>
#include <cmath>
using namespace std;
double minX=-4;
double maxX=6;
double minY=-12;
double maxY=5;
void displayValues(double result[],int result_size,int graduationVal,double percentIncrease,int menuSelection){
int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;
int k=0;
int i=0;
int division=0;
//Add Values into array until result-1 values
while(i < result_size){
//cout<<"Before j";
//Calculate all the horizontal axis values
for(int j=0; j< graduationVal;j++){
if(displaySelection==1){
//cout<<"In if";
if(menuSelection==1){
result[i]=sin(minX+(percentIncrease*j))*cos(minY+(percentIncrease*k));
cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
//cout<<sin(minX+(percentIncrease*j))<<"\n";
//cout<<cos(minY+(percentIncrease*k))<<"\n";
cout<<result[i]<<" ";
}else if(menuSelection==2){
result[i]=sin(minX+(percentIncrease*j))+pow(cos(minY+(percentIncrease*j)),2)-(minX+(percentIncrease*j))/(minY+(percentIncrease*k));
cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
cout<<result[i]<<" ";
}else if(menuSelection==3){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(0.5 *cos(minY+(percentIncrease*k)));
cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
cout<<result[i]<<" ";
}else if(menuSelection==4){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(minX+(percentIncrease*j)) * cos(3 * (minY+(percentIncrease*k)));
cout<<result[i]<<" ";
}
//cout<<"J is"<<j<<"\n";
//cout<<"K is"<<k<<"\n";
//cout<<"I is"<<i<<"\n";
}else{
if(menuSelection==1){
result[i]=sin(minX+(percentIncrease*j))*cos(minY+(percentIncrease*k));
cout<<((result[i] > 0 )?"O":"X");
}else if(menuSelection==2){
result[i]=sin(minX+(percentIncrease*j))+pow(cos(minY+(percentIncrease*j)),2)-(minX+(percentIncrease*j))/(minY+(percentIncrease*k));
cout<<((result[i] > 0 )?"O":"X");
}else if(menuSelection==3){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(0.5 *cos(minY+(percentIncrease*k)));
cout<<((result[i] > 0 )?"O":"X");
}else if(menuSelection==4){
result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(minX+(percentIncrease*j)) * cos(3 * (minY+(percentIncrease*k)));
cout<<((result[i] > 0 )?"O":"X");
}
}//End display choice if
//Increment Array Index
i++;
//cout<<"Bottom of j";
}//End of j loop
cout<<"\n";
//Increment y-values
if(k<graduationVal){
k++;
}
}//End of While
}
int main(){
int menuChoice=-1;
int displaychoice=0;
double distanceFromMinMaxX=4+6;
double distanceFromMinMaxY=12+5;
int graduations=0;
// double precisionX;
// double precisionY;
double pctIncrease;
while(menuChoice!=0){
cerr<<"Select your function\n";
cerr<<"1. sin(x)cos(y)\n";
cerr<<"2. sin(x)+cos^2(x)-x/y\n";
cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
cerr<<"0. Quit\n";
cin>>menuChoice;
if(menuChoice == 0){
return 0;
}
cerr<<"Number of graduations per axis: ";
cin>>graduations;
pctIncrease=1/(double)(graduations - 1);
int values_size=graduations * graduations;
double values[values_size];
/*int yValues[graduationVal];*/
// precisionX=distanceFromMinMaxX/graduations;
// precisionY=distanceFromMinMaxY/graduations;
displayValues(values,values_size,graduations,pctIncrease,menuChoice);
}
}
Edit:
#include <iostream>
#include <cmath>
using namespace std;
double minX=-4;
double maxX=6;
double minY=-12;
double maxY=5;
double* calculateValues(double val[],int val_size,int graduationVal,double xPrecision,double yPrecision,int menuSelection){
/*int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;*/
int k=0;
int i=0;
//Add Values into array until result-1 values
while(i < val_size){
for(int j=0; j<graduationVal;j++){
if(menuSelection==1){
val[i]=sin(minX+(xPrecision*j))*cos(minY+(yPrecision*k));
}else if(menuSelection==2){
val[i]=sin(minX+(xPrecision*j))+pow(cos(minY+(xPrecision*j)),2)-(minX+(xPrecision*j))/(minY+(yPrecision*k));
}else if(menuSelection==3){
val[i]=(0.5 * sin(minX+(xPrecision*j)))+(0.5 *cos(minY+(yPrecision*k)));
}else if(menuSelection==4){
val[i]=(0.5 * sin(minX+(xPrecision*j)))+(minX+(xPrecision*j)) * cos(3 * (minY+(yPrecision*k)));
}
//Increment Array Index
i++;
}//End of j loop
//Increment y-values
if(k<graduationVal){
k++;
}
}//End of While
return val;
}
void displayValues(double result[],int result_size,int numOfGraduations){
int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;
int k=0;
int i=0;
while(i< result_size){
for(int j=0;j<numOfGraduations;j++){
if(displaySelection==1){
cout<<result[i]<<" ";
}else{
cout<<((result[i] > 0 )?"O":"X");
}
i++;
}
cout<<"\n";
}
}
int main(){
int menuChoice=-1;
int displaychoice=0;
double distanceFromMinMaxX=4+6;
double distanceFromMinMaxY=12+5;
int graduations=0;
double precisionX;
double precisionY;
double pctIncrease;
while(menuChoice!=0){
cerr<<"Select your function\n";
cerr<<"1. sin(x)cos(y)\n";
cerr<<"2. sin(x)+cos^2(x)-x/y\n";
cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
cerr<<"0. Quit\n";
cin>>menuChoice;
if(menuChoice == 0){
return 0;
}
cerr<<"Number of graduations per axis: ";
cin>>graduations;
pctIncrease=1/(double)(graduations - 1);
int values_size=graduations * graduations;
double values[values_size];
/*int yValues[graduationVal];*/
precisionX=distanceFromMinMaxX/graduations;
precisionY=distanceFromMinMaxY/graduations;
/*cout << "# of graduations: " << graduations << endl;
cout << "Precision: "<< endl;
cout << "x: " << precisionX << endl;
cout << "y: " << precisionY << endl;*/
calculateValues(values,values_size,graduations,precisionX,precisionY,menuChoice);
displayValues(values,values_size,graduations);
}
}
Edit:I am using gcc
This produces output similar to the screencap you linked (where the user chose option 2). The TextPlot2d function takes a lambda or functor which calculates the desired function, as well as two PlotRange structs for specifying how the x and y axes should be scaled. I also used different output characters because I had a hard time distinguishing 'X' vs. 'O'.
#include <iostream>
#include <cmath>
struct PlotRange {
double begin, end, count;
double get_step() const { return (end - begin) / count; }
};
template <typename F>
void TextPlot2d(F func, PlotRange range_x, PlotRange range_y) {
const auto step_x = range_x.get_step();
const auto step_y = range_y.get_step();
// multiply steps by iterated integer
// to avoid accumulation of error in x and y
for(int j = 0;; ++j) {
auto y = range_y.begin + step_y * j;
if(y >= range_y.end) break;
for(int i = 0;; ++i) {
auto x = range_x.begin + step_x * i;
if(x >= range_x.end) break;
auto z = func(x, y);
if(z != z) { std::cout << '?'; } // NaN outputs a '?'
else { std::cout << (z < 0 ? '#':'o'); }
}
std::cout << '\n';
}
}
int main() {
TextPlot2d(
[](double x, double y){ return std::sin(x) + std::cos(y/2)*std::cos(y/2) - x/y; },
{-4.0, 6.0, 40.0},
{-12.0, 5.0, 40.0}
);
}
Tada!
ooooooo######ooooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
ooooo#########oooooooooooooooooooooooooo
oooo###########ooooooooooooooooo######oo
ooo#############ooooooooooooooo#######oo
ooo#############ooooooooooooooo########o
oo##############ooooooooooooooo########o
ooo#############ooooooooooooooo########o
ooo#############oooooooooooooooo######oo
oooo###########oooooooooooooooooo####ooo
ooooo#########oooooooooooooooooooooooooo
ooooo#########oooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
ooooooo######ooooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
ooooo#########oooooooooooooooooooooooooo
ooo############ooooooooooooooooooooooooo
oo#############ooooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
################oooooooooooooooooooooooo
oooooooooooooooooo######################
oooooooooooooooooooooo##################
oooooooooooooooooooooooo################
ooooooooooooooooooooooooo###############
ooooooooooo###ooooooooooo###############
ooooooooo#######ooooooooo###############
oooooooo########oooooooooo##############
ooooooo#########oooooooooo##############
ooooooo#########ooooooooooo#############
ooooooo########oooooooooooo#############
oooooooo######oooooooooooooo############
I finally got the output!!Appearantly function#2 was wrong and that's why I wasn't getting correct answer.
#include <iostream>
#include <cmath>
using namespace std;
double minX=-4;
double maxX=6;
double minY=-12;
double maxY=5;
void calculateValues(double val[],int val_size,int graduationVal,double xPrecision,double yPrecision,int menuSelection){
/*int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;*/
//double val[];
int k=0;
int i=0;
//Add Values into array until result-1 values
while(i < val_size){
for(int j=0; j<graduationVal;j++){
if(menuSelection==1){
val[i]=sin(minX+(xPrecision*j))*cos(minY+(yPrecision*k));
}else if(menuSelection==2){
val[i]=sin(minX+(xPrecision*j))+pow(cos((minY+(yPrecision*k))/2),2)-(minX+(xPrecision*j))/(minY+(yPrecision*k));
}else if(menuSelection==3){
val[i]=(0.5 * sin(minX+(xPrecision*j)))+(0.5 *cos(minY+(yPrecision*k)));
}else if(menuSelection==4){
val[i]=(0.5 * sin(minX+(xPrecision*j)))+(minX+(xPrecision*j)) * cos(3 * (minY+(yPrecision*k)));
}
//Go to next value in array
i++;
}//End of j loop
//Increment y-values
if(k<graduationVal){
k++;
}
}//End of While
}
void displayValues(double result[],int result_size,int numOfGraduations){
int displaySelection=0;
cerr<<"(0) Bitmap or (1) Values?";
cin>>displaySelection;
int k=0;
int i=0;
while(i< result_size){
for(int j=0;j<numOfGraduations;j++){
if(displaySelection==1){
cout<<result[i]<<" ";
}else{
cout<<((result[i] > 0 )?"O":"#");
}
i++;
}
cout<<"\n";
}
}
int main(){
int menuChoice=-1;
int displaychoice=0;
double distanceFromMinMaxX=maxX-minX;
double distanceFromMinMaxY=maxY-minY;
int graduations=0;
double precisionX;
double precisionY;
double pctIncrease;
while(menuChoice!=0){
cerr<<"Select your function\n";
cerr<<"1. sin(x)cos(y)\n";
cerr<<"2. sin(x)+cos^2(y/2)-x/y\n";
cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
cerr<<"0. Quit\n";
cin>>menuChoice;
if(menuChoice == 0){
return 0;
}
cerr<<"Number of graduations per axis: ";
cin>>graduations;
//pctIncrease=1/(double)(graduations - 1);
int values_size=graduations * graduations;
double values[values_size];
/*int yValues[graduationVal];*/
precisionX=(distanceFromMinMaxX)/graduations;
precisionY=(distanceFromMinMaxY)/graduations;
/*cout << "# of graduations: " << graduations << endl;
cout << "Precision: "<< endl;
cout << "x: " << precisionX << endl;
cout << "y: " << precisionY << endl;*/
calculateValues(values,values_size,graduations,precisionX,precisionY,menuChoice);
displayValues(values,values_size,graduations);
}
}
When I try to start my C++ program it stops with error "5.exe has stopped working". This program supposed to calculate how many tiles you need for pool, if number of tiles on one side is non-round number, add one row of tiles to it. P.S. Sorry for my bad English.
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
int main()
{
int x,y,z;
int a,b;
cout << "Insert dimensions of pool in metres: " << endl;
cin >> x >> y >> z;
cout << "Insert dimensions of tile in centimeters: " << endl;
cin >> a >> b;
a=a/100;
b=b/100;
int brx = 0, brzx = 0, bry = 0, brzy = 0, bxpod = 0, bypod = 0;
if (x%a == 0) {
brx = x / a;
}
else {
brx = x / a + 1;
}
if (z%b == 0) {
brzx = z / b;
}
else {
brzx = z / b + 1;
}
if (y%a == 0) {
bry = y / a;
}
else {
bry = y / a + 1;
}
if (z%b == 0) {
brzy = z / b;
}
else {
brzy = z / b + 1;
}
if (x%a == 0) {
bxpod = x / a;
}
else {
bxpod = x / a + 1;
}
if (y%b == 0) {
bypod = y / b;
}
else {
bypod = y / b + 1;
}
int s = (brx*brzx + bry*brzy) * 2 + bxpod*bypod;
cout << "You need " << s << "tiles." << endl;
system("pause");
return 0;
}
Using a debugger, you can easily find that you have a division by 0 in the following lline:
if (x%a == 0) {
brx = x / a;
}
You are doing an integer division on "a":
a = a / 100;
So if a is lower than 100, a will be 0. 10 / 100 = 0.1 = 0 when cast as int.
You should use double instead of int
Here is my code so far. There seems to be soemthing wrong since I keep getting an incorrect answer. I am writing in a text file that is formatted:
2
3.0 1.0
2 being the size of the array and then 3.0 and 1.0 being the coefficients. Hopefully I didnt miss much in my explanation. Any help would be greatly appreciated.
Thanks
double polyeval(double* polyarray, double x, int arraySize)
{
//int result = 0;
if(arraySize == 0)
{
return polyarray[arraySize];
}
//result += x*(polyarray[arraySize]+polyeval(polyarray,x,arraySize-1));
return polyarray[arraySize-1]+ (x* (polyeval(polyarray,x,arraySize-1)));
//return result;
}
int main ()
{
int arraySize;
double x;
double *polyarray;
ifstream input;
input.open("polynomial.txt");
input >> arraySize;
polyarray = new double [arraySize];
for (int a = arraySize - 1; a >= 0; a--)
{
input >> polyarray[a];
}
cout << "For what value x would you like to evaluate?" << endl;
cin >> x;
cout << "Polynomial Evaluation: " << polyeval(polyarray, x, arraySize);
delete [] polyarray;
}
the idea that if i read in a text file of that format varying in size that it will solve for any value x given by the user
Jut taking a wild guess
for (int a = arraySize - 1; a >= 0; a--)
// ^^
{
input >> polyarray[a];
}
One error is here:
for (int a = arraySize - 1; a > 0; a--)
{ //^^should be a >=0
input >> polyarray[a];
}
You are missing some entry this way.
The recursive function should look like the following:
int polyeval(double* polyarray, double x, int arraySize)
{
if(arraySize == 1)
{
return polyarray[arraySize-1];
}
return x*(polyarray[arraySize-1]+polyeval(polyarray,x,arraySize-1));
}
The problem is mainly with the definition of the polynomial coefficients.
Your code assumes a polynomial on the form:
x( p(n) + x( p(n-1) + x( p(n-2) + ... x(p(1) + p(0)))..))
This line:
result += x*(polyarray[arraySize]+polyeval(polyarray,x,arraySize-1));
Should become:
result += pow(x,arraySize)*polyarray[arraySize]+polyeval(polyarray,x,arraySize-1);
This way the polynomial is defined correctly as p(n)x^n + p(n-1)x^(n-1) ... + p1 x + p0
Couldn't work out exactly what you were trying to do, or why you were using recursion. So I whipped up a non-recursive version that seems to give the right results.
#include <iostream>
using namespace std;
double polyeval(const double* polyarray, double x, int arraySize) {
if(arraySize <= 0) { return 0; }
double value = 0;
const double * p = polyarray + (arraySize-1);
for(int i=0; i<arraySize; ++i) {
value *= x;
value += *p;
p--;
}
return value;
}
int main () {
const int arraySize = 3;
const double polyarrayA[3] = {0.0,0.0,1.0}; // 0 + 0 x + 1 x^2
const double polyarrayB[3] = {0.0,1.0,0.0}; // 0 + 1 x + 0 x^2
const double polyarrayC[3] = {1.0,0.0,0.0}; // 1 + 0 x + 0 x^2
cout << "Polynomial Evaluation A f(x) = " << polyeval(polyarrayA, 0.5, arraySize)<<std::endl;
cout << "Polynomial Evaluation B f(x) = " << polyeval(polyarrayB, 0.5, arraySize)<<std::endl;
cout << "Polynomial Evaluation C f(x) = " << polyeval(polyarrayC, 0.5, arraySize)<<std::endl;
}
You can see it running here:
http://ideone.com/HE4r6x