Hello Cplusplus community, I'm new here, and i'm also new to C++ and programming in general. I was reading a little course on C++ in spanish (CConClase) and after some chapters we were given some problems for us to solve.
I tried solving the first problem: 1- Create a program that reads ten int numbers from the keyboard and calculate and show in screen the: Sum of them all, the middle point, the maximum value and the minimum value.
When i compile and run the little program it lets me put the ten numbers, it then calculates de sum and the middle point but it fails to calculate the maximum and minimum values, which indicates that part of the code is bad, but i can't figure out what is wrong.
I used the IDE Code::Blocks and the compiler GCC.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <iostream>
using namespace std;
main ()
{
//declaracion de variables y array
int suma, prom, may, men, a = 0;
int lista[9];
cout << "introduzca los 10 números" << endl;
//introducción de números
while (a < 10)
{
int n;
cin >> n;
lista[a] = n;
a++;
}
//calculo de suma y prob
suma = lista[0]+lista[1]+lista[2]+lista[3]+lista[4]+lista[5]+lista[6]+lista[7]+lista[8]+lista[9];
prom = suma / 10;
//calculo de mayor
lista[0] = may;
if (lista[1] > may) lista[1] = may;
if (lista[2] > may) lista[2] = may;
if (lista[3] > may) lista[3] = may;
if (lista[4] > may) lista[4] = may;
if (lista[5] > may) lista[5] = may;
if (lista[6] > may) lista[6] = may;
if (lista[7] > may) lista[7] = may;
if (lista[8] > may) lista[8] = may;
if (lista[9] > may) lista[9] = may;
//calculo de menor
lista[0] = men;
if (lista[1] < men) lista[1] = men;
if (lista[2] < men) lista[2] = men;
if (lista[3] < men) lista[3] = men;
if (lista[4] < men) lista[4] = men;
if (lista[5] < men) lista[5] = men;
if (lista[6] < men) lista[6] = men;
if (lista[7] < men) lista[7] = men;
if (lista[8] < men) lista[8] = men;
if (lista[9] < men) lista[9] = men;
//devolución de resultados
cout <<"la suma es: "<<suma<<", "<<"el promedio es: "<<prom<<", "<<"el mayor es: "<<may<<", "<<"y el menor es: "<<men<<"."<<endl;
return 0;
}
|
The annotations are in Spanish because i'm from Argentina. The variable "may" is intended to hold the maximum number and "men" the minimum.
Can you please help me with this?
Thanks in advance and sorry for any language mistakes there may be in the text :)