A
Andrew
Hi all , i am supposed to create a program that performs a specific
operation to a 2-D matrix whose elements are positive (or zero ).
This operation is repeated T times on the matrix and only the final
matix is needed . The operation to be perormed is
V(t,x,y)=1/2*(sqrt(V(t-1,x-1,y))+sqrt(V(t-1,x-1,y))) . The problem iis
that i have (or i think i have ) created a correct program that
performs this operation but something happens that i cannot understand
.. The final matrix is the same regardless the value of T . That means
that the final matrix is the same for T=10 and T=1000 !!! The code i
have managed to write so far is the following :
/********** VERSION FOR ONE PROCESSOR *****/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#define T 2000
#define X 10
#define Y 10
/* TODO ... to pass these values from the command line ... */
double **CreateMatrix(int,int);
void DestroyMatrix(double **,int);
void InitializeMatrix(double **,int,int);
void CopyMatrix(double **,double **,int,int);
/* The following function is for DEBUG purposes only */
void PrintMatrix(double **,int,int);
int main(void)
{
double **currentMatrix; // Initialization ??? cast to NULL
???
double **tempMatrix; // Initialization ???
double tempValue;
int t,x,y;
for(t=1; t<T; t++)
{
currentMatrix=CreateMatrix(X,Y);
InitializeMatrix(currentMatrix,X,Y);
/* printf("Matrix Created .... \n"); */
if(t==1)
{
tempMatrix=CreateMatrix(X,Y);
InitializeMatrix(tempMatrix,X,Y);
}
for(x=1; x<X; x++)
{
for(y=1; y<Y; y++)
{
if((x==1) && (y==1))
{
tempValue=(double)1;
}
if((x==1) && (y>1))
{
tempValue=(double)1+(double)sqrt(tempMatrix[y-1][x]);
}
if((y==1) && (x>1))
{
tempValue=(double)sqrt(tempMatrix[y][x-1]);
}
if((x>1) && (y>1))
{
tempValue=(double)sqrt(tempMatrix[y][x-1])+(double)sqrt(tempMatrix[y-1][x]);
}
currentMatrix[y][x]=((double)tempValue)/((double)2);
}
}
/* printf("After changing the matrix ..... \n"); */
/* PrintMatrix(currentMatrix,Y,X); */
if(t!= T-1)
{
/* printf("You reached so far .... !!\n"); */
CopyMatrix(tempMatrix,currentMatrix,Y,X);
/* printf("Now printing the old matrix..... \n");
*/
/* PrintMatrix(tempMatrix,Y,X); */
DestroyMatrix(currentMatrix,Y);
}
} /* end of the triple loop ... */
for(int tempi=0; tempi<Y ; tempi++)
{
for(int tempj=0; tempj<X; tempj++)
{
printf(" %f ",currentMatrix[tempi][tempj]);
}
printf("\n");
}
DestroyMatrix(currentMatrix,Y);
DestroyMatrix(tempMatrix,Y);
return 0;
}
double ** CreateMatrix(int X_dim,int Y_dim)
{
double **retdouble=malloc(Y_dim*(sizeof(double *)));
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");
exit(EXIT_FAILURE);
}
for (int k=0; k<Y_dim; k++)
{
retdouble[k]=malloc(X_dim*(sizeof(double)));
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
exit(EXIT_FAILURE);
}
}
return retdouble;
}
void DestroyMatrix(double ** matrix,int Y_dim)
{
for(int i=0; i<Y_dim; i++)
{
free(matrix);
}
free(matrix);
}
void InitializeMatrix(double **matrix,int X_dim,int Y_dim)
{
for(int i=0; i<Y_dim; i++)
{
for(int j=0; j<X_dim; j++)
{
matrix[j]=0;
}
}
}
void CopyMatrix(double **target,double **source,int Y_dim,int X_dim)
{
for (int i=0; i<Y_dim; i++)
{
for(int j=0; j<X_dim; j++)
{
target[j]=source[j];
}
}
}
void PrintMatrix(double **dest,int Y_dim,int X_dim)
{
for(int tempi=0; tempi<Y_dim ; tempi++)
{
for(unsigned short tempj=0; tempj<X_dim; tempj++)
{
printf(" %f ",dest[tempi][tempj]);
}
printf("\n");
}
}
The program simply creates two matrices dynamically , currentMartix
for the current operation and tempMatrix for the last operation ...
After all the elements of the current matrix have changed according to
the mathematical formula i wrote before they are going to be copied
to the tempMatrix and then the currentMatrix is going to be erased ...
This is performed T times ..
(Sorry for my english )
operation to a 2-D matrix whose elements are positive (or zero ).
This operation is repeated T times on the matrix and only the final
matix is needed . The operation to be perormed is
V(t,x,y)=1/2*(sqrt(V(t-1,x-1,y))+sqrt(V(t-1,x-1,y))) . The problem iis
that i have (or i think i have ) created a correct program that
performs this operation but something happens that i cannot understand
.. The final matrix is the same regardless the value of T . That means
that the final matrix is the same for T=10 and T=1000 !!! The code i
have managed to write so far is the following :
/********** VERSION FOR ONE PROCESSOR *****/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#define T 2000
#define X 10
#define Y 10
/* TODO ... to pass these values from the command line ... */
double **CreateMatrix(int,int);
void DestroyMatrix(double **,int);
void InitializeMatrix(double **,int,int);
void CopyMatrix(double **,double **,int,int);
/* The following function is for DEBUG purposes only */
void PrintMatrix(double **,int,int);
int main(void)
{
double **currentMatrix; // Initialization ??? cast to NULL
???
double **tempMatrix; // Initialization ???
double tempValue;
int t,x,y;
for(t=1; t<T; t++)
{
currentMatrix=CreateMatrix(X,Y);
InitializeMatrix(currentMatrix,X,Y);
/* printf("Matrix Created .... \n"); */
if(t==1)
{
tempMatrix=CreateMatrix(X,Y);
InitializeMatrix(tempMatrix,X,Y);
}
for(x=1; x<X; x++)
{
for(y=1; y<Y; y++)
{
if((x==1) && (y==1))
{
tempValue=(double)1;
}
if((x==1) && (y>1))
{
tempValue=(double)1+(double)sqrt(tempMatrix[y-1][x]);
}
if((y==1) && (x>1))
{
tempValue=(double)sqrt(tempMatrix[y][x-1]);
}
if((x>1) && (y>1))
{
tempValue=(double)sqrt(tempMatrix[y][x-1])+(double)sqrt(tempMatrix[y-1][x]);
}
currentMatrix[y][x]=((double)tempValue)/((double)2);
}
}
/* printf("After changing the matrix ..... \n"); */
/* PrintMatrix(currentMatrix,Y,X); */
if(t!= T-1)
{
/* printf("You reached so far .... !!\n"); */
CopyMatrix(tempMatrix,currentMatrix,Y,X);
/* printf("Now printing the old matrix..... \n");
*/
/* PrintMatrix(tempMatrix,Y,X); */
DestroyMatrix(currentMatrix,Y);
}
} /* end of the triple loop ... */
for(int tempi=0; tempi<Y ; tempi++)
{
for(int tempj=0; tempj<X; tempj++)
{
printf(" %f ",currentMatrix[tempi][tempj]);
}
printf("\n");
}
DestroyMatrix(currentMatrix,Y);
DestroyMatrix(tempMatrix,Y);
return 0;
}
double ** CreateMatrix(int X_dim,int Y_dim)
{
double **retdouble=malloc(Y_dim*(sizeof(double *)));
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");
exit(EXIT_FAILURE);
}
for (int k=0; k<Y_dim; k++)
{
retdouble[k]=malloc(X_dim*(sizeof(double)));
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
exit(EXIT_FAILURE);
}
}
return retdouble;
}
void DestroyMatrix(double ** matrix,int Y_dim)
{
for(int i=0; i<Y_dim; i++)
{
free(matrix);
}
free(matrix);
}
void InitializeMatrix(double **matrix,int X_dim,int Y_dim)
{
for(int i=0; i<Y_dim; i++)
{
for(int j=0; j<X_dim; j++)
{
matrix[j]=0;
}
}
}
void CopyMatrix(double **target,double **source,int Y_dim,int X_dim)
{
for (int i=0; i<Y_dim; i++)
{
for(int j=0; j<X_dim; j++)
{
target[j]=source[j];
}
}
}
void PrintMatrix(double **dest,int Y_dim,int X_dim)
{
for(int tempi=0; tempi<Y_dim ; tempi++)
{
for(unsigned short tempj=0; tempj<X_dim; tempj++)
{
printf(" %f ",dest[tempi][tempj]);
}
printf("\n");
}
}
The program simply creates two matrices dynamically , currentMartix
for the current operation and tempMatrix for the last operation ...
After all the elements of the current matrix have changed according to
the mathematical formula i wrote before they are going to be copied
to the tempMatrix and then the currentMatrix is going to be erased ...
This is performed T times ..
(Sorry for my english )