Ú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!
- HP ZBook Firefly 16 G19 Profi Tervező Vágó Laptop -50% i7-2355U 32/512 FHD+ Nvidia T550 4GB
- Nintendo Switch 2
- Native Instruments Kontrol S49 MK3
- ELADÓ i5 14600KF + RTX5070 + 32Gb DDR4 + 600W Platinum + Wifi + SOK GARI + AKÁR HÁZHOZSZÁLLÍTÁS IS!
- Nintendo Switch Lite CFW 20.4 + 256GB microSD, Dual Boot + Atmosphere 1.9.4 + 3 hó garancia!
- Samsung Galaxy S22+ 5G 128GB, Kártyafüggetlen, 1 Év Garanciával
- 1MII B03PRO+ Receiver, Transmitter LDAC
- HP EliteBook 830 G8 i5-1135G7 16GB 256GB 1 év garancia
- Konzol felvásárlás!! Xbox Series S, Xbox Series X
- BESZÁMÍTÁS! ASUS H510M i5 10600KF 32GB DDR4 512GB SSD RTX 3060Ti 8GB Rampage SHIVA Enermax 700W
Állásajánlatok
Cég: CAMERA-PRO Hungary Kft.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest