I created a code.I want to use the same varible in 2 functions, but I don't want the funtion to change the value to the other function. To make my self more clear here is an example:
int num1(int arr[5][6],int count);
int num2(int arr[5][6],int count2);
int main()
{
int count = 0;
int count2 = 0;
int arr[5][6] = {
{0, 0, 0, 1, 0, 0} ,
{0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0} ,
{0, 0, 0, 0, 0, 0}
};
cout << num1(arr,count);
cout << num2(arr,count2);
return 0;
}
int num1(int arr[5][6],int count){
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
if(arr[i][j] == 1){
count++;
arr[i][j] = 0;
}
}
}
return count;
}
int num2(int arr[5][6],int count2){
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j++) {
if(arr[i][j] == 1){
count2++;
arr[i][j] = 0;
}
}
}
return count2;
}
This code will print 1 and 0 because num1 changes the only '1' in arr to '0' and because of that num2 will get an array which all of the places have 0. what I want is for BOTH of the functions to print 1 so the output will be "11"
insted of 10
. And no, without making a new array I really want to know if there's a way to do it with a single array