445,898 Members | 2,022 Online Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

# Unpredictable program behavior ..

 P: n/a 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 #include #include #include #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; t1)) { 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
3 Replies

 P: n/a On Sat, 06 Dec 2003 17:27:17 +0200, Andrew wrote: Hi all , i am supposed to create a program that performs a specificoperation to a 2-D matrix whose elements are positive (or zero ).This operation is repeated T times on the matrix and only the finalmatix is needed . The operation to be perormed isV(t,x,y)=1/2*(sqrt(V(t-1,x-1,y))+sqrt(V(t-1,x-1,y))) . The problem iisthat i have (or i think i have ) created a correct program thatperforms this operation but something happens that i cannot understand. The final matrix is the same regardless the value of T . That means I ran your code with T defined as 5 and determined from the debugger that tempMatrix had three non-zero values. Ran it again with T defined as 100 and tempMatrix had nine non-zero values. Something else must be the problem. Keep reading. that the final matrix is the same for T=10 and T=1000 !!! The code ihave managed to write so far is the following :/********** VERSION FOR ONE PROCESSOR *****/#include #include #include #include #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??? There is no need since you initialize it immediately during execution. Some like to do it as a matter of style. double **tempMatrix; // Initialization ??? double tempValue; int t,x,y; for(t=1; t1)) {tempValue=(double)1+(double)sqrt(tempMatrix[y-1][x]); sqrt returns a double so that cast is completely superfluous. } 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); You really are in love with casts, aren't you? Why? What do you think they accomplish? } } /* 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); This copies currentMatrix to tempMatrix. /* printf("Now printing the old matrix..... \n");*/ /* PrintMatrix(tempMatrix,Y,X); */ DestroyMatrix(currentMatrix,Y); This completely frees currentMatrix in preparation for reallocating it during the next iteration through the loop. It would have been simpler to merely reinitialize currentMatrix. } } /* end of the triple loop ... */ for(int tempi=0; tempi> Nov 13 '05 #2

 P: n/a Barry Schwarz wrote: On Sat, 06 Dec 2003 17:27:17 +0200, Andrew wrote:#include Additional comment: is not a standard header. Use for all your mallocing needs. -Kevin -- My email address is valid, but changes periodically. To contact me please use the address from a recent posting. Nov 13 '05 #3

 P: n/a On 6 Dec 2003 22:20:29 GMT, Barry Schwarz wrote: Mr. Schwarz thank you very-very much for reading/commenting/executing my code . I can now remove all the unnecessary operations and make it run faster . It doesn't matter if the loop(s) are executed T or T-1 times because i am creating a parallel version of the program ( for more than one processors , actually 16 ) and I must ensure that the final matrix is the same with that calculated from the one processor program . We even ignore the boundary conditions (for X=0 and Y=0 ) we need them only to compute the V(t,1,y) and V(t,x,1) values... Anyway a friend of mine is getting the same results with this program so I think I don't make something wrong with the algorithm ... I will optimize my code to get faster execution times following your remakrs .. Thanks again !! Nov 13 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 