HannibalTheCannibal
HannibalTheCannibal

Reputation: 33

Passing a 2D array to a function in C++ gives me the following error

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

\\Errors:
\\'n' was not declared in this scope
\\ expected ')' before ',' token
\\ expected unqualified-id before 'int'
void abst_diff(int arr[][n], int n){
int sum_1=0;
int sum_2=0;

for(int a_i = 0; a_i < n; a_i++)
    sum_1 = sum_1 + a[a_i][a_i];

for(int a_i = 0, a_j = n-1; a_i < n; a_i++, a_j--)
    sum_2 = sum_2 + a[a_i][a_j];

sum_2=abs(sum_2-sum_1);
cout << sum_2;

}

int main(){
int n;
cin >> n;
int arr[n][n];
for(int a_i = 0;a_i < n;a_i++){
   for(int a_j = 0;a_j < n;a_j++){
      cin >> arr[a_i][a_j];
   }
}
abst_diff(arr,n);
return 0;
}

Can someone please help me identify the error here in this code. its a fairly simple logic. the errors that i'm getting are for the line: void abst_diff(int arr[][n], int n){

Upvotes: 1

Views: 111

Answers (2)

HannibalTheCannibal
HannibalTheCannibal

Reputation: 33

i was actually looking to pass a dynamic variable (which is the array), not a defined variable of fixed size.

I took your first advise and managed to come up with a solution of my own:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int **createArr(int **array, int n){

array = new int*[n];
for (int row=0; row<n; row++) {
    array[row] = new int[n];
}
return array;

}

void abst_diff(int **arr,int n){
int sum_1=0;
int sum_2=0;

for(int a_i = 0; a_i < n; a_i++)
    sum_1 = sum_1 + *(*(arr+a_i)+a_i);//arr[a_i][a_i];

for(int a_i = 0, a_j = n-1; a_i < n; a_i++, a_j--)
    sum_2 = sum_2 + arr[a_i][a_j];

sum_2=abs(sum_2-sum_1);
cout << sum_2;

}

int main(){
int n;
cin >> n;
int **arr=createArr(arr,n);
for(int a_i = 0;a_i < n;a_i++){
   for(int a_j = 0;a_j < n;a_j++){
      cin >> arr[a_i][a_j];
   }
}
abst_diff(arr,n);
return 0;
}

This program calculates the abs. difference between the sum of the diagonals. My issue was just passing a dynamic variable to a function.

Thanks for your help! :)

PS. '\' was an honest mistake.

Upvotes: 1

Pavan
Pavan

Reputation: 250

You can't assign a variable to define the size of an array.May Be you could use a macro.

#define SIZE 50

Or you could use dynamic variable. Ex:

#include<iostream>

using namespace std;

int main()
{
    int n ,*a;
    cout<<"Enter size of array= ";
    cin>>n;
    a=new int[n];       //allocate memory 
    for(int i=0;i<n;i++)    
        *(a+i)=i;
    for(int i=0;i<n;i++)
        cout<<*(a+i)<<endl;
    return 0;
} 

And

void abst_diff(int arr[][**n**], int n)

You can't give the size here too.

#include <vector>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;


void abst_diff(int arr[50][50], int n){
int sum_1=0;
int sum_2=0;

for(int a_i = 0; a_i < n; a_i++)
    sum_1 = sum_1 + arr[a_i][a_i];

for(int a_i = 0, a_j = n-1; a_i < n; a_i++, a_j--)
    sum_2 = sum_2 + arr[a_i][a_j];

sum_2=abs(sum_2-sum_1);
cout << sum_2;

}

int main(){
int n;
cin >> n;
int arr[50][50];
for(int a_i = 0;a_i < n;a_i++){
   for(int a_j = 0;a_j < n;a_j++){
      cin >> arr[a_i][a_j];
   }
}
abst_diff(arr,n);
return 0;
}

Please comment using '/' not '\'.

Upvotes: 1

Related Questions