Hello Yuripetrocchi,
To go along with what salem c has said.
"double" is the preferred floating point type over the "float".
Two points about a function:
First it must have a return type. "void" if it returns nothing. "char", "int", "double" or "string". it could also use other types like a class or struct and others.
Second a function can only return one item. As salem c pointed out the last two parameters are better passed by reference, so that they can change what is defined in "main" when they are given a value in the function.
It is always
int main()
because "main" will always return an int,(usually zero), even if you do not tell it to. That is the way it is designed to work.
Your line 21 will not work. One because the function is not setup to return a value. And because the comma operator does not work the way you think it will. If the function did return a value only "ybar" would receive the value because of the comma operator.
My personal choice; it is a good idea to initialize your variables when you define them. If for no other reason than to know that they do not contain garbage.
As for shorting the program there is not much there that you could do differently to make it shorter. I actually added to the code to make it easier to read. I did shorten the "cin" statement to one line. The "cout" statements , lines 18, 19 and 20, I put in one line, but left on three lines for readability.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
using namespace std; // <--- Best not to use.
void baricentro(double ax, double ay, // <--- Needs a return value.
double bx, double by,
double cx, double cy,
double& xbar, double& ybar)
{
xbar = ((ax + bx + cx) / 3);
ybar = ((ay + by + cy) / 3);
}
int main()
{
double ax{}, ay{}, bx{}, by{}, cx{}, cy{}, xbar{}, ybar{};
cout << "Inserisci le coordinate di a,b e c" << endl;
cin >> ax >> ay;
cin >> bx >> by;
cin >> cx >> cy;
//cin >> ax >> ay >> bx >> by >> cx >> cy; // <--- Could be written as.
system("CLS");
cout << "A(" << ax << ";" << ay << ")" << endl;
cout << "B(" << bx << ";" << by << ")" << endl;
cout << "C(" << cx << ";" << cy << ")" << endl;
//cout << "A(" << ax << ";" << ay << ")" << '\n' // <--- Could be written as or even on one line.
// << "B(" << bx << ";" << by << ")" << '\n'
// << "C(" << cx << ";" << cy << ")" << endl;
baricentro(ax, ay, bx, by, cx, cy, xbar, ybar);//accorciabile?
cout << "Il baricentro e'uguale a Bar (" << xbar << " ; " << ybar << ")" << endl; // <--- Added some spaces.
system("PAUSE");
return 0;
}
|
The comments in the code should explain what was done. If not let me know.
When it comes to the compiler it does not care about white space or blank lines. The blank lines and indentation just make it easier to read and follow.
Look close at the last "cout" statement. I added some spaces to make the output look better and easier to read.
Hope that helps,
Andy