Here I am passing the array name as a parameter,which i think is pass
by refernece.However I cannot add the arrays.Something is fishy in the
add function,i cannot add the arrays
You can use int* or use int[][], but you need to write the program as required by each case. I will show you an example of both cases.
about your program
- do not use
void. It is in general a waste, sometimes an error. Return something.
- write generically. Compare:
void display(int* sum)
{
printf("The sum is:\n");
for(int i =0;i<3;i++)
{
for(int j =0;j<3;j++)
{
printf("%d ",(sum[i]+j));
}
printf("\n");
}
}
with
void display_a(int s[][3], int l, const char* msg)
{
if( *msg != 0) printf("%s\n",msg);
for(int i =0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%d ", s[i][j]);
}
printf("\n");
}
printf("\n");
return;
};
In the second case you can use the same function to display ANY array s and also write a simple title. Note that in C you can omit all but the last dimension in the vector. In fact C does not have multidimensional array as e.g. FORTRAN has. And passing the number of lines and the message as parameters you have much more than just using as you wrote.
always test the return of scanf(). scanf() is not meant to read from the keyboard. Use at least one space in every prompt. Is is far more confortable to the user.
- Use constants to test your program. Avoid reading for the user as it will waste much time on testing.
An example
output
Using arrays
Matriz A at [1][1] 1
Matriz A at [1][2] 2
Matriz A at [1][3] 3
Matriz A at [2][1] 4
Matriz A at [2][2] 5
Matriz A at [2][3] 6
Matriz B at [1][1] 6
Matriz B at [1][2] 5
Matriz B at [1][3] 4
Matriz B at [2][1] 3
Matriz B at [2][2] 2
Matriz B at [2][3] 1
The 1st matrix:
1 2 3
4 5 6
The 2nd matrix:
6 5 4
3 2 1
The sum (using arrays) is:
7 7 7
7 7 7
Using pointers
Matriz A at [1][1] 2
Matriz A at [1][2] 3
Matriz A at [1][3] 4
Matriz A at [2][1] 5
Matriz A at [2][2] 6
Matriz A at [2][3] 7
Matriz B at [1][1] 7
Matriz B at [1][2] 6
Matriz B at [1][3] 5
Matriz B at [2][1] 4
Matriz B at [2][2] 3
Matriz B at [2][3] 2
The 1st matrix:
2 3 5
5 6 7
The 2nd matrix:
7 6 4
4 3 2
The sum (using pointers) is:
9 9 9
9 9 9
Using t1 t2
The t1 matrix:
1 2 3
4 5 6
7 8 9
The t2 matrix:
9 8 7
6 5 4
3 2 1
t1 + t2 (using pointers) is:
10 10 10
10 10 10
10 10 10
the code
this code uses all that I tried to explain, I believe. Pay attention to the way you write the code when using the pointers: you just use the address and displacement.No brackets are needed.
#include<stdio.h>
void add_a(int[][3],int[][3],int[][3],int);
void display_a(int[][3],int,const char*);
int input_a(int[][3], int, const char*);
void add_p(int*,int*,int*,int);
void display_p(int*,int,const char*);
int input_p(int*, int, const char*);
int main(void)
{
int t1[][3] =
{
{ 1,2,3 },
{ 4,5,6 },
{ 7,8,9 }
};
int t2[][3] =
{
{ 9,8,7 },
{ 6,5,4 },
{ 3,2,1 }
};
int a[3][3];
int b[3][3];
int sum[3][3];
printf("Using arrays\n\n");
input_a(a,2,"Matriz A");
input_a(b,2,"Matriz B");
display_a(a, 2, "The 1st matrix:");
display_a(b, 2, "The 2nd matrix:");
add_a( a,b,sum, 2 );
display_a(sum, 2, "The sum (using arrays) is:");
printf("Using pointers\n\n");
input_p((int*) a,2,"Matriz A");
input_p((int*) b,2,"Matriz B");
display_p( (int*) a, 2, "The 1st matrix:");
display_p( (int*) b, 2, "The 2nd matrix:");
add_p( (int*) a, (int*) b, (int*) sum, 2 );
display_p( (int*) sum, 2, "The sum (using pointers) is:");
printf("\nUsing t1 t2 \n\n");
display_p( (int*) t1, 3, "The t1 matrix:");
display_p( (int*) t2, 3, "The t2 matrix:");
add_p( (int*) t1, (int*) t2, (int*) sum, 3 );
display_p( (int*) sum, 3, "t1 + t2 (using pointers) is:");
return 0;
};
void add_a(
int a[][3],
int b[][3],
int sum[][3],
int l // lines
)
{
for(int i =0;i<l;i++)
for(int j=0;j<3;j++)
sum[i][j] = a[i][j] + b[i][j];
return;
};
void display_a(int s[][3], int l, const char* msg)
{
if( *msg != 0) printf("%s\n",msg);
for(int i =0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%d ", s[i][j]);
}
printf("\n");
}
printf("\n");
return;
};
int input_a(int a[][3], int l, const char* msg )
{
for(int i = 0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%s at [%d][%d] ", msg, i+1,j+1);
int res = scanf("%d",&a[i][j]);
if ( res != 1 ) return -1; // read nothing
}
};
return 0;
};
void add_p( int*a, int* b, int* sum, int l )
{
for(int i =0;i<l;i++)
for(int j=0;j<3;j++)
*(sum + i*l + j) = *(a + i*l + j) + *(b + i*l + j);
return;
};
void display_p(int* s, int l, const char* msg)
{
if( *msg != 0) printf("%s\n",msg);
for(int i =0;i<l;i++)
{
for(int j =0;j<3;j++)
printf("%d ", *( s + i*l + j) );
printf("\n");
};
printf("\n");
return;
};
int input_p(int* a, int l, const char* msg )
{
for(int i = 0;i<l;i++)
{
for(int j =0;j<3;j++)
{
printf("%s at [%d][%d] ", msg, i+1,j+1);
int res = scanf("%d", (a + i*l + j) );
if ( res != 1 ) return -1; // read nothing
}
};
return 0;
};