Calling an Array in C ++

0

The program consists in storing elements in an array and then displaying them in   screen. The function of storing the array works but then when calling the   array in the other function shows on the screen negative and very long numbers.

I guess the error is in:

  

tArrayNumber to

     

const tArrayNumber a

 #include<iostream>
 using namespace std;

 const int MAX=3;
 typedef int tArrayNumero[MAX];
 void valoresArray();
 void mostrarArray(const tArrayNumero a);

 int main()
 {
     tArrayNumero a;
     valoresArray();
     mostrarArray(a);
     return 0;
 }

void valoresArray()
{
    tArrayNumero a;
    cout << "Introduzca los valores del array";
    for(int i=0; i<MAX; i++)
    {
        cin >> a[i];
    }
}

 void mostrarArray(const tArrayNumero a)
 {
     for(int i=0; i<MAX; i++)
     {
         cout << a[i];
     }
 }
    
asked by Vendetta 15.12.2017 в 23:37
source

1 answer

2

The problem is simple, at first you create an array and call it "a" and then call the function valuesArray (), your problem is that within the function you create another array with the same name instead of receiving the one You have created and give values. This would be the correct way:

#include<iostream>
using namespace std;

const int MAX = 3;
typedef int tArrayNumero[MAX];
void valoresArray(tArrayNumero a);
void mostrarArray(const tArrayNumero a);

int main()
{
    tArrayNumero a;
    valoresArray(a);
    mostrarArray(a);
    system("PAUSE");
    return 0;
}

void valoresArray(tArrayNumero a)
{
    cout << "Introduzca los valores del array" << endl;
    for (int i = 0; i<MAX; i++)
    {
        cin >> a[i];
    }
}

void mostrarArray(const tArrayNumero a)
{
    for (int i = 0; i<MAX; i++)
    {
        cout << a[i] << endl;
    }
}

I hope it helps you

    
answered by 16.12.2017 / 17:29
source