Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
Is sequence of mathematical operations in C++ is important?
Hello. Could you explain me why in my code mathematical operation " score = ( a / b ) * 100 " did not work and the code showed " score = 0 " ( the score should have been different), but when I changed sequence, " score = ( a * 100 ) / b ", it worked right. I used 'int' in every variables. Variables and score were positive and total. Thank you :)
5 Respostas
+ 4
yes the sequence is definetly important
could you show us the code?
btw: umiesz po polsku?š
+ 3
ok wiec:
jeÅli liczysz a/b to musisz wiedzieÄ Å¼e komputer trochÄ inaczej to traktuje niż siÄ może wydawaÄ
a i b = liczby typu int wiec nie zapisujÄ
zadnych liczb bo " , "
a/b to 0,5 a 0,5 to nie int wiec zostaje zaokrÄ
glone do liczby 0 ktĆ³ra można bez problemu zapisaÄ w int
nastepnie to już dziecinnie prosta matma š 0*100 to nadal 0
natomiast w 2 przypadku pierw liczysz * 100 a potem /b czyli /100 => zostaje tylko a
mam nadzieje ze pomogÅem, jak coÅ to pisz tutajš
i sorry bo mĆ³j polski trochÄ slabyš
0
Umiem š
#include <iostream>
using namespace std;
int main()
{
// variables
int b = 100;
int a = 50;
int score;
// counting of score
score = (a / b ) * 100 ;
// showing
cout << score;
return 0;
}
0
DziÄki za wytÅumaczenie Kamil, już to zrozumiaÅem š
0
Thanks for that