C Programiranje množenje matrike - Matrična manipulacija in algoritem

Kazalo:

Anonim

Uvod v množenje matrice v C programiranju

V članku C Programiranje množenja matrike je matrica mreža, ki se uporablja za shranjevanje podatkov v strukturirani obliki. Pogosto se uporablja s tabelo, kjer so podatki predstavljeni v vodoravnih vrsticah in navpičnih stolpcih. Matrice se pogosto uporabljajo v programskih jezikih in se uporabljajo za predstavljanje podatkov v grafični strukturi. Če želi uporabnik pomnožiti, seštevati, odštevati in deliti dve matriki, je treba najprej razglasiti vrstni red matrice. Ko je vrstni red matrike razglašen za prvo in drugo matriko, potem mora uporabnik vnesti elemente (vhod) matric. Če vrstni red matrice ni sorazmeren drug drugemu, se prikaže sporočilo o napaki, ki ga programer vstavi v stavek. Če matrica vsebuje samo eno vrstico, se imenuje vektor vrstic, in če vsebuje samo en stolpec, se imenuje vektor stolpcev.

Matrica, ki vsebuje enako število vrstic in stolpcev, potem se imenuje kvadratna matrica. Matrica se uporablja za shranjevanje skupine povezanih podatkov. Nekateri programski jeziki se uporabljajo za podporo matric kot podatkovnega tipa, ki ponuja večjo prožnost kot statični niz. Namesto da bi vrednosti shranili v matrico, jo lahko shranimo kot posamezno spremenljivko, program lahko dostopa do podatkov in izvede učinkovitejše operacije s podatki. V matričnem programiranju C množenje poteka z uporabo nizov, funkcij, kazalcev. Zato bomo razpravljali o algoritmu za množenje Matrix skupaj s diagramom poteka, ki ga lahko uporabimo za pisanje programske kode za pomnoževanje matrice 3 × 3 v jeziku na visoki ravni. Ta podrobna razlaga vam bo pomagala analizirati delovni mehanizem množenja matric in pomagala razumeti, kako napisati kodo.

Algoritem pomnoževanja matrike C programiranja

1. korak: Zaženite program.

2. korak: Vnesite vrstico in stolpec prve (a) matrike.

3. korak: Vnesite vrstico in stolpec druge matrice (b).

4. korak: Vnesite elemente prve (a) matrike.

5. korak: Vnesite elemente druge (b) matrike.

6. korak: Elemente prve (a) matrice natisnite v obliki matrice.

7. korak: Elemente druge (b) matrice natisnite v obliki matrice.

8. korak: nastavite zanko do vrstice.

9. korak: Nastavite notranjo zanko do stolpca.

Korak 10: Nastavite drugo notranjo zanko do stolpca.

Korak 11: Pomnožite prvo (a) in drugo (b) matriko in element shranite v tretjo matrico (c)

12. korak: Natisnite končno matrico.

Korak 13: Ustavite program.

Diagram toka množenja matrice

Primer množenja matrike programiranja na C

C program izvaja matrično množenje, poglejmo nekaj primerov.

Koda:

#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)
#include
void main()
(
int a(25)(25), b(25)(25), c(25)(25), i, j, k, r, s;
int m, n;
printf("Enter the first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the second matrix\n");
scanf("%d%d", &r, &s);
if(m!=r)
printf("\n The matrix cannot multiplied");
else
(
printf("\n Enter the elements of first matrix ");
for(i= 0;i (
for(j=0;j scanf("\t%d", &a(i)(j));
)
printf("\n Enetr the elements of second matrix ");
for(i=0;i (
for(j=0;j scanf("\t%d", &b(i)(j));
)
printf("\n The element of first matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", a(i)(j));
)
printf("\n The element of second matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", b(i)(j));
)
for(i=0;i (
printf("\n");
for(j=0;j (
c(i)(j)=0;
for(k=0;k c(i)(j)=c(i)(j)+a(i)(k)*b(k)(j);
)
)
)
printf("\n Multiplication of two matrix is");
for(i=0;i (
printf("\n");
for(j=0;j printf("\t%d", c(i)(j));
)
)

Izhod:

Delo pomnoževanja matrike C programiranja

  • V zgornjem programu smo inicializirali spremenljivke in matrike znotraj glavne metode v celotnem (int) podatkovnem tipu.
  • Po inicializacijskem delu dobimo od uporabnika vrstni red matrike za prvo matrico, nato mora uporabnik hkrati razglasiti vrstni red druge matrice.
  • Ko je razvrščen vrstni red matric, potem se bo pogojni del izvršil, se bo program še naprej izvajal le, če naročilo izpolnjuje pogoj ali pa bo program sam prenehal ali ustavil na tem delu.
  • Ko je pogoj izpolnjen, mora uporabnik v času izvajanja vnesti matrične elemente kot vhode.
  • Glede na uporabniško vhodno matrico se izračuna množenje.
  • Zgornji matrični program je preprost in lahko izračuna posodobitev 25 × 25, tako da lahko v nizu preprosto uredimo na zahtevane številke.

Prednosti C programiranja množenja matric

  • Programski jezik C podpira matrico kot podatkovni tip in ponuja večjo prilagodljivost. Prav tako porabi manj pomnilnika med obdelavo.
  • S programom C lahko s shranjevanjem vrednosti v matriki in ne kot posameznih spremenljivkah učinkoviteje dostopa do operacij s podatki.
  • Informacije o vrtenju predmetov je lažje črpati in tudi v programu C jih je enostavno manipulirati.

Zaključek

Matrično množenje se večkrat uporablja v programih, da predstavlja grafično strukturo podatkov, ki se uporablja za shranjevanje več vektorjev in se uporablja tudi v mnogih aplikacijah, kot je reševanje linearnih enačb in še več. Veliko raziskav je bilo izvedenih o množenju matric z uporabo minimalnega števila operacij.

Priporočeni članek

To je vodnik za pomnoževanje matrike C programiranja. Tukaj razpravljamo o manipulaciji z matrico, algoritmu, diagramu pretoka in primeri, skupaj z različnimi prednostmi pri programiranju c. Če želite izvedeti več, lahko preberete tudi druge naše predlagane članke -

  1. Uvod v matrike v C programiranju
  2. Vzorci v programiranju na C - (primeri)
  3. C Programiranje Intervju Vprašanja | Top 13
  4. Kaj je programski jezik R?
  5. Nizi v strukturi podatkov