When I try to build the solution for this program I am receiving the error End1 Not declared for all the end1 statements. Am I missing something?
#include "StdAfx.h
#include <iostream>
using namespace std;
const double PI = 3.14;
int main()
{
double circumference;
double radius;
double area;
cout << "Enter the radius: ";
cin >> radius;
cout << end1;
circumference = 2 * PI * radius;
cout << "Circumference = " << circumference << end1;
area = PI * radius * radius;
cout << "Area = " << area << end1;
return 0;
}
Try replacing the number 1 with the letter l
http://en.cppreference.com/w/cpp/io/manip/endl
replace end1 with endl
cout << "Circumference = " << circumference << endl;
Related
I am trying to solve a very simple task tested by my univeristy's code checker. The code is about a c++ implementation of the equation of the quadratic equation. The code won't work for all cases, I am supplying my code, and if there is some comment, hint, please help me with it.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int a, b, c, delta;
double x1, x2, x;
cin >> a >> b >> c;
delta = ((b*b) - (4 * a * c));
if (a == 0) {
if (b != 0) {
x = (-(c*1.0) / (b));
cout << 1 << " " << x;
}
else
cout << "-1";
}
else
if (delta > 0)
{
x1 = ((-(b + sqrt(delta))*1.0) / (2 * a));
x2 = ((-(b - sqrt(delta))*1.0) / (2 * a));
cout << 2 << " " << x1 << " " << x2;
}
else if (delta == 0)
{
x = (-(b*1.0) / (2 * a));
cout << 1 << " " << x;
}
else
cout << "-1";
return 0;
}
I am dealing with generation of tool path where composed many points in three dimension and I am using CNC machine to generate them. One of the things that I want to calculate is tool path length which is defined the total length of path. So I tried this:
1.6760 3.7901 6.1955
1.2788 4.1872 5.3681
0.2832 5.1828 3.2939
0.1835 5.2173 3.0576
0.1097 5.1205 2.8292
0.0815 4.9185 2.6699
0.0812 4.8728 2.6491
0.0810 4.8270 2.6288
0.0807 4.7810 2.6089
The points are like these.
// math.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::ifstream;
using std::operator>>;
using std::operator<<;
struct point
{
float x ;
float y ;
float z ;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
float sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
}
}
else
cout << "Unable to open file";
pointfile.close();
vector<line>::iterator iter = side.begin();
line temp, closest = *iter;
float minimumDistance = closest.sqDistance(), distance = 0.0;
system("PAUSE");
return 0;
}
-I expect the distance between point and its next point.
-the total length of this line.
What about something like this - suppose you want distance between points and not from start (line 93 with comment):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
#include <conio.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::ifstream;
using std::operator>>;
using std::operator<<;
struct point
{
float x ;
float y ;
float z ;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
float sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
ray.start = ray.next; // set start to last end (?)
}
}
else
cout << "Unable to open file";
pointfile.close();
vector<line>::iterator iter = side.begin();
line temp, closest = *iter;
float minimumDistance = closest.sqDistance(), distance, sumDistance = 0.0;
cout << "Line coords" << endl << "distance, Sum of distances, minimum" << endl;
while(iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumDistance += distance;
if(minimumDistance > distance) minimumDistance = distance;
cout << closest << endl
<< distance << " | " << sumDistance << " | " << minimumDistance << endl;
sumDistance += distance;
iter++;
}
getch();
return 0;
}
Sorry for the bug - sum line was there twice - line after cout was an error, also noticed some precision warning - mixed double/float, so switched to double everywhere, now main loop looks like this:
vector<line>::iterator iter = side.begin();
line closest = *iter;
double distance, sumOfDistances = 0.0;
cout << "Line coords" << endl << "distance | Sum of distances" << endl;
while (iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumOfDistances += distance;
cout << closest << endl << distance << " | " << sumOfDistances << endl; // step info output
iter++;
}
Here complete, a bit shorter version including simple results.txt file output.
You can remove 2 lines with comment info output:
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
using namespace std;
struct point
{
float x;
float y;
float z;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
double sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
ray.start = ray.next; // set start to last end (?)
}
}
else
cout << "Unable to open file";
pointfile.close();
ofstream results("results.txt");
vector<line>::iterator iter = side.begin();
line closest = *iter;
double distance, sumOfDistances = 0.0;
cout << "Line coords" << endl << "distance | Sum of distances" << endl;
while (iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumOfDistances += distance;
results << distance << endl;
cout << closest << endl << distance << " | " << sumOfDistances << endl; // info output
iter++;
}
results << sumOfDistances << " << Sum" << endl;
results.close();
cout << "Complete path distance: " << sumOfDistances << endl; // info output
getch();
return 0;
}
I'm making a program where a person can input a direction and in the if statement, it adds/subtracts x/y axis and it draws a line after it gets over. The problem is that for some reason, it only works at the first iteration and doesn't draw any more lines after that.
I added a cin >> x >> y to test it out but it only draws one line and doesn't draw anymore.
Initially, the choices were in a switch statement but I changed to if because I thought that was causing the error.
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
char b = NULL;
char u = 'œ';
char d = 'd';
int main()
{
cairo_surface_t *surface = cairo_image_surface_create_from_png("background.png");
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 5);
double x = 0, y = 240;
cairo_move_to(cr, x, y);
long int count = 0;
int cl = 0;
int crr = 0;
int choice = 0;
int n;
system("cls");
while (choice != 5)
{
cin >> x >> y;
cairo_line_to(cr, x, y);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, "spiral.png");
cout << "Current no. of points are : " << count << "/4096" << endl;
cout << "Enter direction: \n" << endl;
cout << "1 - Top Left \t 2 - Up \t 3 - Top Right " << endl;
cout << "4 - Left \t 5 - Stop \t 6 - Right" << endl;
cout << "7 - Bot. Left \t 8 - Down \t 9 - Bot. Right" << endl << endl;
cout << "Enter you choice: ";
cin >> choice;
if (choice == 1)
cout << "Test";
else
{
//More choices include the direction the person needs to go and it subtracts/adds to the x/y part
cout << "How many times ?: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
x++;
count++;
cl++;
if (cl == 256)
{
cl = 0;
crr++;
}
}
system("cls");
}
}
}
I expect it to draw lines to a particular direction. Say the person inputs right, it draws a line towards right and so on. But here, no lines get drawn at all (except if I add a cin >> x >> y at the start of the while loop, that draws one line and that's it, no more lines.)
This fails because there is no current point anymore. After cairo_stroke(cr);, you can add cairo_move_to(cr, x, y); and it should start drawing more lines in the way you expect. I think... I'm not quite sure what you are up to with this program.
I am trying to work out the angle of a right angled triangle. I have an array containing the lengths of the two sides of the triangle. I also have an array containing the Euclidean Distance between these two points. How would I find the angle of the triangles? In other words, how would I do the sin and then arcsin methods to find the angle? I am just looking for the angle opposite of the hypotenuse. I'm trying to do this in C++.
Solved it now, misinterpreted what I had been asked to do
Solution: How would I find the angle of the triangles
#include <iostream>
#include <cmath>
using namespace std;
#define radians(x) return x * (180/pi)
int main()
{
double opposite, adjacent, angle1, angle2, angle3, choice, radians, hypotenuse;
cout << "Opposite: ";
cin >> opposite;
cout << "Adjacent: ";
cin >> adjacent;
cout << "Radians or Degrees: (R/D)";
cin >> choice;
if(choice == "R")
{
angle1 = arctan(adjacent/opposite);
hypotenuse = opposite\cos(radians(angle1));
angle2 = arcsin(adjacent\hypotenuse);
cout << "Angle 1: "<< radians(angle1) << endl;
cout << "Angle 2: "<< "90\n";
cout << "Angle 3: "<< radians(angle2) << endl;
cout << "Hypotenuse: " << hypotenuse;
}
else if(choice = "D")
{
angle1 = arctan(adjacent/opposite);
hypotenuse = opposite\cos((angle1));
angle2 = arcsin(adjacent\hypotenuse);
cout << "Angle 1: " << (angle1) << endl;
cout << "Angle 2: " << "90\n";
cout << "Angle 3: " << (angle2) << endl;
cout << "Hypotenuse: " << hypotenuse;
}
return 0;
}
or just
angle2 = 180 - (angle1 + 90)
The relation between sides and angles of triangle is:-
a/sinA = b/sinB = c/sinC
where 'a' is the side opposite angle 'A'.
You know one angle let's say it's A = 90. Then you can calculate other two angles from above equation.
You have the lengths of the sides, if you us tangents, you can find the angle for the corresponding side.
Also, once you find one angle, all you need to do is subtract 90 from it to get the final angle:
tan(angle) = opposite/adjacent;
angle = arctan(opposite/adjacent);
otherAngle = 90 - angle;
I'm at a loss here, so I am looking for any hints to point me in the right direction. I can't figure out how to input the Celsius values that I converted from the Fahrenheit temperatures into the centigrade array. I tried to work in another for loop for that very purpose but it only outputs the last value for C after the calculation from the first for loop. Any ideas? Thanks in advance.
// Temperature Converter
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
int main()
double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
double centigrade[] = { 0 }, C(0);
int i(0);
cout << setw(13) << "Farenheit " << setw(9) << " Centigrade";
cout << endl;
for (double t : temps)
{
C = (t - 32) * 5 / 9;
cout << setw(10) << t << setw(12) << C;
cout << endl;
}
for (i = 0; i <= 12; i++)
{
centigrade[i] = C;
cout << centigrade[i] << endl;
}
return 0;
}
Here is a full working example based on the other answer.
#include <iostream>
using std::cout;
using std::endl;
int main() {
double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
const int count = sizeof(temps) / sizeof(temps[0]);
double centigrade[count];
for (int i = 0; i < count; i++) {
centigrade[i] = (temps[i] - 32) * 5 / 9;
cout << centigrade[i] << endl;
}
return 0;
}
If you want to work without an explicit indexing loop, then replace double centigrade[count]; with std::vector<double> centigrade, and replace the loop with:
for (double t : temps)
centigrade.push_back((t - 32) * 5 / 9);
If you then wanted an array back for some reason, you could use this trick to get an array back:
double* array_version = ¢igrade[0];
Store values in the array in the first loop itself..
for (i=0;i<=12;i++)
{
centigrade[i]= (temps[i] - 32) * 5 / 9;
cout << setw(10) << temps[i] << setw(12) << centigrade[i];
cout << endl;
}
U can generalize the for loop by finding the size of temps array dynamically..maybe
sizeof (temps) / sizeof (temps[0]);
Also allocate memory for centigrade array accordingly.
I am adding a new answer based on clarifications to the OP's question, rather than updating my existing answer, because I feel the context is more clear this way.
If you want to use a range-based loop, and avoid std::vector, there is a way to do it, but this solution is more in the spirit of C thanC++, because it uses pointer arithmetic.
#include <iostream>
using std::cout;
using std::endl;
int main() {
double temps[] = { 65.5, 68.0, 38.1, 75.0, 77.5, 76.4, 73.8, 80.1, 55.1, 32.3, 91.2, 55.0 };
const int count = sizeof(temps) / sizeof(temps[0]);
double centigrade[count];
double * walker = centigrade;
for (double t : temps)
*walker++ = (t - 32) * 5 / 9;
// verify results by printing.
for (double t: centigrade)
cout << t << endl;
return 0;
}