Question:
mulitping,adition and subtraction of matrix in c programing using function or pointer?
Lominat
2009-06-02 05:37:04 UTC
using C programming the program accept two matrice and display the sum,differnt and product using array function
Three answers:
Uma
2009-06-02 05:42:03 UTC
include "stdio.h"

#include "stdlib.h"



int main()

{

int i,j,k,l; k=l;// Matrix dimensions

int o,p; // iterations of cycles

printf("\t Matrix multiplication by pointers\n");



do {

do{

fflush (stdin);

printf("Enter rows of matrix A: ");

} while (scanf("%d",&i) == 0);

do{

fflush (stdin);

printf("Enter columns of matrix A: ");

} while (scanf("%d",&k) == 0);

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

do{

fflush (stdin);

printf("Enter rows of matrix B: ");

} while (scanf("%d",&l) == 0);

do{

fflush (stdin);

printf("Enter columns of matrix B: ");

} while (scanf("%d",&j) == 0);

if (k != l) printf ("Wrong -- cannot multiply enter same columns A as rows B");

} while (k != l);

int matrixA[i*k];

int matrixB[k*j];

int matrixC[i*j];

// Pointer inititiation

int *ukA = &matrixA[0];

int *ukB = &matrixB[0];

int *ukC = &matrixC[0];

for (o=0; o

{

do

{

fflush (stdin);

printf("Enter element of matrix A of coordinates a%d: ",o);

}

while (scanf("%d",&(*ukA)) == 0);

ukA++;

}

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

for (o=0; o

{

do

{

fflush (stdin);

printf("Enter element of matrix B of coordinates b%d: ",o);

}

while (scanf("%d",&(*ukB)) == 0);

ukB++;

}

printf("\n Begining output of matrix A[%d, %d]\n",i,k);

ukA=&matrixA[0]; // reset the pointer adress

for (o=0; o

{

printf(" %d ", *ukA);

if ((o+1)%k==0) { printf ("\n");} // this will create a column

ukA++;

}

printf("\n Zahajuji vypis matice B[%d, %d]\n",k,j);

ukB=&matrixB[0]; // reset the pointer adress

for (o=0; o

{

printf(" %d ", *ukB);

if ((o+1)%j==0) { printf ("\n");} // this will create a column



ukB++;

}

//Cycle for zeroing the matrix C



for (o=0; o

{



*ukC = 0;

ukC++;

}

ukA=&matrixA[0]; // reset the pointer adress

ukB=&matrixB[0]; // reset the pointer adress

ukC=&matrixC[0]; // reset the pointer adress

int *zacA = &matrixA[0]; // this points to the begining of row in A

int *zacB = &matrixB[0]; // this points to the begining of column in B

// This is actual algorithm for multiplication

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

printf ("Multpng. matrices A[%d, %d] x B[%d, %d] = C[%d, %d] \n",i,k,k,j,i,j);

for (o=0;o

for (p=0;p

*ukC = (*ukA) * (*ukB) + *ukC;

ukA= ukA+1;

ukB = ukB + j;

}

if ((o+1)%j==0)

{

zacA=ukA;

zacB=&matrixB[0];

ukB=zacB;

}

ukB=zacB+((o+1)%j);

ukA = zacA;

ukC++;

}

ukC=&matrixC[0]; // this resets the pointer again

for (o=0; o

{

printf(" %d ", *ukC);

if ((o+1)%j==0) { printf ("\n");} // this will create a column



ukC++;

}

printf("\n\n");

system("pause");

return 0;

}
te
2016-10-04 16:58:06 UTC
you could, whether it is going to look like this: int posit(int* x, int n){ bear in strategies: guidance that could desire to int are additionally guidance that could desire to arrays of int (and additionally for all different varieties). once you upload the [], it is like asserting you decide on a pointer to a pointer to an integer array, yet it is not what you decide on here. additionally, I recommend protecting the asterisk by using the form somewhat of the variable, when you consider that 'pointer to int' is a sort distinctive from int (precise, the compiler shouldn't enable it the way you place it, whether it does, so it is as much as you to maintain your code from being confusing).
anonymous
2009-06-02 11:14:38 UTC
include

#include



void accept();

void display();

void add();

void sub();

void mul();



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

int i,j,k;

int r1,r2,c1,c2; //globaly declares so that no need to pass parameters



void main()

{

int choice;

clrscr();

accept();



do

{

printf("\n\n\nMAIN MENU:");

printf("\n\n 1:ADDITION");

printf("\n 2:SUBSTRACTION");

printf("\n 3:MULTIPLICATION");

printf("\n 4:NEW MATRICES");

printf("\n 5:EXIT");

printf("\nEnter Your Choice:");

scanf("%d",&choice);



switch(choice)

{

case 1:

add(); break;



case 2:

sub(); break;



case 3:

mul(); break;



case 4:

newm(); break;



case 5:

exit(0);



default:

printf("\nWRONG CHOICE...!!!");

}; // switch part is optional you can directly call fuctions



}while(choice!=5);



getch();

}



void accept()

{

printf("\nEnter No of rows & columns of matrix 'A':");

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

printf("\nEnter No of rows & columns of matrix 'B':");

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

printf("\nEnter elements of matrix A:");

for(i=0;i
{

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

}

printf("\nEnter elements of matrix B:");

for(i=0;i
{

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

}

}



void display()

{

printf("\nFINAL MATRIX:\n\n");

for(i=0;i
{

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

printf("\n");

}

}



void add()

{

if(r1==r2&&c1==c2)

{

for(i=0;i
{

for(j=0;j
c[i][j]=a[i][j]+b[i][j];

}

display();

}

else

printf("\nADDITION IS NOT POSSIBLE...!!!");

}



void sub()

{

if(r1==r2&&c1==c2)

{

for(i=0;i
{

for(j=0;j
c[i][j]=a[i][j]-b[i][j];

}

display();

}

else

printf("\nSUBTRACTION IS NOT POSSIBLE...!!!");

}



void mul()

{

if(c1==r2)

{

for(i=0;i
{

for(j=0;j
{

c[i][j]=0;

for(k=0;k
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

}

}

display();

}

else

printf("\nMULTIPLICATION IS NOT POSSIBLE...!!!");

}









Hope this will help you.......

All the best......!!!!!!!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...