### Need help with my code

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include 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: "<

The annotations are in Spanish because i'm from Argentina. The variable "may" is intended to hold the maximum number and "men" the minimum.

Thanks in advance and sorry for any language mistakes there may be in the text :)
All of your assignments (`lista[1] = may;`) are backward. You should be assigning the value in the array to the variable representing mayor, not putting the value of mayor into the array. Same for menor.
Last edited on
You could also approach this another way: keep track of the min/max values as values are entered.

 ``12345678910111213141516171819202122232425`` ``````#include #include int main(int argc, const char* argv[]) { int min = std::numeric_limits::max(); int max = std::numeric_limits::min(); int input, list[10]; std::cout << "Please enter ten numbers" << std::endl; for(int i=0; i < 10; ++i) { std::cin >> input; list[i] = input; if(input < min) min = input; if(input > max) max = input; } std::cout << "Smallest number is " << min << std::endl; std::cout << "Largest number is " << max << std::endl; return 0; }``````

Or, if you prefer, you could keep track of which index holds the min/max value in a similar way.

(Note, you also want your array to have 10 elements, not nine)
Thank you. I know understand the problem. The course says now to read this "sentences" like ecuations. It's something so stuck in my head...

Again thank you, problem solved!

iHutch105: Thank you for the idea, it's much simpler to assign the values on the run. The only problem I have is that i don't understand your code (I'm a complete newbie).

And according to the tutorial i'm reading the number of elements in the array is just fine because it uses the number 0. Unless there is a problem with that and i don't know it.
Last edited on
Topic archived. No new replies allowed.