Find the last element of the array

1

Good evening people, I have a weird problem. Create a function to find the last element of an array, and I'm over it, I do not understand why

int ultimo_elem (int x[]){
int contador = 0;
while (x[contador]){
    contador += 1;
}
return contador;
}

And using the main like this:

int main (){
int array[] = {23, 43, 35, 38, 67, 12, 76, 10, 34, 8};

cout << ultimo_elem (array) << endl;
}

Thank you in advance. Greetings.

    
asked by Uso Compartido 08.10.2017 в 02:14
source

4 answers

3

Your approach is wrong. Your code does not search for the last element of the array , look for the first element that is 0 , which is not much the same.

In C ++, an array is nothing more than a succession of data in memory. It does not have tagged any information about its size, nor any end of fix mark or anything like that. It is not more than a group of data, mixed with all the other data that is in the memory of the team.

That's why your code fails. Find a 0 , and you will surely find it, but there is no way to know exactly where you will find it. Of course, there are none in the values of your array .

You have to change your approach:

  • You can call your function with an argument that indicates the size.

In that case, you have to change the way you call it and what the function does:

int ultimo_elem (int x[], size_t size ) {
  return ( size / sizeof( x[0] ) ) - 1;
}

cout << ultimo_elem( array, sizeof( array ) ) << endl;
  • You can indicate an exact size in the array that you expect.

But then, you will not be able to use the function with arrays of other sizes:

int ultimo_elem (int x[10]){ return 9; }

cout << ultimo_elem( array ) << endl;
  • You can put your own brand in the last value of the array.

It's the least changes you need:

int array[] = { 23, 43, 35, 38, 67, 12, 76, 10, 34, 8, 0 };
  • You can make a template that works for different sizes.

I think it's still early for you to consider this option: -)

    
answered by 08.10.2017 в 06:24
3

Below I put a "generic" form of a function that receives a reference to an array and returns its last element. I think it's a common way of doing it in modern C ++:

template <typename T, size_t N> 
T ultimo_elem(T(&a)[N])
{
    return a[N - 1];
}

If you only need to know the position of the last element:

template <typename T, size_t N> 
size_t ultimo_elem(T(&a)[N])
{
    return N - 1;
}
    
answered by 08.10.2017 в 07:02
0

Remember that to get the object of an array starts at position 0, this means that if your array is var arr = {1} and you want to obtain the value you have to access your position 0 ie arr [0]

if you want to get the last value of your array it would be arr [arr.count-1];

greetings

    
answered by 08.10.2017 в 02:24
-2

Here you have the program working. The best and most practical thing is to use a formula to calculate the last element (largo / sizeof(arreglo[0]) ) - 1; where largo = sizeof(arreglo);

#include <iostream>
#include <windows.h>

using namespace std;

int ultimo_elem (int arreglo[],int largo);

int main()
{
    /*  Aquí se define el arreglo de int de tamaño de 10 elementos:                         */
    int largo;
    int arreglo[] = {23, 43, 35, 38, 67, 12, 76, 10, 34, 8};
    largo = sizeof(arreglo);
    cout << "El valor almacenado correspondiente al ultimo elemento es " << arreglo[ultimo_elem(arreglo,largo)]<<endl;
    system("pause");
    return 0;
}

int ultimo_elem (int arreglo[],int largo){
    int ultimo=0;
    ultimo = (largo / sizeof(arreglo[0]) ) - 1; //tomar el tamaño del arreglo y lo dividirlo por el tamaño del primer elemento
    return ultimo;
}
    
answered by 08.10.2017 в 03:31