answersLogoWhite

0


Best Answer

void main()

{

int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;

printf("Enter The Rows And Cloumns And Of The First Matrix:");

scanf("%d %d",&m,&n);

printf("\nEnter The Rows And Cloumns And Of The Second Matrix:");

scanf("%d %d",&p,&q);

printf("\nEnter Elements Of The First Matrix:\n");

for(i=0;i< m;i++)

{

for(j=0;j< n;j++)

{

scanf("%d",&a[i][j]);

}

}

printf("\nEnter Elements Of The Second Matrix:\n");

for(i=0;i< p;i++) {

for(j=0;j< q;j++)

scanf("%d",&b[i][j]);

}

printf("The First Matrix Is:\n"); /* Print the first matrix */

for(i=0;i< m;i++) {

for(j=0;j< n;j++)

printf(" %d ",a[i][j]);

printf("\n");

}

printf("The Second Matrix Is:\n"); /* Print the second matrix */

for(i=0;i< p;i++) {

for(j=0;j< q;j++)

printf(" %d ",b[i][j]);

printf("\n");

}

if(n!=p) {

printf("Aborting./nMultiplication Of The Above Matrices Not Possible.");

exit(0);

}

else {

for(i=0;i< m;i++) {

for(j=0;j< q;j++) {

c[i][j] = 0;

for(k=0;k< n;k++) {

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

}

}

printf("\nThe Product Of The Two Matrices Is:\n\n");

for(i=0;i< m;i++) {

for(j=0;j< q;j++) {

printf(" %d ",c[i][j]);

}

printf("\n");

}

}

return 0;

}

-----------------------------------------------------">-----------------------------------------------------

The above code is not the solution of Matrix multiplication using pointers.

the following code is absolutely correct.

void main()

{int *a,*b,*c,row1,col1,row2,col2;

clrscr();

printf("enter rows of 1at matrix");

scanf("%d",&row1);

printf("enter columns of 1at matrix");

scanf("%d",&col1);

printf("enter rows of 2nd matrix");

scanf("%d",&row2);

printf("enter columns of 2nd matrix");

scanf("%d",&col2);

if(col1!=row2)

{

printf("\nsorry\n");

}

else

{

a = (int *) malloc(row1*col1 * 2);

b = (int *) malloc(row2*col2 * 2);

c = (int *) malloc(col1*row2 * 2);

clrscr();

printf("enter 1st matrix \n");

insert(a,row1,col1);

getch();

clrscr();

printf("enter 2st matrix \n");

insert(b,row2,col2);

getch();

clrscr();

printf("1st matrix is\n");

display(a,row1,col1);

printf("\n2nd matrix is\n");

display(b,row2,col2);

prod(a,b,c,row1,col2);

printf("\nafter multiplication \n\n\n");

display(c,row1,col2);

}

getch();

}

insert(int *q,int r,int c)

{int i,j;

for(i=0;i

{

for(j=0;j

{ printf("\nEnter [%d][%d] element-- ",i,j);

scanf("%d",(q+i*c+j));

}

}

}

display(int *q,int r,int c)

{int i,j;

for(i=0;i

{

printf("\n");

for(j=0;j

{

printf("%d\t",*(q+i*c+j));

}

}

}

prod(int *p,int *q,int *z,int r1,int c2)

{int i,j,k;

for(i=0;i

{

for(j=0;j

{

*(z+i*c2+j)=0;

for(k=0;k

{

*(z+i*c2+j)+=*(p+k*2+j)*(*(q+i*c2+k));

}

}

}

}

//Designed by Asjad Farrukh.......

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago
Matrix multiplication using pointers1. Why use it?Hi, so I've been looking for this answer myself and because it was not created at that time I've decided to do it.

Why use the multiplication by pointers other than normal multiplication by using 2 dimensional arrays (e.g. array[number rows][number columns] ) ? The point is when to use it. When calculating small matrices like matrix 3x3 or matrix 10x10 it is normally possible to use the second way (i think its easier) however, when asked to multiply a matrix of 1024 x 768 its best to use the pointer version, because when using the second way without the use of pointer, a computer has to do a lot more operations than with the pointer version, therefore it will take longer. Time is money and this is why pointers are used in this algorithm.

2. CompilingThe code i have made was tested under GNU C compiler using Code Blocks program. It should be compatible with bloodshed DevCPP program. The reason I am writing this is because MS Visual Studio might pop up a few errors. Maybe because of declarations that Visual Studio compiler won't accept. 3. The whole program//Includes for I/O and Standard Library

#include

#include

//Define functions that will be used in the program

void multiplyMatrices();

void getRowColumn(int *row, int *col, char *matrix);

int *createMatrix(int *row, int *col);

void getMatrixValues(int *source, int row, int col, char *name);

int *destroyMatrix(int *matrix);

void printMatrix(const int *source, int row, int col);

//Start of main

int main()

{

//Call multiplyMatrices function

multiplyMatrices();

//End main

return(0);

//End of main

}

//Start of multiplyMatrices function

void multiplyMatrices()

{

//Declare variables

int oneRow, oneCol, twoRow, twoCol, i, j, k;

//Call getRowColumn to ask user for the matrix size for both matrices

getRowColumn(&oneRow, &oneCol, "Matrix 1");

getRowColumn(&twoRow, &twoCol, "Matrix 2");

//The number of columns in oneMatrix must equal the number of rows in twoMatrix

if(oneCol != twoRow)

{

printf("The number of columns in the first matrix must equal the number of rows in the second matrix.\n");

exit(EXIT_FAILURE);

}

//Create the arrays in the heap memory

int *oneMatrix = createMatrix(&oneRow, &oneCol);

int *twoMatrix = createMatrix(&twoRow, &twoCol);

int *multipliedMatrix = createMatrix(&oneRow, &twoCol);

//Declare pointer variables to store the initial locations of my arrays for later use

int *initialOneMatrix, *initialTwoMatrix, *initialMultipliedMatrix;

initialOneMatrix = oneMatrix;

initialTwoMatrix = twoMatrix;

initialMultipliedMatrix = multipliedMatrix;

//Ask user for the values of each matrix

getMatrixValues(oneMatrix, oneRow, oneCol, "Matrix 1");

getMatrixValues(twoMatrix, twoRow, twoCol, "Matrix 2");

//Multiply the matrices (took me like 5 hours in the debugger to figure out how to arrange these loops lol)

//A for loop while i is less than oneRow(number of rows in first matrix)

for(i = 0; i < oneRow; i++)

{

//A for loop while j is less than twoCol

for(j = 0; j < twoCol; j++)

{

//Increment the address of oneMatrix by its number of columns multiplied by i

oneMatrix += (oneCol * i);

//Increment the address of twoMatrix by j

twoMatrix += j;

//A for loop while k is less than oneCol

for(k = 0; k < oneCol; k++)

{

//Multiply the current entries of the matracies and add the answer to the current entry for the multiplied matrix

*multipliedMatrix += *oneMatrix * *twoMatrix;

//Increment oneMatrix by 1

oneMatrix++;

//Increment twoMatrix by the number of its columns

twoMatrix += twoCol;

}

//Increment multipliedMatrix

multipliedMatrix++;

//"Rewind" oneMatrix to its original address

oneMatrix = initialOneMatrix;

//"Rewind" twoMatrix to its original address

twoMatrix = initialTwoMatrix;

}

}

//"Rewind" multipliedMatrix to its original address

multipliedMatrix = initialMultipliedMatrix;

//Newline to make it pretty

printf("\n");

//Call printMatrix function to print oneMatrix

printMatrix(oneMatrix, oneRow, oneCol);

//Newline, multiplication symbol, and two more newlines to make it pretty

printf("\n*\n\n");

//Call printMatrix function to print twoMatrix

printMatrix(twoMatrix, twoRow, twoCol);

//Newline, equal sign, and two more newlines to make it pretty

printf("\n=\n\n");

//Call printMatrix function to print multipliedMatrix

printMatrix(multipliedMatrix, oneRow, twoCol);

//Call destroyMatrix function to clear clear all of the matrices from memory

destroyMatrix(oneMatrix);

destroyMatrix(twoMatrix);

destroyMatrix(multipliedMatrix);

return;

//End of multiplyMatrices

}

//Start of getRowColumn function

void getRowColumn(int *row, int *col, char *matrix)

{

//Ask user how many rows for the specified matrix

printf("Please enter the number of rows for %s\n", matrix);

//Set variable 'row' to what the user input

scanf("%d", row);

//Ask user how many columns for the specified array

printf("Please enter the number of columns for %s\n", matrix);

//Set variable 'col' to what the user input

scanf("%d", col);

//End of getRowColumn

}

//Start of createMatrix function

int *createMatrix(int *row, int *col)

{

//Initialize the new matrix in the heap memory using calloc with row times col for an argument and using sizeof to let calloc know the size of int

int *newMatrix = calloc(*row * *col, sizeof(int));

//Check if newMatrix is nonexistent, if so the memory was unable to allocate and exit the program

if(!newMatrix)

{

printf("Unable to allocate memory.\n");

exit(EXIT_FAILURE);

}

//Return the newMatrix to where the function was called

return(newMatrix);

//End of createMatrix

}

//Start of getMatrixValues

void getMatrixValues(int *source, int row, int col, char *name)

{

//Declare counting variables for the for loops

int i, j;

//For loop to cycle through the rows

for(i = 0; i < row; i++)

{

//For loop to cycle through the columns

for(j = 0; j < col; j++)

{

//Ask user for a value for the specified entry

printf("Enter the value for %d %d for %s\n", i, j, name);

//Set source to what the user input, then increment source by 1

scanf("%d", source++);

}

}

//End of getMatrixValues

}

//Start of destroyMatrix function

int *destroyMatrix(int *matrix)

{

//Check if matrix exists

if(matrix)

{

//Function to free the matrix from memory

free(matrix);

}

return(NULL);

//End of destroyMatrix

}

//Start of printMatrix function

void printMatrix(const int *source, int row, int col)

{

//Declare counting variables for the for loops

int i, j;

//Cycle through the rows

for(i = 0; i < row; i++)

{

//Print beginning "side" to the matrix

printf("|");

//Cycle through the columns

for(j = 0; j < col; j++)

{

//Print the specified matrix entry

printf("%5d", *source++);

}

//Print an ending "side" to the matrix with a newline

printf("|\n");

}

//End of printMatrix

}

I realize answers.com does not format indents, so if you paste this in a reasonably good source code editor (xcode does this) it will auto indent and such.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

void accept(int[][10],int,int);

void mul(int[][10],int[][10],int[][10],int,int,int);

void disp(int[][10],int,int);

void main()

{int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2;

clrscr();

printf("\n enter rows and col of 1st matrix");

scanf("%d%d",&r1,&c1);

printf("\n enter rows and col of 2nd matrix");

scanf("%d%d",&r2,&c2);

if(c1==r2)

{printf("\n enter elements of 1st matrix");

accept(a,r1,c1);

disp(a,r1,c1);

printf("\n enter elements of 2nd matrix");

accept(b,r2,c2);

disp(b,r2,c2);

mul(a,b,c,r1,c2,r2);

disp(c,r1,c2);

}

void accept(int x[][10],i,j)

printf("\n input elements of first matrix");

for(i=0;i< r;i++) {

for(j=0;j< c;j++)

{

scanf("%d",&x[i][j]);

}

}

void mul(int a[][10],int b[][10],int c[][10],int r1,c2,r2)

for(i=0;i< m;i++) {

for(j=0;j< q;j++) {

c[i][j] = 0;

for(k=0;k< n;k++) {

c[i][j] = c[i][j] + a[i][k] * b[k][j];

}

}

}

void disp(int x[][10],r,c)

for(i=0;i<r1;i++)

{for(j=0;j<c2;j++)

{printf("%5d \t",c[i][j]);

}

printf("\n");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

write a program for multiplication of two 3x3 matrix using pointer

----------------------------------------------------------------------------------

#include<conio.h>

#include<stdio.h>

#include<alloc.h>

void main()

{

int (*a)[10],(*b)[10],(*c)[10],m,n,p,q,i,j,k;

clrscr();

printf("Rows and coloumns of 1 st matrix:");

scanf("%d%d",&m,&n);

printf("Rows and coloumns of 2 nd matrix:");

scanf("%d%d",&p,&q);

if(n!=p)

{

printf("Not possible");

}

else

{

a=(int*)malloc(m*n*sizeof(int));

b=(int*)malloc(p*q*sizeof(int));

c=(int*)malloc(n*p*sizeof(int));

printf("Enter first matrix:");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

scanf("%d",(*(a+i)+j));

printf("Enter second matrix:\n");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

scanf("%d",(*(b+i)+j));

printf("First matrix is:\n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<n;j++)

printf("%d\t",*(*(a+i)+j));

}

printf("\nSecond matrix is:\n");

for(i=0;i<p;i++)

{

printf("\n");

for(j=0;j<q;j++)

printf("%d\t",*(*(b+i)+j));

}

for(i=0;i<m;i++)

{

for(j=0;j<q;j++)

{

*(*(c+i)+j)=0;

for(k=0;k<n;k++)

*(*(c+i)+j)+=*(*(a+i)+k)*(*(*(b+k)+j));

}

}

printf("\nProduct=\n");

for(i=0;i<m;i++)

{

printf("\n");

for(j=0;j<q;j++)

printf("%d\t",*(*(c+i)+j));

}

}

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,sum;

clrscr();

printf("enter the two numbers");

scanf("%d%d",&a,&b);

sum=a+b;

printf("sum =%d");

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

main()

{

int a[3][3],b[3][3],c[3][3];

int i,j,k;

printf("enter the elements in A matrix:\n");

for(i=0;i<=2;i++)

{

for(j=0;j<=2;j++)

{

scanf("%d",&a[i][j]);

}

}

printf("enter b matrix:\n");

for(i=0;i<=2;i++)

{

for(j=0;j<=2;j++)

{

scanf("%d",&b[i][j]);

}

}

for(i=0;i<=2;i++)

{

printf("\n");

for(j=0;j<=2;j++)

{

c[i][j]=0;

for(k=0;k<=2;k++)

{

c[i][j] = c[i][j]+a[i][k] * b[k][j];

}

}

}

printf("multiplication matrix is:\n");

for(i=0;i<=2;i++)

{

for(j=0;j<=2;j++)

{

printf("%d\t",c[i][j]);

}

printf("\n");

}

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

int* add(int *i,int *j)

{

int *result;

*result=(*i) + (*j);

return result;

}

main()

{

int *i,*j,,*res;

*i=3;

*j=5;

res=add(i,j);

printf("%d",*res);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a functions using pointers to multiply two matrices and return it to the calling function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are function pointers?

Function Pointers are basically used when we want to select a function which is to be used dynamically at run time.AnswerFunction pointers are the only way for "Interept programming". In UNIX all the Interepts are called using function pointers. This is mainly used in system programming. Answerits nothing but a pointer to function. which is similar to ptr to a variable, if we are saying ptr to a variable then it will hold address of the variable like that fn. ptr will have the address of the function.. one of the major application of the function pointer is call back function.. i.e callback.AnswerPointers to functions/methods/subroutines aka 'Delegates' are frequently used in .NET programming especially in EventHandling, MemberInvoking A function pointer is used to pass a function as an argument to another function, or to store a function as a data item, for example a list of functions can be implemented as an array of pointers to functions. Function pointers are used to store interrupt handlers in tables.


Can you swap two matrix?

Provided both matrices are mutable, two matrices A and B can be swapped like any other two items: create temporary storage to store a copy of A, then assign B to A, and finally assign the temporary copy of the previous version of A to B. Note that in the C programming language, matrices cannot be assigned to each as such. One implementation of this algorithm might operate on the basis of references (pointers), and can thus swap two matrix references by swapping two pointers in the manner detailed above. Implementations wishing to actually transfer the data held in one matrix to another would use a library function such as memcpy() to transfer data.


How to multiply two int pointer variable in C language?

You do not multiply pointers. If you want to multiply the values that they point to you must dereference them, usually with a *


Function prototype in c?

Yes. Examples can be found in stdio.h


Is Use of pointers in function saves the memory space?

No. But avoiding unnecessary duplication of data does.

Related questions

What is meant by passing function to another function in c plus plus?

Just as pointers can point to variables, pointers can also point to functions. Thus you can pass function pointers to functions. In so doing, you can alter the behaviour of the function by having it call dynamically call arbitrary functions rather than just preset functions.


What is virtual function table?

A virtual function table is a table of pointers to functions.


Where are array of functions used?

Function Pointers are basically used when we want to select a function which is to be used dynamically at run time.


What are functions and pointers in c?

function-these are self contained block of statements to solve a particular task whereas pointers are defined as the variable which stores the address of another variable


Pointers to review in a napolcom examination?

government functions


Can functions be defined in C structures?

No, all functions must be defined outside of C structures. However, all functions in C have a type (the return type) and an identity (an address), so you can define function pointers as members of a structure to achieve the same end.


What are function pointers?

Function Pointers are basically used when we want to select a function which is to be used dynamically at run time.AnswerFunction pointers are the only way for "Interept programming". In UNIX all the Interepts are called using function pointers. This is mainly used in system programming. Answerits nothing but a pointer to function. which is similar to ptr to a variable, if we are saying ptr to a variable then it will hold address of the variable like that fn. ptr will have the address of the function.. one of the major application of the function pointer is call back function.. i.e callback.AnswerPointers to functions/methods/subroutines aka 'Delegates' are frequently used in .NET programming especially in EventHandling, MemberInvoking A function pointer is used to pass a function as an argument to another function, or to store a function as a data item, for example a list of functions can be implemented as an array of pointers to functions. Function pointers are used to store interrupt handlers in tables.


Can you swap two matrix?

Provided both matrices are mutable, two matrices A and B can be swapped like any other two items: create temporary storage to store a copy of A, then assign B to A, and finally assign the temporary copy of the previous version of A to B. Note that in the C programming language, matrices cannot be assigned to each as such. One implementation of this algorithm might operate on the basis of references (pointers), and can thus swap two matrix references by swapping two pointers in the manner detailed above. Implementations wishing to actually transfer the data held in one matrix to another would use a library function such as memcpy() to transfer data.


How to multiply two int pointer variable in C language?

You do not multiply pointers. If you want to multiply the values that they point to you must dereference them, usually with a *


What is the difference between pointer and function?

Pointers and functions are two entirely different things, similar to comparing cats to oranges. A pointer allows a program to dynamically call functions and/or dynamically load or store data. Pointers are commonly called "dangerous" in most programming circles, which is why newer languages do not explicitly allow programmers to access them (although they may use them internally). They are dangerous because a pointer used incorrectly can corrupt data or crash a system (or at least an application). While this is true, they are not dangerous by themselves, it is the novice programmer's misunderstanding of pointers that is dangerous. Functions are pieces of code that can be used over and over again in various parts of a program without duplicating code. For example, a function that calculates simple interest might be used in several parts of a banking application. This way, if a bug was found in how simple interest was calculated, it could be fixed just once instead of scanning the entire code base to make sure it was fixed everywhere. Functions reduce source code size, executable size, and memory usage. Pointers can dynamically call functions, while functions are pieces of code that can execute. Functions have no concept of pointers, and do not care from where they are called.


Function prototype in c?

Yes. Examples can be found in stdio.h


When do you use pointers in C?

There are many uses of pointer in C. Pointers are efficient and elegant to use. All string variables are pointers, and calling a function using a pointer allows the function to change the value globally. These are what you can do with pointers in C. 1) Returning multiple values by passing address of variables. eg. foo(&amp;a,&amp;b,&amp;c); 2) When you want to modify the value passed to function. eg. scanf() function. int n; scanf("%d",&amp;n); /* pass address of variable n so that scanf can change its value*/ 3) When you need to pass large data structure to function, it is more efficient to pass pointer to structure than passing the entire structure. If you don't want to modify the structure members, you can use 'const' keyword which the ANSI-complaint C compiler will either warn or give error if you modify. eg strlen(const char *str) in string library should not modify the content of str. 4) Implementing 'goto' data structures are easy with pointers. eg. linked-list,binary trees, hash table. 5) You can allocate dynamic memory using pointers. 6) Pointers to function are used for call back function and jump table. eg qsort() and bsearch() functions require the caller to provide pointer to function to do the comparison.