Doubt about Array with c ++ top

0

I am learning to program in C ++. In the last class we saw something that the teacher called " Array with cap ". I wanted to deepen this concept in a self-taught way I looked for it but I could not find anything similar.

An example of the code we handle is:

#define N 20
#define TAM 100
typedef char palabra[N];
struct fecha { int dd; int mm; int aa; };

struct empleado { palabra nombre; palabra apellido; int cedula; fecha
nacimiento; };

fecha leo_fecha() { fecha f; cin >> f.dd >> f.mm >> f.aa; return f; }



empleado leo_empleado() {
empleado e;
cout << "Ingrese nombre: " << endl;
cin >> e.nombre;
cout << "Apellido: " << endl;
cin >> e.apellido;
cout << "Ingrese cedula: " << endl;
cin >> e.cedula;
cout << "Ingrese su fecha de nacimiento: ";
e.nacimiento=leo_fecha();
return e; }

struct arr_emp { empleado e [N]; int tope; };

arr_emp creo_vacioE() {
arr_emp a;
a.tope=0; }

bool es_vacioE(arr_emp a) {
return (a.tope==0); }

void agrego(arr_emp &a, empleado e) { a.e[a.tope]=e; }

void imprimoE(arr_emp a) {

for (int i=0; i<a.tope; i++)
imprimo_empleado(a.e[i]); }

int este_enE (arr_emp a, int cedula) {
for(int i=0; i<a.tope; i++)
{
    if(a.e[1].cedula==cedula)
    {
    return i;
    }
    else
        {
        return -1;
        }
 } }

main () {
arr_emp a;
empleado e;
a=creo_vacioE();
if (es_vacioE(a)==0)
{
cout << "El arreglo esta vacio" << endl;
}
e=leo_empleado();
agrego(a,e);

}

I would be grateful if you could tell me what concept this refers to. Thanks.

    
asked by Federico Ventura 19.07.2018 в 00:43
source

2 answers

1

What your teacher is referring to is 2 different concepts

Boundaries or Limites in Spanish, which represent the maximum size of any object pixels, arrays etc, How many apples do you fit in this box? boundary = 20.

Arrays of primitive form are treated by crossing pointers without limit, therefore, you need a limit to not get out.

So you always need an array (pointer at the beginning) and a limit eg. twenty which means that we have an array that goes from start to start + 20 This would be the "cap" that your teacher expresses you.

    
answered by 21.07.2018 в 19:37
0

The concept of primitive array in C is just a pointer to a memory address where the array starts.

You can see it as an arrow that points to where you should start looking for any element that is in the array . When writing a style instruction:

int a = arr[5];

What is really done is to go to the position where the array starts and advance 5 more positions, given that the elements of an array Primitive are all followed in memory.

This way of handling arrays is very basic, and does not include things like limits, that is, what is the capacity of the array or how many items you have , which are two different things.

In the case you raise, your tope represents how many elements are currently in the array . You can deduce it from the function:

bool es_vacioE(arr_emp a) {
    return (a.tope==0); 
}

To determine if an array is empty, look if tope equals 0.

Beyond this, I think the function

void agrego(arr_emp &a, empleado e) { 
    a.e[a.tope]=e; 
}

is incomplete. First, it would be necessary to verify that the array is not already full, and second, it is necessary to update the tope , because the array has one more element

void agrego(arr_emp &a, empleado e) {
    if(a.tope < N){
        a.e[a.tope]=e; 
        a.tope++;
    }
}
    
answered by 25.07.2018 в 08:44