C Ohjelmointimatriisin kertolasku - Matriisin manipulointi ja algoritmi

Sisällysluettelo:

Anonim

Johdanto matriisikertomiseen C-ohjelmoinnissa

Artiklassa C Matriisin kertolaskun ohjelmointi matriisi on ruudukko, jota käytetään tietojen tallentamiseen jäsennellyssä muodossa. Sitä käytetään usein taulukon kanssa, jossa tiedot on esitetty vaakasuorissa riveissä ja pystysarakkeissa. Matriiseja käytetään usein ohjelmointikielellä ja niitä käytetään edustamaan tietoja graafisessa rakenteessa. Jos käyttäjä haluaa ohjelmoida, kertoa, lisätä, vähentää ja jakaa kaksi matriisia, matriisin järjestys tulisi ilmoittaa ensin. Kun matriisin järjestys on ilmoitettu ensimmäiselle ja toiselle matriisille, käyttäjän on syötettävä matriisien elementit (syöttö). Jos matriisin järjestys ei ole suhteessa toisiinsa, näyttöön tulee virhesanoma, jonka ohjelmoija on istuttanut ehtolausekkeeseen. Jos matriisi sisältää vain yhden rivin, sitä kutsutaan rivivektoriksi, ja jos se sisältää vain yhden sarakkeen, niin sitä kutsutaan sarakevektoriksi.

Matriisia, joka sisältää saman määrän rivejä ja sarakkeita, sitä kutsutaan neliömatriisiksi. Matriisia käytetään tallentamaan liittyvien tietojen ryhmä. Joitakin ohjelmointikieliä käytetään matriisien tukemiseen tietotyyppinä, joka tarjoaa enemmän joustavuutta kuin staattinen taulukko. Sen sijaan, että arvoja tallennetaan matriisiin, se voidaan tallentaa yksittäisenä muuttujana, ohjelma voi käyttää tietoja tehokkaammin ja suorittaa niitä koskevia tietoja. C-ohjelmoinnissa matriisikertolaskut tehdään käyttämällä taulukkoja, funktioita, osoittimia. Siksi aiomme keskustella Matrix-kertolaskun algoritmista yhdessä vuokaavion kanssa, jota voidaan käyttää ohjelmointikoodin kirjoittamiseen 3x3-matriisikertolaskuille korkean tason kielellä. Tämä yksityiskohtainen selitys auttaa sinua analysoimaan matriisin kertolaskun toimintamekanismia ja ymmärtää koodin kirjoittamista.

C-ohjelmointimatriisin kertolaskun algoritmi

Vaihe 1: Käynnistä ohjelma.

Vaihe 2: Kirjoita ensimmäisen (a) matriisin rivi ja sarake.

Vaihe 3: Syötä toisen (b) matriisin rivi ja sarake.

Vaihe 4: Syötä ensimmäisen (a) matriisin elementit.

Vaihe 5: Syötä toisen (b) matriisin elementit.

Vaihe 6: Tulosta ensimmäisen (a) matriisin elementit matriisimuodossa.

Vaihe 7: Tulosta toisen (b) matriisin elementit matriisimuodossa.

Vaihe 8: Aseta silmukka riville.

Vaihe 9: Aseta sisempi silmukka sarakkeeseen.

Vaihe 10: Aseta toinen sisäsilmukka sarakkeeseen.

Vaihe 11: Kerro ensimmäinen (a) ja toinen (b) matriisi ja tallenna elementti kolmanteen matriisiin (c)

Vaihe 12: Tulosta lopullinen matriisi.

Vaihe 13: Pysäytä ohjelma.

Matriisikertomuksen vuokaavio

Esimerkki C-ohjelmointimatriisin kertolaskusta

C-ohjelma suorittaa matriisin kertolaskun, katsokaamme muutamia esimerkkejä.

Koodi:

#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));
)
)

lähtö:

C-ohjelmointimatriisin kertolaskutoimitus

  • Yllä olevassa ohjelmassa olemme alustaneet muuttujat ja taulukot päämenetelmän sisällä kokonaislukuina (int).
  • Alustusosan jälkeen saamme matriisin tilauksen käyttäjältä ensimmäiselle matriisille, sitten käyttäjän on samanaikaisesti ilmoitettava toisen matriisin järjestys.
  • Kun matriisien järjestys on ilmoitettu, ehto-osa suoritetaan, ohjelma jatkuu vain, jos tilaus täyttää ehdon tai muuten ohjelma lopetetaan tai lopetetaan siinä osassa.
  • Kun ehto täyttyy, käyttäjän on syötettävä matriisielementit syötteinä ajon aikana.
  • Käyttäjän syöttömatriisin perusteella lasketaan kertolasku.
  • Yllä oleva matriisiohjelma on yksinkertainen ja voi laskea päivityksen 25 × 25, joten voimme yksinkertaisesti muokata taulukossa tarvittavia lukuja.

C-ohjelmointimatriisin kertolaskun edut

  • C-ohjelmointikieli tukee matriisia tietotyyppinä ja tarjoaa enemmän joustavuutta. Ja se vie myös vähemmän muistia käsittelyn aikana.
  • Tallentamalla arvoja matriisiin kuin yksittäisinä muuttujina, C-ohjelma voi käyttää ja suorittaa datan toimintoja tehokkaammin.
  • Kohteen kiertoa koskevia tietoja on helpompi saada, ja C-ohjelmassa on myös helppo käsitellä.

johtopäätös

Matriisikertolaskua käytetään toistuvasti ohjelmissa graafisen tietorakenteen esittämiseen, jota käytetään useiden vektorien tallentamiseen, ja sitä käytetään myös monissa sovelluksissa, kuten lineaaristen yhtälöiden ratkaisemisessa ja muissa. Paljon tutkimusta on tehty matriisien kertomiseksi käyttämällä minimimäärä operaatioita.

Suositeltava artikkeli

Tämä on opas C-ohjelmointimatriisin kertolaskuun. Tässä keskustellaan matriisin manipuloinnin, algoritmin, vuokaavion ja esimerkkien työskentelystä sekä erilaisista eduista c-ohjelmoinnissa. Voit myös käydä läpi muiden ehdotettujen artikkeleidemme saadaksesi lisätietoja -

  1. Johdanto ryhmiin C-ohjelmoinnissa
  2. Kuviot C-ohjelmoinnissa - (esimerkkejä)
  3. C Ohjelmointihaastattelu | Top 13
  4. Mikä on R-ohjelmointikieli?
  5. Tietorakenteen taulukot