How to use the "pointer formalism" to calculate the average of a table?

0

I have an exercise in C ++ / C to make the average of an array using the "pointer formalism" ("formalisme pointeur" in French). For example, this next one uses the "pointer formalism" to manipulate the table:

int tab[10];

for{int i = 0 ; i < 10 ; i ++ )
{
   *(tab + i) = 0
}

But this one uses the "table formalism":

int tab[10];

for{int i = 0 ; i < 10 ; i ++ )
{
   tab[i] = 0
}

I do not understand well if they do the same thing ...

However I have done the following to calculate the average of a table of 10 numbers with the "table formalism" how to do it with the "pointer formalism"?

#include<iostream>
#include <fstream>

using namespace std;

//Écrire, de deux manières différentes, un programme qui lit 10 nombres entiers et les stocke dans un tableau statique de taille 10 avant de calculer la media des éléments de ce tableau

void mediaTableau(int tabula[]){

    int media = 0;

    for(int i = 0; i<sizeof(tabula); i++){
        media = tableauEntiers[i] +moyenne;
    }

    cout << "media tabula " << media/sizeof(tabula) <<endl;
}

int main(){

    static int tabula[10];

    cout << "Da diez numeros" <<endl;

    for(int i = 0; i<10; i++){
            cin >> tabula[i];
            cout << "numero " << i+1 <<endl;
        }

        mediaTableau(tabula);

    return 0;

}
    
asked by ThePassenger 10.02.2017 в 18:14
source

1 answer

1

Indeed,

tabla[indice] == *( puntero + indice )

is one of the C / C ++ facilities for working with pointers, which allows you to work with them in the way that suits you best. It allows you to abstract the logical usage (how you organize your data) from the physical use (as the compiler handles the data in memory).

Regarding your question, you have to change your function mediaTableau( ) , adding a new argument: the length of the table, since the pointers do not provide that information:

void mediaTableau( int *tabula, size_t size ){
  int media = 0;

  for(int i = 0; i < size; ++i )
    media += *( tabula + i );

  cout << "media tabula " << media / size <<endl;
}

And, consequently, you have to change the way to call it, indicating explicitly the size in elements of it:

mediaTableau( tabula, sizeof( tabula ) / sizeof( tabula[0] ) );

EDITO

As indicated in the comments, a array is nothing more than a succession of elements in memory; when we do something like

int lista[5] = { 1, 2, 3, 4, 5 };

the compiler generates in memory exactly what it seems, a sequence of 5 data of type int , with the values that we indicate:

  

| 1 | 2 | 3 | 4 | 5 |

The concept of array or array does not exist in the core of C / C ++ languages. They are nothing more than sequences of bytes, and, in the indicated example, lista is not more than a pointer to that sequence of bytes that the compiler generates.

The above is for the declaration of variables. For its use, the concepts pointer and array are interchangeable , and allow what we saw in the question:

  

list [0] == * list == * (list + 0)
  list [1] == * (list + 1) == list [0] [1]
  list [2] == * (list + 2) == list [0] [2] == list [1] [1]

Before expressions of type

variable[indice1][indice2]...[indiceN]

the compiler interprets them as

*( variable + ( indice1 + indice2 + ... + indiceN ) )

And even indices negative

are allowed
variable[-1] == *( variable - 1 )

Careful with what we do. So much freedom has a price: the compiler does not check the indexes we use . From their point of view, there we . If we do

lista[100] = 0

We will be writing out of the memory block pointed to by lista , and we will cause any odd thing . From operating system errors, to changing the value of other variables.

    
answered by 10.02.2017 / 18:33
source