I have to make a program that takes the average grade of a student, with a for counter, but when I run it and show the values that are stored in the variables, I get a number or set of numbers with letters that I don't understand. So if you could provide me with a solution or a contribution to understand this better, I would appreciate it.
#include <iostream>
using namespace std;
int main(){
float nota,nota1,nota2,nota3,nota4,nota5,resultado;
for(int i=0; i<5; i++){
cout<<"Ingrese la nota: "<<endl;
cin>>nota; cout<<endl;
if (i==0){
nota = nota1;
cout<<"La nota1 es: "<<nota1<<endl;;
}
else if (i==1){
nota = nota2;
cout<<"La nota2 es: "<<nota2<<endl;;
}
else if (i==2){
nota = nota3;
cout<<"La nota 3 es: "<<nota3<<endl;;
}
else if (i==3){
nota = nota4;
cout<<"La nota 4 es: "<<nota4<<endl;;
}
else if (i==4){
nota = nota5;
cout<<"La nota 5 es: "<<nota5<<endl;;
}
}
resultado = ((nota1+nota2+nota3+nota4+nota5) / 5);
cout<<"El promedio es: "<<resultado;
return 0;
}
This is what it looks like, when I run the program.
Ingrese la nota:
5.0
La nota1 es: 8.99963e-039
Ingrese la nota:
4.0
La nota2 es: 5.88745e-039
Ingrese la nota:
3.0
La nota 3 es: 5.8422e+032
Ingrese la nota:
3.0
La nota 4 es: 8.99953e-039
Ingrese la nota:
3.0
La nota 5 es: nan
El promedio es: nan
The values you describe as " numbers with letters " are scientific notation for floating point numbers when the value is too large or too small to be meaningfully represented with traditional representation, for example:
8.99963e-039
is 8.99963×10 -39 which would be an 8 with thirty-nine leading zeros 1 .5.88745e-039
is 5.88745e×10 -39 which would be a 5 with thirty-nine zeros to its left.5.8422e+032
is 5.8422×10 32 which would be 58422 with twenty eight zeros to its right.8.99953e-039
is 8.99953×10 -39 which would be an 8 with thirty-nine zeros to its left.If these numbers mean little to you, it's because they really have little meaning... they're the result of displaying a floating point number that hasn't been initialized 2 , which you do with all the notes:
If you only want to calculate the mean of five values, you don't need to store all five values individually, you can accumulate them and divide by five at the end:
You can see the code working in Try it online!.
1 0.000000000000000000000000000000000000000899963.
2 Uninitialized numbers have an indeterminate value.