Home > Back-end >  The dynamic two-dimensional array values
The dynamic two-dimensional array values

Time:11-12

#include
#include
Void show (int v [], int n)//compression matrix into the original matrix output
{
int i,j;
for(i=0; i{
for(j=0; J{
If (i>=j)//comparison of row and column subscript
Printf (" % 5 d, v [I * (I + 1)/2 + j]);
The else
Printf (" % 5 d, v [j * (j + 1)/2 +] I);
}
printf("\n");
}
}
Void multiply (int va [], int vb [], int [] c [n], int n)
{
Int I, j, k, s, op1, op2;
for(i=0; i{
for(j=0; J{
S=0;
for(k=0; K{
If (i>=k)//comparison of row and column subscript, line constant, change is equal to the column subscript k
{
Op1=va [I * (I + 1)/2 + k];
}
The else
{
Op1=va [k * (k + 1)/2 +] I;
}
If (k>=j)//comparison of row and column subscript, column, line is equivalent to change k subscript
{
Op2=vb [k * (k + 1)/2 + j];
}
The else
{
[j * op2=vb (j + 1)/2 + k];
}
S +=op1 * op2;
}
C [I] [j]=s;
}
}
for(i=0; i{
for(j=0; J{
printf("%5d",c[i][j]);
}
printf("\n");
}
return;
}
Void Destroy (int * * a, int n)//2 d array dynamic destruction
{
int i;
for(i=0; i{
Free (a [I]);
}
}
Int main ()
{
Int n, k, I, j;
Int * a * b;
Int * * c=(int) * * malloc (n * sizeof (int *));//dynamic allocation two-dimensional array
for(i=0; i{
[I] c=(int *) malloc (sizeof (int) * n);
}
Printf (" please enter the order number n: \ n ");
The scanf (" % d ", & amp; N);
K=n * (n + 1)/2;//k as a one-dimensional array to store the n order symmetric matrix the number of elements in the one-dimensional array requirements
A=(int *) malloc (sizeof (int) * k);//a dynamically allocated a one-dimensional array
B=(int *) malloc (sizeof (int) * k);//dynamically allocated a one-dimensional array b
Printf (" generating matrix A, please input % d compression matrix of Numbers: \ n ", n * (n + 1)/2);
for(i=0; i{
The scanf (" % d ", & amp; A [I]);//input compression matrix element
}
Printf (" matrix A is: \ n ");
Show (a, n);//the compressed matrix into the original matrix output


Printf (" generator matrix B please input % d compression matrix of Numbers: \ n ", n * (n + 1)/2);
for(i=0; i{
The scanf (" % d ", & amp; B [I]);////input compression matrix element
}
Printf (" matrix B is: \ n ");
Show (b, n);//the compressed matrix into the original matrix output
Printf (" A * B=: \ n ");
Multiply (a, b, c, n);//returns the address of the two-dimensional arrays
Destroy (c, n);//destruction of two-dimensional array
return 0;
}
Why would an error?

CodePudding user response:

Void multiply (int va [], int vb [], int [] c [n], int n)//int [] c here [n] n is not a constant, dynamic array can't define, want to use a pointer int * * c (or n are defined as macros),
The dynamic array problem how do I think lz asked more than once, how to make the same mistake or old,
  • Related