Új hozzászólás Aktív témák
-
Lacces
őstag
Sziasztok!
Lenne egy kis problémám, hogy megfelelőlen olvassam be a mátrixokat és vektorokat, illetve írassam ki.
Pelda input:
4
0 1 −2 4
1 −3 0 2
4 2 −28 1
−1 0 1 1
2
3 0 −21 1
1111
4
−1 −2 0 1
2401
1314
3 8 2 −2
2
−2 7 9 11
2.34 1.245 −3.4 1.234
3
2 3 1.2
2.4 1.6 2.44
−4.6 −10.1 2.34
1
−1.7 5.96 27.21
0Pelda output:
1.00000000 1.00000000 1.00000000 1.00000000
-0.92063492 -0.46031746 -0.19047619 0.26984127
szingularis
1.10000000 −2.50000000 3.00000000szingularis, a második mátrixokra vonatkozik.
Az a baj, hogy néha a bemenet 33-as mátrix, így igen csak gondban vagyok, hogyan lehetne megoldani a main-ben, már próbáltam többszörösen is iterálni a main()-t de nem jött össze.
Valakinek van rá megoldás? Tényleg csak ennyin múlik az egész, ezzel küzdök már 1 napja.#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void inMatrix(int n, double **matrix)
{
int j, i;
for (i = 0; i < n; i++)
{
for (j= 0; j < n; j++)
{
scanf("%lf", &matrix[i][j]);
}
}
}
void inVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
scanf("%lf", &vektor[k]);
}
}
void outVector(double *vektor, int n)
{
int k;
for (k = 0; k < n; k++)
{
printf("%.8lf ", vektor[k]);
}
}
void lup(int n, double **A, double *b)
{
int *Permutation = (int*)malloc(sizeof(int)*n);
int i,j;
double *max = (double*) malloc (sizeof(double)*n);
int m=0, p=0;
int tmp=0, tmp2=0;
int t=0, isSingular=0;
double largestElement=0.0;
double *helpVector = (double*) malloc (sizeof(double)*n);
double *helpVectorA = (double*) malloc (sizeof(double)*n);
double *helpVectorB = (double*) malloc (sizeof(double)*n);
for(i=0; i<n; i++)
Permutation[i]=i;
for(m=0; m<n-1; m++)
{
for(i=m; i<n; i++)
{
max[i]=fabs(A[i][m]);
}
for(i=m; i<n; i++)
{
if(max[i]>largestElement)
{
largestElement=max[i];
p=i;
}
}
for(i=0; i<n; i++)
{
helpVectorA[i]=A[m][i];
helpVectorB[i]=A[p][i];
}
for(i=0; i<n; i++)
{
A[m][i]=helpVectorB[i];
A[p][i]=helpVectorA[i];
}
tmp=Permutation[m];
tmp2=Permutation[p];
Permutation[m]=tmp2;
Permutation[p]=tmp;
if(fabs(A[m][m])>0.00000000000000001)
{
for(i=m+1; i<n; i++)
{
A[i][m]=A[i][m]/A[m][m];
for(j=m+1; j<n; j++)
{
A[i][j]=A[i][j]-A[i][m]*A[m][j];
}
}
}
if(fabs(A[m][m])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
break;
}
for(i=0; i<n; i++) max[i]=-1;
largestElement=0.0;
p=m+1;
}
if(isSingular==0)
{
if(fabs(A[n-1][n-1])<0.00000000001)
{
printf("szingularis\n");
isSingular=1;
}
}
if(isSingular==0)
{
for(i=0; i<n; i++)
{
t=Permutation[i];
helpVector[i]=b[t];
}
for(i=0; i<n; i++)
{
b[i]=helpVector[i];
}
for(i=1; i<n; i++)
{
for(j=0; j<i; j++)
{
b[i]=b[i]-A[i][j]*b[j];
}
}
for(i=(n-1); i>=0; i--)
{
for(j=(i+1); j<n; j++)
{
b[i]=b[i]-A[i][j]*b[j];
}
b[i]=b[i]/A[i][i];
}
}
}
int main()
{
int k, v,n;
int loop;
double **A;
double *b;
// read dimension of matrix and value
scanf("%d", &n);
//matrix
A = (double **) calloc(n, sizeof ( double*));
//vector
b = (double *) calloc(n, sizeof ( double));
loop=1;
while(n!=0)
{
for (k = 0; k < n; k++)
A[k] = (double *) calloc(n, sizeof ( double));
// read values of matrix
inMatrix(n, A);
// read values of vector
scanf("%d", &v);
for(k=0;k<v;k++)
{
inVector(b, n);
lup(n,A,b);
}
for(k=0; k<v;k++)
{
outVector(b, n);
printf("\n");
}
//free(matrix[k]);
loop++;
}
return 0;
}
Új hozzászólás Aktív témák
● olvasd el a téma összefoglalót!
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Plazma TV topic
- Milyen alaplapot vegyek?
- OnePlus 15 - van plusz energia
- Steam, GOG, Epic Store, Humble Store, Xbox PC Game Pass, Origin Access, uPlay+, Apple Arcade felhasználók barátságos izgulós topikja
- Milyen okostelefont vegyek?
- AMD Ryzen 9 / 7 / 5 9***(X) "Zen 5" (AM5)
- Vezetékes FEJhallgatók
- Autós topik
- Milyen TV-t vegyek?
- Vigneau interaktív lokálblogja
- További aktív témák...
- ELADÓ Gamer Konfig
- EIZO FlexScan EV2495 Made in Japan! 1920x1200 IPS USB-c állítható magasság, PIVOT 2024-04 gyártás!
- 10 DB Lenovo ThinkPad Laptop X13 G2 G3, T14 G3 G5, T14s G4 G6, P15 G1 G2
- RAM memória Crucial Pro OC Gaming 16GB DDR5 6400MHz CL32 Black - bontatlan, új
- Dell Latitude 7420, 14" FHD IPS kijelző, i7-1185G7 CPU, 16GB DDR4, 256GB SSD, W11, Számla, 1 év ga
- ÁRGARANCIA!Épített KomPhone i9 14900KF 32/64GB DDR5 RAM RTX 5070 12GB GAMER PC termékbeszámítással
- BESZÁMÍTÁS! ASUS ROG STRIX B460 i7 10700 16GB DDR4 1TB SSD RTX 5060 8GB NZXT S340 fehér CM 600W
- Lenovo 14 Thinkbook 2in1 Yoga FHD IPS Touch i7-1165G7 16GB 512GB Intel IrisXE AktívToll W11 Garancia
- ÁRGARANCIA!Épített KomPhone i5 10400F 16/32/64GB RAM RX 7600 8GB GAMER PC termékbeszámítással
- BESZÁMÍTÁS! Sony PlayStation VR2 virtuális valóság szemüveg garanciával hibátlan működéssel
Állásajánlatok
Cég: Laptopszaki Kft.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest

