0

C++ I don't understand something/no entiendo algo

#include <iostream> using namespace std; int main() { int n; cin >> n; int max = 0; int actual; int* p = new int[n]; for(int i=0; i<n; i++) { cin >> p[i]; actual = p[i]; if(actual > max) { max = actual; } } cout << max; delete[] p; return 0; } Why the before code give the number more longer but the next code: #include <iostream> using namespace std; int main() { int n; cin >> n; int max; int actual; int* p = new int[n]; for(int i=0; i<n; i++) { cin >> p[i]; actual = p[i]; if(actual > max) { max = actual; } } cout << max; delete[] p; return 0; } give the direction of the archives? Por q el anterior codigo (el primero q puse), envia el numero mas grande pero el segundo envia la direccion donde se guardan los archivos?

20th Jul 2024, 10:58 PM
Chumexis
Chumexis - avatar
2 Respostas
+ 2
It is because you used an uninitialized int. int max; An uninitialized variable can be 0, but it can also be any number, usually very big. So you will bave problems if you expect to start from 0. If you are doing any incrementing, counting or indexing, it is better to use int max = 0;
20th Jul 2024, 11:36 PM
Bob_Li
Bob_Li - avatar
+ 2
Thanks you a lot friend :3
21st Jul 2024, 2:12 AM
Chumexis
Chumexis - avatar