i have written the below program for matrix mult using pointers and functions.
the code is: - #include<stdio.h>
-
int M = 3,N = 2;
-
main()
-
{
-
int mat1[M][N],mat2[N][M],i,j,res[M][M];
-
void matmul(int *a,int *b,int *c);
-
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[0][0],&mat2[0][0],&res[0][0]);
-
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 *a,int *b,int *c)
-
{
-
int i,j,k,d = sizeof(int);
-
printf("\n size of int = %d \n",d);
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < M;i++)
-
{
-
*(c + i * d + j) = 0;
-
//*(*(c + i) + j) = 0;
-
for (k = 0;k < N;k++)
-
*(c + i * d + j) += (*(a + i * d + k)) * (*(b + k * d + j));
-
//*(*(c + i) + j) += (*(*(a + i) + k)) * (*(*(b + k) + j));
-
}
-
}
-
}
-
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
6 3747
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:
is passed to a function using the argument of a pointer to an array of 4 int: - 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 - #include<stdio.h>
-
int M = 3,N = 2;
-
main()
-
{
-
int mat1[M][N],mat2[N][M],i,j,res[M][M];
-
void matmul(int (*a)[N],int (*b)[M],int (*c)[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 (*a)[N],int (*b)[M],int (*c)[M])
-
{
-
int i,j,k,d = sizeof(int);
-
printf("\n size of int = %d \n",d);
-
or (i = 0;i < N;i++)
-
for (j = 0;j < M;j++)
-
{
-
printf("%d \t",(*(a + i))[j]);
-
}
-
for (i = 0;i < M;i++)
-
{
-
for (j = 0;j < M;i++)
-
{
-
(*(c + i))[j] = 0;
-
for (k = 0;k < N;k++)
-
(*(c + i))[j] += (*(a + i))[k] * (*(b + k))[j];
-
}
-
}
-
}
-
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
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?
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.
It crashes because 'i' runs out of range.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
...
|
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 |
|
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...
|
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
|
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;
| |
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*/
|
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
|
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;
|
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;
|
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.
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |