C Programiranje množenja matrice - Manipulacija i algoritam matrice

Sadržaj:

Anonim

Uvod u množenje matriksa u C programiranju

U članku C Programiranje umnožavanja matrice matrica je mreža koja se koristi za pohranu podataka u strukturiranom formatu. Često se koristi sa tablicom, gdje su podaci prikazani u vodoravnim redovima i okomitim stupcima. Matrice se često koriste u programskim jezicima i koriste se za predstavljanje podataka u grafičkoj strukturi. U programiranju ako korisnik želi množiti, zbrajati, oduzimati i dijeliti dvije matrice, tada se prvo treba navesti redoslijed matrice. Jednom kada je redoslijed matrice deklariran za prvu i drugu matricu, tada elemente (ulaz) za matrice mora unijeti korisnik. Ako redoslijed matrice nije proporcionalan jedno drugom, tada će se prikazati poruka o pogrešci koju programer ugrađuje u izjavu o stanju. Ako matrica sadrži samo jedan redak, onda se naziva redni vektor, a ako sadrži samo jedan stupac, onda se zove i vektor stupaca.

Matrica koja sadrži isti broj redaka i stupaca onda se naziva kvadratnom matricom. Matrica se koristi za spremanje grupe povezanih podataka. Neki se programski jezici koriste za podršku matrica kao podatkovnog tipa koji nudi veću fleksibilnost od statičkog niza. Umjesto pohranjivanja vrijednosti u matricu, ona se može pohraniti kao pojedinačna varijabla, program može pristupiti i učinkovitije izvoditi operacije na podacima. U C programiranju matrice množenja se provode pomoću nizova, funkcija, pokazivača. Stoga ćemo raspravljati o algoritmu za množenje Matrice, zajedno s dijagramom tijeka, koji se može koristiti za pisanje programskog koda za umnožavanje matrice 3 × 3 na jeziku visoke razine. Ovo detaljno objašnjenje pomoći će vam da analizirate radni mehanizam umnožavanja matrice i pomoći će vam da shvatite kako napisati kod.

Algoritam C programiranja množenja matrice

Korak 1: Pokrenite program.

Korak 2: Unesite redak i stupac prve (a) matrice.

Korak 3: Unesite redak i stupac druge (b) matrice.

Korak 4: Unesite elemente prve (a) matrice.

Korak 5: Unesite elemente druge (b) matrice.

Korak 6: Ispišite elemente prve (a) matrice u obliku matrice.

Korak 7: Ispišite elemente druge (b) matrice u obliku matrice.

Korak 8: Postavite petlju na red.

Korak 9: Postavite unutarnju petlju do stupca.

Korak 10: Postavite još jednu unutarnju petlju do stupca.

Korak 11: Pomnožite prvu (a) i drugu (b) matricu i pohranite element u treću matricu (c)

12. korak: Ispišite završnu matricu.

Korak 13: Zaustavite program.

Dijagram točenja množenja matrice

Primjer množenja matrice programiranja C

C program izvodi množenje matrice, pogledajmo nekoliko primjera.

Kodirati:

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

Izlaz:

Rad umnožavanja matrice programiranja C

  • U gornjem programu inicijalizirali smo varijable i nizove unutar glavne metode u cjelobrojnoj (int) vrsti podataka.
  • Nakon dijela inicijalizacije, dobivamo redoslijed matrice od korisnika za prvu matricu, tada korisnik istovremeno mora proglasiti redoslijed druge matrice.
  • Nakon što se deklarira redoslijed matrica, tada će se izvršiti dio uvjeta, program će se nastaviti izvoditi samo ako nalog zadovoljava uvjet ili će program prekinuti ili zaustaviti na samom tom dijelu.
  • Jednom kada je uvjet zadovoljen, korisnik mora tijekom vremena izvođenja unijeti elemente matrice kao ulaze.
  • Prema korisničkoj ulaznoj matrici izračunava se množenje.
  • Gornji program matrice jednostavan je i može izračunati ažuriranje 25 × 25, tako da u polju možemo jednostavno urediti potrebne brojeve.

Prednosti C programiranja množenja matrice

  • C programski jezik podržava matricu kao podatkovni tip i nudi veću fleksibilnost. Također uz to troši manje memorije tijekom obrade.
  • Spremanjem vrijednosti u matricu, a ne kao pojedinačne varijable, C program može pristupiti i izvršiti operacije na podacima učinkovitije.
  • Lakše je izdvojiti podatke o rotaciji objekata, a također je lako manipulirati u programu C.

Zaključak

Umnožavanje matrice opetovano se koristi u programima za predstavljanje grafičke strukture podataka, koja se koristi za pohranu više vektora, a također se koristi u mnogim aplikacijama poput rješavanja linearnih jednadžbi i više. Mnogo je istraživanja provedeno na množenju matrica koristeći minimalni broj operacija.

Preporučeni članak

Ovo je vodič za množenje C matrice programiranja. Ovdje ćemo raspravljati o radu na matrici, algoritmu, dijagramu tijeka i primjerima zajedno s različitim prednostima u c programiranju. Možete i proći naše druge predložene članke da biste saznali više -

  1. Uvod u nizove u C programiranju
  2. Obrasci programiranja C - (Primjeri)
  3. C Programiranje pitanja za intervju | Top 13
  4. Što je programski jezik R?
  5. Nizovi u strukturi podataka