Related
I'm a neophyte with c++. I wrote this code but the result for q have to be 1.0, but the code give me, changing the variable's order when I recall function "intercetta", for example -34, 0, 9.75. Why?
#include <iostream>
using namespace std;
float coefficienteAngolare(float x1, float x2, float y1, float y2, float m) {
return m = ((y2 - y1) / (x2 - x1));
}
float intercetta(float m, float x1, float y1, float q) {
return q = y1 - m * x1;
}
int main() {
float x1, x2, y1, y2, m=0, q=0;
x1 = 3.5;
x2 = 6.5;
y1 = 9.75;
y2 = 17.25;
cout << "m= " << coefficienteAngolare(x1, x2, y1, y2, m) << endl;
cout << "q= " << intercetta(x1, y1, m, q) << endl;
}
This function
float coefficienteAngolare(float x1, float x2, float y1, float y2, float m) {
return m = ((y2 - y1) / (x2 - x1));
}
has parameters passed by value. It means that it receives copies of the parameters you give. Whatever you do inside the function, cannot alter the parameters passed to it in main().
If you really want to modify m, you have to pass it by reference
float coefficienteAngolare(float x1, float x2, float y1, float y2, float& m) {
return m = ((y2 - y1) / (x2 - x1));
}
But then, if you modify m, why do you need to return it?
Most probably you either want to not return anything and just store the result in m
void coefficienteAngolare(float x1, float x2, float y1, float y2, float& m) {
m = ((y2 - y1) / (x2 - x1));
}
//....
// in main()
coefficienteAngolare(x1, x2, y1, y2, m);
cout << "m= " << m << endl;
Or you want to return the resulting value, without passing a variable to store it.
float coefficienteAngolare(float x1, float x2, float y1, float y2) {
return ((y2 - y1) / (x2 - x1));
}
//....
// in main()
m = coefficienteAngolare(x1, x2, y1, y2);
cout << "m= " << m << endl;
Along the same line you have to modify intercetta.
Please notice that the order of the parameters is relevant. The compiler cannot guess that the q variable in main() should be the same as the q variable in intercetta, they belong to different scopes.
The variable m (and q) in your main function are different variables than the variables in your other functions. The assignment you have after your return statement assigns a value to a variable which has its lifetime limited to the respective function's scope.
If you want to pass-by-reference, you can do this by declaring the argument as a reference:
float intercetta(float m, float x1, float y1, float& q) {
// ^-------- reference
I have this assignment that asks to use functions in order to calculate the distance and slope of two points. The thing is I don't really understand how I can use a single function for two separate variables(has to be done this way for the assignment). In this case, the formula asks for x1 and x2, but I have to use a single function. So my question is how can I use my getX and getY functions that prompts for x and y twice? Basically use a single function for both first and second values. Hope that makes more sense.
/*Include statements*/
#include <stdio.h>
#include <math.h>
/*Given Prototypes*/
double getX();
double getY();
double distance(double, double, double, double);
double slope(double, double, double, double);
// begin main
int main()
{
// declare variables
double x1;
double x2;
double y1;
double y2;
// read in variables for points on a graph
x1 = getX();
y1 = getY();
x2 = getX();
y2 = getY();
// print out the distance between the points and the slope of the line
printf("\n");
printf("Distance between the points is %.2f \n", distance(x1, x2, y1, y2));
printf("Slope of the line is %.2f \n\n", slope(x1, x2, y1, y2));
}
/* begin getX function */
double getX()
{
double x;
printf( "Please enter the value of x:");
scanf("%lf", &x);
return x;
}
/* begin getY function */
double getY()
{
double y;
printf("Please enter the value of y:");
scanf("%lf", &y);
return y;
}
/* header for distance function */
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
/* header for slope function */
double slope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
you can use the "Struct" in C this will define new datatype that can sotre two or more separate values as below code
-- please if you have any questions
Code is Here :
/Include statements/
#include <stdio.h>
#include <math.h>
/*Given Prototypes*/
double getX();
double getY();
double distance(double, double, double, double);
double slope(double, double, double, double);
// begin main
int main()
{
// declare variables
double x1;
double x2;
double y1;
double y2;
// read in variables for points on a graph
x1 = getX();
y1 = getY();
x2 = getX();
y2 = getY();
// print out the distance between the points and the slope of the line
struct SlopDistance FinalValues;
FinalValues = CalcuateValues(x1, y1, x2, y2);
printf("\n");
printf("Distance between the points is %.2f \n", FinalValues.Distance);
printf("Slope of the line is %.2f \n\n", FinalValues.Distance);
}
/* begin getX function */
double getX()
{
double x;
printf("Please enter the value of x:");
scanf_s("%lf", &x);
return x;
}
/* begin getY function */
double getY()
{
double y;
printf("Please enter the value of y:");
scanf_s("%lf", &y);
return y;
}
/* header for distance function */
double distance(double x1, double y1, double x2, double y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
/* header for slope function */
double slope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
struct SlopDistance
{
double Slope;
double Distance
};
struct SlopDistance CalcuateValues(double x1, double y1, double x2, double y2)
{
struct SlopDistance FinalValue;
FinalValue.Distance = distance(x1, x2, y1, y2);
FinalValue.Slope = slope(x1, x2, y1, y2);
return FinalValue;
}
Sorry, I edited my question now .Pay attention to the bold type words .
I really need a recursive constructor while defining a kdtree class .
But I'm afraid I'm not doing it the right way .
How can I do it more elegantly ?
This is my code using the this pointer ,it compiles and works well .
Don't do anything at all ,just showing the brief idea of what a recursive constructor should look like .
#include <iostream>
using namespace std;
class foo
{
public:
int a, b;
foo(unsigned int k)//this piece of code just shows the brief idea of what i'm trying to do.
{
if (k)
*this = foo(--k);
else
a = k, b = k;
}
};
int main()
{
foo f(3);
cout << f.a << f.b << endl;
getchar();
}
This is my kdtree sample code .This is what I'm actully trying to achieve ,still don't compile ,I'll edit it later.
class kdtree
{
public:
int16_t count;//数组里面可以只存mask和key生成的unique_key,因为树结构,和count可以后期生成
int16_t key;
int16_t mask;
inline bool is_full()
{
return mask + count == 0x8000;
};
shared_ptr<kdtree> left, right;
kdtree(){}
kdtree(int x1, int y1, int z1, int x2, int y2, int z2, int _x = 0, int _y = 0, int _z = 0, int len = 0, int ikey = 0x8000)
{
int i = 0x80 >> len / 3, j = 0x4000 >> len;
if ((x2 - x1)*(y2 - y1)*(z2 - z1) == j << 10)
{
count = j << 1;
key = ikey;
mask = ~ikey ^ (ikey - 1);
return;
}
switch (len++ % 3)
{
case 0:
if (x1 < _x&&x2 < _x)
{
*this = kdtree(x1, y1, z1, x2, y2, z2, _x, _y, _z, len, ikey -= j);
return;
}
if (x1 >= _x&&x2 >= _x)
{
*this = kdtree(x1, y1, z1, x2, y2, z2, _x + i, _y, _z, len, ikey += j);
return;
}
left = shared_ptr<kdtree>(new kdtree(x1, y1, z1, _x, y2, z2, _x, _y, _z, len, ikey -= j));
right = shared_ptr<kdtree>(new kdtree(_x, y1, z1, x2, y2, z2, _x + i, _y, _z, len, key += j));
count = j << 1;
key = ikey;
mask = ~ikey ^ (ikey - 1);
return;
case 1:
if (y1 < _y&&y2 < _y)
{
*this = kdtree(x1, y1, z1, x2, y2, z2, _x, _y, _z, len, ikey -= j);
return;
}
if (y1 >= _y&&y2 >= _y)
{
*this = kdtree(x1, y1, z1, x2, y2, z2, _x, _y + i, _z, len, ikey += j);
return;
}
left = shared_ptr<kdtree>(new kdtree(x1, y1, z1, x2, y2, z2, _x, _y, _z, len, ikey -= j));
right = shared_ptr<kdtree>(new kdtree(x1, y1, z1, x2, y2, z2, _x, _y + i, _z, len, ikey += j));
count = j << 1;
key = ikey;
mask = ~ikey ^ (ikey - 1);
return;
case 2:
if (x1 < _x&&x2 < _x)
{
*this = kdtree(x1, y1, z1, x2, y2, z2, _x, _y, _z, len, ikey);
return;
}
if (x1 >= _x&&x2 >= _x)
{
*this = kdtree(x1, y1, z1, x2, y2, z2, _x, _y, _z + i, len, ikey + j);
}
left = shared_ptr<kdtree>(new kdtree(x1, y1, z1, x2, y2, _z, _x, _y, _z, len, ikey));
right = shared_ptr<kdtree>(new kdtree(x1, y1, _z, x2, y2, z2, _x, _y, _z + i, len, ikey + j));
count = j << 1;
key = ikey;
mask = ~ikey ^ (ikey - 1);
return;
}
}
};
A constructor only builds one thing, so you can't use a constructor to build a group of things.
If you use new Class[ 20]; // 20 Classes get allocated, but each is constructed once in the constructor.
class Class
{
Class * left;
Class * right;
Class( SomeObject & x )
{
eatSomeData( x );
left = nullptr;
right = nullptr;
if (x->buildleft() )
left = new Class( x );
if (x->buildright() )
right = new Class( x );
}
};
At each call to the constructor, the constructor only deals with the object it is creating, the fact it is doing this recursively (based on the data in x), is sort of different. In this case the class is heavily bound into the tree, and can't be easily constructed without building a tree. Yes it is possible (from comments), but really really not advisable.
If you have a group of items you want to store (e.g. a tree), the typical building blocks are
Item - the thing (object) you store in the tree.
Node - an object which understands the tree, and how to traverse the tree.
TreeContainer - an object which holds the top of the tree, and which knows how to find stored Items
Builder - an object or function which takes your data and adds it into the tree by calling methods of the TreeContainer
I've got a problem in my code. I'm rewriting an existing module that draw graph (from electrical consomation) with GDI+ (yes Visual Studio Microsoft and co)
Here is my code to draw one graph
void CourbeComptage::afficheCourbeJour_nonEDF(CWnd *cwnd)
{
dessin ligne,rectangle,texte;
int i;
int lx = x + margeH;
int ly = y + haut - margeV;
int x1, y1, x2, y2;
if(flagCourbe)
{
afficheGraduation();
// 4 - tracer la courbe de consommation avec des plages horaires
for(i=0;i<24;i++)
{
x1 = lx + i * pasX;
y1 = ly - coorX_conso[i];
x2 = lx + (i + 1) * pasX;
y2 = ly - 1;
plage[i] = (plage[i] > 3) ? 3 : (plage[i] < 1) ? 1 : plage[i];
if(typeCourbe == COURBE_BARRE_3D)
{
drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, gradient[plage[i]], "transparent", 1);
switch(plage[i])
{
case 1: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HC", "transparent", 1); break;
case 2: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HP", "transparent", 1); break;
case 3: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_P", "transparent", 1); break;
}
}
else if(typeCourbe == COURBE_BARRE)
{
switch(plage[i])
{
case 1: drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, "conso2_HC", "transparent", 1); break;
case 2: drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, "conso2_HP", "transparent", 1); break;
case 3: drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, "conso2_P", "transparent", 1); break;
}
}
else if(this->typeCourbe == COURBE_LIGNE)
{
if(i!= 23)
{
switch(plage[i])
{
case 1: drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], "conso2_HC"); break;
case 2: drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], "conso2_HP"); break;
case 3: drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], "conso2_P"); break;
}
}
// A ecrire 4 types point x 2 lignes de courbe (conso et react)
}
}
}
}
In order to simplify my code and avoid some test I just want to transform that :
switch(plage[i])
{
case 1: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HC", "transparent", 1); break;
case 2: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_HP", "transparent", 1); break;
case 3: drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, "conso_P", "transparent", 1); break;
}
Into that :
drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, gradient[plage[i]], "transparent", 1);
So I rewrite this function like that :
void CourbeComptage::afficheCourbeJour_nonEDF(CWnd *cwnd)
{
static char gradient[4][9] = {"default", "conso_HC", "conso_HP", "conso_P"},
solid[4][10] = {"default", "conso2_HC", "conso2_HP", "conso2_P"};
dessin ligne,rectangle,texte;
int i;
int lx = x + margeH;
int ly = y + haut - margeV;
int x1, y1, x2, y2;
if(flagCourbe)
{
afficheGraduation();
// 4 - tracer la courbe de consommation avec des plages horaires
for(i=0;i<24;i++)
{
x1 = lx + i * pasX;
y1 = ly - coorX_conso[i];
x2 = lx + (i + 1) * pasX;
y2 = ly - 1;
plage[i] = (plage[i] > 3) ? 3 : (plage[i] < 1) ? 1 : plage[i];
if(typeCourbe == COURBE_BARRE_3D)
{
drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, gradient[plage[i]], "transparent", 1);
}
else if(typeCourbe == COURBE_BARRE)
{
drawer->DrawFilledSolidRectangle(x1, y1, x2, y2, solid[plage[i]], "transparent", 1);
}
else if(this->typeCourbe == COURBE_LIGNE)
{
if(i!= 23)
{
drawer->DrawLine(lx + pasX/2 + i*pasX, ly - coorX_conso[i], lx + pasX/2 + (i+1)*pasX, ly - coorX_conso[i+1], solid[plage[i]]);
}
}
}
}
}
And that don't works, meaning that the DrawFilledSolidRectangle (for example) get the string, but it don't draw the rectangle following the color passed in the string.
I don't know why.
I tried to dinamically allocate the two tab gradient and solid but that don't works too.
Here are the prototypes of some functions :
void DrawLine(int x1, int y1, int x2, int y2, char * penName);
void DrawFilledSolidRectangle(int x1, int y1, int x2, int y2, char * colorName, char * borderPen, int borderSize = 1, bool ombre = false);
void DrawFilledGradientRectangle(int x1, int y1, int x2, int y2, char * gradientName, char * borderPen, int borderSize = 1, bool ombre = false);
And here is the code of the DrawFilledSolidRectangle (DrawFilledGradientRectangle is the same except the Brush that is used)
Rect * rects = (Rect *)malloc(borderSize * sizeof(Rect));
Rect * tmp;
int i;
if(ombre) DrawRectangle(x1, y1, x2, y2, borderPen, true);
for(i = 0 ; i < borderSize ; i++)
{
tmp = new Rect(x1+i, y1+i, x2-x1-i, y2-y1-i);
rects[i] = *tmp;
}
DrawRectangles(rects, borderSize, borderPen);
x1 += i;
y1 += i;
x2 -= x1+i;
y2 -= y1-i;
SolidBrush * b = new SolidBrush(couleurs[colorName]);
gNaa->FillRectangle(b, x1, y1, x2, y2);
delete[](rects);
Thanks for your help, and sorry for my english.
Using conditional expressions makes this very simple:
drawer->DrawFilledGradientRectangle(x1, y1, x2, y2, (plage[i] == 1 ? "conso_HC" : (plage[i] == 2 ? "conso_HP" : "conso_P")), "transparent", 1);
I tried searching for awnsers, but all the threads are different langs.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void calcDistance (int x1, int y1, int x2, int y2);
int main()
{
int x1, y1, x2, y2;
cout << "Enter the points in coordinate pair form, ommiting parantheses" << endl;
cin >> x1 >> y1 >> x2 >> y2;
calcDistance (x1, y1, x2, y2);
system("pause");
// how do I cout the dist in main-- says dist isn't declared
}
void calcDistance (int x1, int y1, int x2, int y2)
{
int sideA;
sideA = x2 - x1;
int sideB;
sideB = y2 -y1;
int sideAsqd;
sideAsqd = sideA * sideA;
int sideBsqd;
sideBsqd = sideB * sideB;
int sideCsqd;
sideCsqd = sideAsqd + sideBsqd;
double dist;
dist = sqrt(sideCsqd);
cout << "The calculated distance is "<< dist << endl;
}
How do I make the second cout occur in main. I try just putting it in main, but then I get an error saying that dist is not declared in the scope.
I want to be able to use the dist value in main, while it has been calculated in the function.
Change your function:
double calcDistance (int x1, int y1, int x2, int y2)
{
int sideA = x2 - x1;
int sideB = y2 -y1;
int sideAsqd = sideA * sideA;
int sideBsqd = sideB * sideB;
int sideCsqd = sideAsqd + sideBsqd;
double dist = sqrt(sideCsqd);
return dist;
}
And in main do this:
double res = calcDistance (x1, y1, x2, y2);
cout << "The calculated distance is "<< res << endl;
Given a function, say
void calcDistance (int x1, int y1, int x2, int y2)
{
//...
double dist;
//...
}
The variable dist goes out of scope at the closing brace, so not only can you not refer to it from elsewhere, it won't exist when outside the function.
If you want the value somewhere else, return it:
double calcDistance (int x1, int y1, int x2, int y2)
{
//...
double dist;
//...
return dist;
}
To use it elsewhere just capture the return:
double distance = calcDistance(1,2,3,4);
Now you have another local variable called distance that you can use.