1

How can I use my function output_integer for the array v, without making a new function? I need to print the values that end up in v, but i want to use the same function as i do for m:

#include <iostream>
#include <cmath>
using namespace std;

int m[10];
int v[10];
void input_integer()
{
    for (int i=0; i<10; i++)
    {
        cout<<"Element "<<i+1<<"=";
        cin>>m[i];
    }
}
void output_integer()
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<m[i]<<"\n";
        }
}
void copy_div3()
{
    int k=0;
    for (int i=0; i<10; i++)
    {
        if (m[i]%3==0)
        {
            v[k]=m[i];
            k++;
        }
    }
}

int main()
{
    //input_integer();
    output_integer();
    copy_div3();
    return 0;
}
Oliver
  • 821
  • 5
  • 12
  • 28
  • 2
    If this is homework, please add the appropriate tag. This is a very basic question. If you do not know about function parameters, you should get a [good book](http://stackoverflow.com/questions/388242/), or read a tutorial. – Björn Pollex Oct 19 '11 at 10:30

5 Answers5

4

Make the output_integer to take the array as parameter, so that you can pass it any array

Ankur
  • 33,367
  • 2
  • 46
  • 72
4

You can change the function signature to take the array argument and print it, instead of relying on the global-ness of the variable.

void output_integer(const int (&arr)[10])
{
  cout<<"The values of the array are:\n";
  for (unsigned int i=0; i<10; i++)
  {
    cout<<"Element "<<i+1<<" = "<<arr[i]<<"\n";
  }
}

To make it more generic, you can even think of making it a template:

template<unsigned int SIZE>
void output_integer(const int (&arr)[SIZE]);
iammilind
  • 68,093
  • 33
  • 169
  • 336
1

Just provide a pointer to that array and its size:

void output_integer_array(int* array, int size)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<size; i++)
        {
            cout<<"Element "<<i+1<<" = "<<array[i]<<"\n";
        }
}

usage:

output_integer_array(m, 10);// you may want to store the size as a const variable instead of a magic number
mbx
  • 6,292
  • 6
  • 58
  • 91
0

You can pass arguments to functions.

Define `output_integer like this:

void output_integer(int* array)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<array[i]<<"\n";
        }
}

And then call it like this:

output_integer(v);
output_integer(m);

Actually, it would probably be a good exercise to move the m and v arrays away from the global scope entirely. Define them only inside the main function so that they have to be passed as parameters to any function that needs to access them.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • Note: With this approach, `output_integer` is vulnerable to buffer overflow. If `array` size is less than `10`. Moreover, OP is mentioning `using namespace std;`; so argument name `array` may collide with `std::array`. – iammilind Oct 19 '11 at 10:25
  • @iammilind: true. I wanted to keep it as simple as possible (and use the most descriptive array name possible), since the OP obviously is new to C++. In reality, he shouldn't be using raw arrays at all (either `std::array` or more likely `std::vector`, but again, I tried to keep it simple :) – jalf Oct 19 '11 at 10:44
0

You need to understand the basic concept of functions: all logic should depend on parameters passed to the functions (if non-member). You have a free function (not part of the class), so if you need to print an array, you should pass that array as a parameter:

void output_integer(const int* m)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<m[i]<<"\n";
        }
}

Since you use c++, I also suggest using std::vector instead of arrays:

void output_integer(const std::vector<int>& m)
{
    cout<<"The values of the array are:\n";
        for (int i=0; i<10; i++)
        {
            cout<<"Element "<<i+1<<" = "<<m[i]<<"\n";
        }
}

When you're done with the changes, google for "magic parameters".

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625