473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

error while executing a matrix multiplication program

i have written the below program for matrix mult using pointers and functions.
the code is:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int M = 3,N = 2;
  3. main()
  4. {
  5. int mat1[M][N],mat2[N][M],i,j,res[M][M];
  6. void matmul(int *a,int *b,int *c);
  7. printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N);
  8. for (i = 0;i < M;i++)
  9. for (j = 0;j < N;j++)
  10. scanf("%d",&mat1[i][j]);
  11. printf("\n enter the second matrix elements of size %dx%d \n",N,M);
  12. for (i = 0;i < N;i++)
  13. for (j = 0;j < M;j++)
  14. scanf("%d",&mat2[i][j]);
  15. matmul(&mat1[0][0],&mat2[0][0],&res[0][0]);
  16. printf("\n Matrix 1 = \n");
  17. for (i = 0;i < M;i++)
  18. {
  19. for (j = 0;j < N;j++)
  20. printf("%d \t",mat1[i][j]);
  21. printf("\n");
  22. }
  23. printf("\n Matrix 2 = \n");
  24. for (i = 0;i < N;i++)
  25. {
  26. for (j = 0;j < M;j++)
  27. printf("%d \t",mat2[i][j]);
  28. printf("\n");
  29. }
  30. printf("\n Result = \n");
  31. for (i = 0;i < M;i++)
  32. {
  33. for (j = 0;j < M;j++)
  34. printf("%d \t",res[i][j]);
  35. printf("\n");
  36. }
  37. }
  38.  
  39. void matmul(int *a,int *b,int *c)
  40. {
  41. int i,j,k,d = sizeof(int);
  42. printf("\n size of int = %d \n",d);
  43. for (i = 0;i < M;i++)
  44. {
  45. for (j = 0;j < M;i++)
  46. {
  47. *(c + i * d + j) = 0;
  48. //*(*(c + i) + j) = 0;
  49. for (k = 0;k < N;k++)
  50. *(c + i * d + j) += (*(a + i * d + k)) * (*(b + k * d + j));
  51. //*(*(c + i) + j) += (*(*(a + i) + k)) * (*(*(b + k) + j));
  52. }
  53. }
  54. }
  55.  
im starting pointer exercises, and executing the pgm in fedora 12.
if i execute the above pgm, i get segmentation fault.
if i execute using the commented lines in the func, i get the foll error:
ex28.c: In function ‘matmul’:
ex28.c:49: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)
ex28.c:52: error: invalid type argument of ‘unary *’ (have ‘int’)

help in debugging the errors or mistakes
Apr 10 '10 #1
6 3723
weaknessforcats
9,208 Expert Mod 8TB
The calling aruments of your matmul function are int*. This is not a 2D array but is only the address of a single int.

There is a correct way to pass the address of a 2D array to a function and that is to pass the address of element 0. In a 2D array element 0 is itself an array so you need to pass the address of the array that is the second dimension.

This array:

Expand|Select|Wrap|Line Numbers
  1. int arr[3][4];
is passed to a function using the argument of a pointer to an array of 4 int:

Expand|Select|Wrap|Line Numbers
  1. void MyFunc(int (*arg)[4]), int num);
You also need to pass in the number of elements. In this case the number of 4 int arrays.


Read this: http://bytes.com/topic/c/insights/77...rrays-revealed
Apr 10 '10 #2
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int M = 3,N = 2;
  3. main()
  4. {
  5. int mat1[M][N],mat2[N][M],i,j,res[M][M];
  6. void matmul(int (*a)[N],int (*b)[M],int (*c)[M]);
  7. printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N);
  8. for (i = 0;i < M;i++)
  9. for (j = 0;j < N;j++)
  10. scanf("%d",&mat1[i][j]);
  11. printf("\n enter the second matrix elements of size %dx%d \n",N,M);
  12. for (i = 0;i < N;i++)
  13. for (j = 0;j < M;j++)
  14. scanf("%d",&mat2[i][j]);
  15. matmul(mat1,mat2,res);
  16. printf("\n Matrix 1 = \n");
  17. for (i = 0;i < M;i++)
  18. {
  19. for (j = 0;j < N;j++)
  20. printf("%d \t",mat1[i][j]);
  21. printf("\n");
  22. }
  23. printf("\n Matrix 2 = \n");
  24. for (i = 0;i < N;i++)
  25. {
  26. for (j = 0;j < M;j++)
  27. printf("%d \t",mat2[i][j]);
  28. printf("\n");
  29. }
  30. printf("\n Result = \n");
  31. for (i = 0;i < M;i++)
  32. {
  33. for (j = 0;j < M;j++)
  34. printf("%d \t",res[i][j]);
  35. printf("\n");
  36. }
  37. }
  38.  
  39. void matmul(int (*a)[N],int (*b)[M],int (*c)[M])
  40. {
  41. int i,j,k,d = sizeof(int);
  42. printf("\n size of int = %d \n",d);
  43. or (i = 0;i < N;i++)
  44. for (j = 0;j < M;j++)
  45. {
  46. printf("%d \t",(*(a + i))[j]);
  47. }
  48. for (i = 0;i < M;i++)
  49. {
  50. for (j = 0;j < M;i++)
  51. {
  52. (*(c + i))[j] = 0;
  53. for (k = 0;k < N;k++)
  54. (*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
  55. }
  56. }
  57. }
  58.  
ok i changed the pgm for passing the array parameters.but i still get segmentation fault. and cant print the array in the func using pointers
Apr 14 '10 #3
newb16
687 512MB
and cant print the array in the func using pointers
?
If you can't see the output it's because crash occurs before the output stream is flushed. As for seg fault itself, the reason may be an array index out of bound - then icorrect pointer is fetched and when it's accessed, crash occurs. Did you localize it?
Apr 14 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
Your code compiled and executed OK on Visual Studio.NET 2008. I didn;t check that the answer was correct but therewas no crash.

I did have to make M and N const and I did have to make main() return an int.
Apr 14 '10 #5
newb16
687 512MB
It crashes because 'i' runs out of range.
Apr 14 '10 #6
its working now thanx for ur replies,heres the working pgm:

#include<stdio.h>
int M = 3,N = 2;
void main()
{
int mat1[M][N],mat2[N][M],i,j,res[M][M];
void matmul(int ar1[][N],int ar2[][M],int ar3[][M]);
printf("\n enter the matrix elements row wise the size is %dx%d \n",M,N);
for (i = 0;i < M;i++)
for (j = 0;j < N;j++)
scanf("%d",&mat1[i][j]);
printf("\n enter the second matrix elements of size %dx%d \n",N,M);
for (i = 0;i < N;i++)
for (j = 0;j < M;j++)
scanf("%d",&mat2[i][j]);
matmul(mat1,mat2,res);
printf("\n Matrix 1 = \n");
for (i = 0;i < M;i++)
{
for (j = 0;j < N;j++)
printf("%d \t",mat1[i][j]);
printf("\n");
}
printf("\n Matrix 2 = \n");
for (i = 0;i < N;i++)
{
for (j = 0;j < M;j++)
printf("%d \t",mat2[i][j]);
printf("\n");
}
printf("\n Result = \n");
for (i = 0;i < M;i++)
{
for (j = 0;j < M;j++)
printf("%d \t",res[i][j]);
printf("\n");
}
}

void matmul(int ar1[][N],int ar2[][M],int ar3[][M])
{
int i = 0,j = 0,k = 0,d = sizeof(int),(*a)[N] = ar1,(*b)[M] = ar2,(*c)[M] = ar3;
printf("\n size of int = %d \n",d);
printf("\n size of int = %d \n",d);
for (i = 0;i < M;i++)
{
for (j = 0;j < M;j++)
{
(*(c + i))[j] = 0;
printf("%d \t",(*(c + i))[j]);
}
}
for (i = 0;i < M;i++)
{
for (j = 0;j < M;j++)
{
(*(c + i))[j] = 0;
for (k = 0;k < N;k++)
{
(*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
}
}
}
return;
}

'i' was going out of range cos i was incremented in the j loop also. newbie mistake.the lesson was very good for arrays to function parameter passing.
Apr 15 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
5
by: Tiza Naziri | last post by:
Hi, How to represent this matrix equation in C code: / a0 \ / 0 1 0 1 0 0 1 0 \/ b0 \ | a1 | | 0 0 1 0 1 0 0 1 || b1 | | a2 | | 1 0 0 1 0 1 0 0 || b2 | | a3 |=| 0 1 0 0 1 0 1 0 || b3 | | a4...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
4
by: John Doe | last post by:
segmentation error !!!! hi guys , i wrote this program to multiply two matrices (just the basic code without checkin 4 the condition n==p ) " #include<stdio.h> main() { int a,b,c; int...
7
by: VijaKhara | last post by:
Hi all, Is there any method which can implememt the matrix multiplication faster than using the formula as we often do by hand? I am writing the following code and my matrice: one is 3x40000 and...
6
by: sadegh | last post by:
Hi I have a problem with my program in VC++6 When I compile it, the following errors are listed. I spend a lot of time on the groups.google.com to find its reason, but none of comments could...
1
by: dcatunlucky | last post by:
Ok, I have an assignment to write a program that multiplies two matrices. The matrices dimensions will be user defined as well as the numbers (floating point values) inside of them. The program must...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
8
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.