Oliver
Oliver

Reputation: 831

C++ How to use a function for another variable?

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;
}

Upvotes: 1

Views: 94

Answers (5)

mbx
mbx

Reputation: 6525

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

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258578

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".

Upvotes: 0

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247949

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.

Upvotes: 0

iammilind
iammilind

Reputation: 69988

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]);

Upvotes: 4

Ankur
Ankur

Reputation: 33637

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

Upvotes: 4

Related Questions