473,774 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unpredictable program behavior ..

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(d ouble **,int);
void InitializeMatri x(double **,int,int);
void CopyMatrix(doub le **,double **,int,int);

/* The following function is for DEBUG purposes only */

void PrintMatrix(dou ble **,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=C reateMatrix(X,Y );
InitializeMatri x(currentMatrix ,X,Y);
/* printf("Matrix Created .... \n"); */
if(t==1)
{
tempMatrix=Crea teMatrix(X,Y);
InitializeMatri x(tempMatrix,X, Y);
}
for(x=1; x<X; x++)
{
for(y=1; y<Y; y++)
{
if((x==1) && (y==1))
{
tempValue=(doub le)1;
}
if((x==1) && (y>1))
{

tempValue=(doub le)1+(double)sq rt(tempMatrix[y-1][x]);
}
if((y==1) && (x>1))
{

tempValue=(doub le)sqrt(tempMat rix[y][x-1]);
}
if((x>1) && (y>1))
{

tempValue=(doub le)sqrt(tempMat rix[y][x-1])+(double)sqrt( tempMatrix[y-1][x]);
}

currentMatrix[y][x]=((double)tempV alue)/((double)2);
}
}
/* printf("After changing the matrix ..... \n"); */
/* PrintMatrix(cur rentMatrix,Y,X) ; */
if(t!= T-1)
{
/* printf("You reached so far .... !!\n"); */
CopyMatrix(temp Matrix,currentM atrix,Y,X);
/* printf("Now printing the old matrix..... \n");
*/
/* PrintMatrix(tem pMatrix,Y,X); */
DestroyMatrix(c urrentMatrix,Y) ;
}
} /* end of the triple loop ... */
for(int tempi=0; tempi<Y ; tempi++)
{
for(int tempj=0; tempj<X; tempj++)
{
printf(" %f ",currentMa trix[tempi][tempj]);
}
printf("\n");
}

DestroyMatrix(c urrentMatrix,Y) ;
DestroyMatrix(t empMatrix,Y);
return 0;
}
double ** CreateMatrix(in t X_dim,int Y_dim)
{
double **retdouble=mal loc(Y_dim*(size of(double *)));
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");
exit(EXIT_FAILU RE);
}
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_FAILU RE);
}
}
return retdouble;
}

void DestroyMatrix(d ouble ** matrix,int Y_dim)
{
for(int i=0; i<Y_dim; i++)
{
free(matrix[i]);
}
free(matrix);
}

void InitializeMatri x(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[i][j]=0;
}
}
}

void CopyMatrix(doub le **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[i][j]=source[i][j];
}
}
}
void PrintMatrix(dou ble **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 )
Nov 13 '05 #1
3 1646
On Sat, 06 Dec 2003 17:27:17 +0200, Andrew
<af************ ********@yahoo. com> wrote:
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
I ran your code with T defined as 5 and determined from the debugger
that tempMatrix[1] 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 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(d ouble **,int);
void InitializeMatri x(double **,int,int);
void CopyMatrix(doub le **,double **,int,int);

/* The following function is for DEBUG purposes only */

void PrintMatrix(dou ble **,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; t<T; t++)
You are aware that this will only execute the loop T-1 times, not T?
{
currentMatrix=C reateMatrix(X,Y );
It is a little confusing that you pass the parameters as X and Y when
you reference the matrix as y and x.
InitializeMatri x(currentMatrix ,X,Y);
/* printf("Matrix Created .... \n"); */
if(t==1)
{
tempMatrix=Crea teMatrix(X,Y);
InitializeMatri x(tempMatrix,X, Y);
}
for(x=1; x<X; x++)
{
for(y=1; y<Y; y++)
{
if((x==1) && (y==1))
You seem to be ignoring row and column 0 here.
{
tempValue=(doub le)1;
The cast is irrelevant. 1 is an int and would be converted to double
automatically. If you want an indication in the source line that you
are not dealing with integers, 1.0 would be preferable.
}
if((x==1) && (y>1))
{

tempValue=(dou ble)1+(double)s qrt(tempMatrix[y-1][x]);
sqrt returns a double so that cast is completely superfluous.
}
if((y==1) && (x>1))
{

tempValue=(dou ble)sqrt(tempMa trix[y][x-1]);
}
if((x>1) && (y>1))
{

tempValue=(dou ble)sqrt(tempMa trix[y][x-1])+(double)sqrt( tempMatrix[y-1][x]);
}

currentMatri x[y][x]=((double)tempV alue)/((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(cur rentMatrix,Y,X) ; */
if(t!= T-1)
{
/* printf("You reached so far .... !!\n"); */
CopyMatrix(temp Matrix,currentM atrix,Y,X);
This copies currentMatrix to tempMatrix.
/* printf("Now printing the old matrix..... \n");
*/
/* PrintMatrix(tem pMatrix,Y,X); */
DestroyMatrix(c urrentMatrix,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<Y ; tempi++)
{
for(int tempj=0; tempj<X; tempj++)
{
printf(" %f ",currentMa trix[tempi][tempj]);
Can you guess which parameter above no longer exists. You are
invoking undefined behavior.
}
printf("\n");
}

DestroyMatrix(c urrentMatrix,Y) ;
You cannot free memory that has already been freed. More undefined
behavior.
DestroyMatrix(t empMatrix,Y);
return 0;
}
double ** CreateMatrix(in t X_dim,int Y_dim)
{
double **retdouble=mal loc(Y_dim*(size of(double *)));
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");
exit(EXIT_FAILU RE);
}
for (int k=0; k<Y_dim; k++)
Unless you happen to have a C99 compiler, declaring variables inside
the for statement is a non-standard extension.
{
retdouble[k]=malloc(X_dim*( sizeof(double)) );
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
A different error message would be nice to distinguish between the two
different allocations.
exit(EXIT_FAILU RE);
}
}
return retdouble;
}

void DestroyMatrix(d ouble ** matrix,int Y_dim)
{
for(int i=0; i<Y_dim; i++)
{
free(matrix[i]);
}
free(matrix);
}

void InitializeMatri x(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[i][j]=0;
}
}
}

void CopyMatrix(doub le **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[i][j]=source[i][j];
}
}
}
void PrintMatrix(dou ble **dest,int Y_dim,int X_dim)
{
for(int tempi=0; tempi<Y_dim ; tempi++)
{
for(unsigned short tempj=0; tempj<X_dim; tempj++)
I give up. Why is this index different from all other indices?
{
printf(" %f ",dest[tempi][tempj]);
Since your values are doubles, wouldn't you like to print them as
doubles instead of floats?
}
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 )


Why? It was understandable.
<<Remove the del for email>>
Nov 13 '05 #2
Barry Schwarz wrote:
On Sat, 06 Dec 2003 17:27:17 +0200, Andrew
<af************ ********@yahoo. com> wrote:

#include <malloc.h>


Additional comment: <malloc.h> is not a standard header. Use <stdlib.h>
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
On 6 Dec 2003 22:20:29 GMT, Barry Schwarz <sc******@deloz .net> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
3011
by: strutsng | last post by:
I want if "a C program is a standard C++ program, but not vice versa" is a correct statement? In a C++ program, we can use standard C libraries. However, we cannot use C++ libraries inside C program. Please advise. thanks!!
54
4121
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
38
2565
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my lead(I have just started working) he said we should not use dynamic allocation in real time systems, the code will not run in predictable time period.Can anybody tell what does he mean?Why the execution time becomes unpredictable? Thanks
40
2652
by: findmadhav | last post by:
I need a program in C (something like a TSR) which will automatically press the function key F6, say about every 5 seconds. Can anyone provide me with an exe of such a program? Thanks in advance.
8
1786
by: sami.jan | last post by:
Hi I am using the xlC_r compiler on AIX 5.2 - this program crashes with a segmentation fault (core dumped) - I compile with a command: xlC_r filename.C - no switches or anything ------------------------------------------------------------------------------------------------------------------------ #include <iostream.h> #include <string.h> char* myfunc()
8
4214
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char* message,int seed){
73
2833
by: Rajeet Dalawal | last post by:
Good day group. I was asked in an interview to explain the behavior of this program. void main() { char *s = "abc"; int *i = (int *) s; printf("%x", *i); }
13
1457
by: bintom | last post by:
Is there any reason why the following C++ code behaves as it does ? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3
8
2091
by: bintom | last post by:
Why doe the following C++ code behaves as it does? int i; i=1; cout << (++i)++; Output: 2 i=1; cout << ++(++i); Output: 3 i=1; cout << (i++)++; Output: Error. LValue required i=1; cout << ++(i++); Output: Error. LValue required
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.