Although the response of Mauricio Brito points out the solution, there are details that have been omitted and that are worth Worth mentioning.
Only four of six variables initialized.
int vector[5];
int i;
int suma=0, producto=1, mayor=vector[0], menor=vector[0];
Both vector
and i
lack initial value (s), it would be advisable to give a value to uninitialized variables:
int vector[5] {}; // todos los elementos del arreglo vector contienen 0
int i = 0;
int suma=0, producto=1, mayor=vector[0], menor=vector[0];
Why is it so important to give these initial values? Because of not acerlo, they get residual values from the memory and these can affect the operation of the program ... for example:
at the time I print the minor I always get that is 2 (so do not enter the number 2).
I bet that the array vector
obtained several values from the memory and in the position 0
would have a 2
, therefore, when initializing menor
this variable obtains that residual value and this residual value is less than any of the ones you enter.
Since the variable i
you use later and the first thing you do with it is to give it a value it would not be necessary to initialize it; even so I intend to do it.
You do not choose the best values to initialize mayor
and menor
.
Even if you had given value to the elements of the vector
fix, none of those values is the best choice for mayor
or menor
; the best choices are respectively the smallest integer and the largest integer, which you can get with the header <limits>
:
int vector[5] {}; // todos los elementos del arreglo vector contienen 0
int i = 0;
int suma = 0, producto = 1;
int mayor = std::numeric_limits<int>::min(); // Menor entero posible
int menor = std::numeric_limits<int>::max(); // Mayor entero posible
These choices to initialize mayor
and menor
guarantee that whatever the input value, any value will be greater than the one that already contains mayor
and less than the one that already contains menor
.
You do not need the fix or three of the loops for
.
Why do things in two steps and do them in one?
int suma = 0, producto = 1;
int mayor = std::numeric_limits<int>::min();
int menor = std::numeric_limits<int>::max();
for (int i = 0; i < 5; ++i){ // i declarado y usado solo aqui
std::cout << "Ingrese el numero " << (i + 1) << '\n';
int numero = 0;
std::cin >> numero;
mayor = std::max(mayor, numero); // std::max pertenece a la libreria <algorithm>
menor = std::min(menor, numero); // std::min pertenece a la libreria <algorithm>
suma += numero;
producto *= numero;
}
With these changes the program behaves the same and you go from using four loops for
to using only one, so the computer works less.
Also, using some of the functions of the algorithm header ( <algorithm>
) you select more easy the major and minor number:
-
std::max
: Returns the highest number of passwords for parameters.
-
std::min
: Returns the smallest number of parameters you pass.
Using these functions and eliminating some lines, makes the code somewhat more readable.
You use unnecessary headers that are also not C ++.
You are including the header <conio>
that is part of c and its mission is to communicate with the console while you include the header <iostream>
which has exactly the same mission but being the latter from c ++ , the program should not even compile exactly as you have shared it.
C ++ headers do not have a file extension: <iostream>
, <vector>
, <string>
, etc ... except the headers that exist for compatibility with C: <math.h>
, <stdlib.h>
, <stdint.h>
, etc ...
Most headers in C
have a version front page at C++
where the extension has been removed, the prefix "c" has been added (for example <cmath>
, <cstdint>
) and have been adapted to C++
. If you are working with C++
you should use the latter not the C
.
Missing the namespace.
Before cin
and cout
you need the namespace, without it the program does not compile; you can save yourself writing the std::
continuously by entering the using namespace std
instruction. I advise you to take a look at this thread to know more details.
Abuse of std::endl
.
Normally it's not a good idea, I suggest you take a look at this thread to Know more details.
With all the suggested corrections applied, the code [would be like this] :
#include <iostream>
#include <limits>
#include <algorithm>
int main () {
int suma = 0, producto = 1;
int mayor = std::numeric_limits<int>::min();
int menor = std::numeric_limits<int>::max();
for (int i = 0; i < 5; ++i) {
std::cout << "Ingrese el numero " << (i + 1) << '\n';
int numero = 0;
std::cin >> numero;
mayor = std::max(mayor, numero);
menor = std::min(menor, numero);
suma += numero;
producto *= numero;
}
std::cout << "La suma es: " << suma << '\n'
<< "El producto es: " << producto << '\n'
<< "El mayor es: " << mayor << '\n'
<< "El menor es: " << menor << '\n';
return 0;
}
By the way, in the loops for
use pre-increment always that you can .