473,779 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error while executing a matrix multiplication program

3 New Member
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 3747
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
girishc13
3 New Member
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 Contributor
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 Recognized Expert Moderator Expert
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 Contributor
It crashes because 'i' runs out of range.
Apr 14 '10 #6
girishc13
3 New Member
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",&mat 1[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",&mat 2[i][j]);
matmul(mat1,mat 2,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
13280
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 matricies at compile-time? Any help would be appreciated. Hopefully this code comes in useful for someone, let me know if you find it useful, or if you have suggestions on how to improve it. // Public Domain by Christopher Diggins, 2005 ...
5
2910
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 | | 0 0 1 0 0 1 0 1 || b4 | | a5 | | 1 0 0 1 0 0 1 0 || b5 |
20
5246
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 used various techniques ( loop unrolling, loop jamming...) and tried some matrix libraries : newmat (slow for large matrix) , STL (fast but ..not usefull) , hand coding (brain consuming...), and recently Meschach...
0
4217
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 but for the starssen's matrix multiplication we need 7 multiplication and 14 additions which is very important from time complexity point of view.please give a solution if anyone knows. ratikanta panda
4
1950
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 m,n,p,q,i,j,k;
7
7590
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 the other one 40000x3. the program runs too slow: /*multiply two matrice: xyz_trans * xyz , the output is w 3x3*/
6
121920
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 not help me. Does any body know what is the problem?. Thanks. OtherFunctions.obj : error LNK2001: unresolved external symbol "int
1
1962
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 check to see if the two matrices are able to be multiplied. This is the code that I have so far: #include "stdafx.h" #include <iostream> using namespace std;
1
9168
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 recMultiply(int i, int j, float a, int k, int l, float b, int x, int y, float c, int s); int i, j, k, s, matrixsize, blocksize, jj, kk, power, bsize; float sum, maxr, total=0.0, startmult, finishmult, multtime; float* A = NULL; float* B = NULL;
8
7899
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 multiplication A*B*A' , where - A' refers to the transpose of matrix A - matrix B is symmetric matrix. - (optional) it will be much better if the result of (A*B) or (A*B)' can be stored as temperal matrix, as this value is required thereafter.
0
10137
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8959
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7483
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.