HadK
HadK

Reputation: 61

How to modify some arrays and not modifying others when we pass them in an argument in C++?

I have the following C++ code. I am okay with modification on the content of array x[] as we pass it through the function modify but I don't want the content of the array z to be modified as we pass it. I am getting several errors in my attempt to do that. Could you help me please? It seems I have errors with the initialization of the matrix m in the function also.

#include<iostream>
using namespace std;
void modify(int y[], int f[], const int size)
{
    int m[size];
    for (int i = 0; i < size; i++)
    {
        m[i] = f[i];
    }
    for (int i = 0; i <= 5; i++)
    {
        y[i] = 2 * y[i];
        m[i] = 2 * m[i];
        cout << "y[" << i << "]=" << y[i] << "\t"<<endl;
        cout << "m[" << i << "]=" << m[i] << "\t"<<endl;
    }
    cout << endl;
}
int main(){
    int x[6] = {1,2,3,4,5,6};
    int z[6] = {1,2,3,4,5,6};
    for (int i = 0; i <= 5; i++)
    {   cout << "x[" << i << "]=" << x[i]<<"\t"<<endl;
        cout << "z[" << i << "]=" << z[i] << "\t"<<endl;
    }
    cout << endl;
    modify(x,z,6);
    for (int i = 0; i <= 5; i++)
    {
        cout << "x[" << i << "]=" << x[i] << "\t" << endl;
        cout << "z[" << i << "]=" << z[i] << "\t" << endl;
    }
    cout << endl;
    system("pause");
    return 0;
}

Upvotes: 2

Views: 56

Answers (3)

HadK
HadK

Reputation: 61

Thank you all for your help. This is the final code after the fix. I don't want to change on z but without using const.

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

void modify(int [], int [], int);

int main()
{

int x[6] = {1,2,3,4,5,6};
int z[6] = {1,2,3,4,5,6};
for (int i = 0; i <= 5; i++)
{
    cout << "x[" << i << "]=" << x[i]<<"\t"<<endl;
    cout << "z[" << i << "]=" << z[i] << "\t"<<endl;
}
cout << endl;
modify(x,z,6);
for (int i = 0; i <= 5; i++)
{
    cout << "x[" << i << "]=" << x[i] << "\t" << endl;
    cout << "z[" << i << "]=" << z[i] << "\t" << endl;
}
cout << endl;
system("pause");
return 0;
}

void modify(int y[], int f[], int size)   
{
vector<int> m;
for (int i = 0; i < size; i++)
{
    m.push_back(f[i]);
}
for (int i = 0; i <= 5; i++)
{
    y[i] = 2 * y[i];
    m[i] = 2 * m[i];
    cout << "y[" << i << "]=" << y[i] << "\t"<<endl;
    cout << "m[" << i << "]=" << m[i] << "\t"<<endl;
}
cout << endl;
}

Upvotes: 1

Fran&#231;ois Andrieux
Fran&#231;ois Andrieux

Reputation: 29022

In c++, you can use the const keyword to specify that something must not be changed.

The statement void modify(int y[], const int f[], const int size) indicates that f will not be changed by the function, therefore you may provide a const argument. Attempting to modify the object through f will produce a compilation error.

The statement const int z[6] = {1,2,3,4,5,6}; indicates that z must not be changed. If you accidentally try to modify z or use it in a context where it may change, the compiler will produce a compilation error.

Upvotes: 1

Joseph Thomson
Joseph Thomson

Reputation: 10393

Make the parameter f of modify an array of const int. This way, no modifications can be made to the contents of f.

void modify(int y[], const int f[], const int size)

Upvotes: 4

Related Questions