lundi 27 avril 2020

Problem with global and local arrays in c++

#include<stdio.h>
int arr2[100]={0};


void performTask(int arr1[])
{
    for(i=1;i<50;i++)
    {
        arr1[i]=arr1[i]+arr1[i-1];
    }
    printf("\n%d %d",arr1[48],arr1[49]);
}

int main()
{

    for(int i=0;i<50;i++)
        arr2[i]=i;
    performTask(arr2);

    for(int i=0;i<50;i++)
        printf("\n%d ",arr2[i]);
}

When some operations are performed on arr1 array in performTask() function the original array(i.e. arr2) is also being manipulated. How can I change this code so that the arr2 values remain unchanged when performTask() function is called ?

Aucun commentaire:

Enregistrer un commentaire