vendredi 23 février 2018

Given double variables x and y what does "double z = ( 1 / 2 ) * ( x + y );" do

#include <iostream>
using namespace std;

int main() {

double x=0.0; 
cout << "x" << "? "; 
cin >> x; 

double y=0.0; 
cout << "y" << "? "; 
cin >> y;


double z = ( 1 / 2 ) * ( x + y );

cout << "z" << " = " << z << endl;

return 0;  

}

I have the written the code above for dev c++ and want to know why I get z=0 every time. I understand that it doesn't give me the average of x and y and that there is another way to do it, but I just want to understand why this doesn't work. When it creates a new variable z does it not initialise it with 0, is it initialised with some random number instead?

(Sorry for any mistakes in my understanding of this, I am very new to coding!)

Aucun commentaire:

Enregistrer un commentaire