I have the following code and g1 is the same as gp till nn which are two dimensional integral. By incidentally when I repeat the integral in with different notation I got different value of the first one and if I put (cout) after finish all the integrals the the values will be change.

#include<iostream>

#include <stdlib.h>

#include <stdio.h>

#include <cmath>

using namespace std;

#define PI 3.1415926536

double A,B,A2,B2,g,c,g2,d1,d2,q,q2;

double f1(double x, double y){

g=(cos(x)+2*(cos(x/2.0))*cos(c*y))/3.0;

A=3*(1+0.5*g);

B=(9/2.0)*g;

d1=A*A-B*B;

if (d1==0)

return 0;

else {

return -0.5*(g*B)/sqrt(d1);

}

}

////////////////////////////////////////////////

double s1(double a1, double b1,double a2, double b2, double n){double hx=(b1-a1)/n, hy=(b2-a2)/n;

double y, x;

double sumx, sumy;

int facy, facx;

facy=2;

for(int j=0; j<=n; j++){

y=a2+j*hy;

sumx=0.0;

facx=2;for(int i=0; i<=n; i++)

{x=a1+i*hx;

if(i==0 || i==n)

sumx=sumx+f1(x,y);

else {

facx=((facx==2) ? 4 : 2);

sumx=sumx+facx*f1(x,y);

}

}sumx=sumx*hx/3.0;

if(j==0 || j==n)

sumy=sumy+f1(x,y);

else{

facy=((facy==2) ? 4 : 2);

sumy=sumy+facy*sumx;

}

}

sumy=(sumy*hy/3.0);

return sumy;

}

////////////////////////////////////////////////////

int main(void){

double m,g1,gp,n,nn,gg;

m=2/sqrt(3.0);

c=sqrt(3)/2.0;

q=-PI;

q2=q;

g1=s1(-PI,PI,-m*PI,m*PI,1000)*(sqrt(3)/(8*PI*PI));

gp=s1(-PI,PI,-m*PI,m*PI,1000)*(sqrt(3)/(8*PI*PI));

n=s1(-PI,PI,-m*PI,m*PI,1000)*(sqrt(3)/(8*PI*PI));

nn=s1(-PI,PI,-m*PI,m*PI,1000)*(sqrt(3)/(8*PI*PI));

cout<<"g1="<<nn<<endl;

cout<<"g1="<<g1<<endl;

cout<<"g1="<<gp<<endl;

cout<<"g1="<<n<<endl;

system("pause");

return 0;

}

I'd love to help you, but I have no idea what your code is trying to do. And there is not a single comment in it to give me even the tiniest clue. This would make a fine entry for the Obfuscated C competition, but to a mere mortal like me I'm afraid it is gibberish.

Perhaps you could explain what you are trying to do, what output you expect and what output you get.

I really I would thank you ijack for your reply. I will try now to explain to you as much as possible and forgive me about the language. However, this code is use to do mathematical calculation which so called numerical integration and the results of the integral or the final value is expressed by nn, gg, n and g1. These variable are the same if you have a look their expression there is no any difference between them. So, when I run the code the results of nn, gg, n and g1 should be the same I mean I should get the same value for all of them. But I notice if I change the order of cout<<g1,cout<<nn etc the value on the screen after run the code will be different. This is the main problem with code not with specific step in my calculation. The problem is again with cout<< only. Hopefully this explanation is satisfied.

Without trying to understand the program, I do notice that in the function "s1" you declare the variable "sumy" but never initialize it (although you did initialize "sumx"). Each time you call the function sumy will start off with different (random) values, hence the function will return different results each time you call it. Try initializing "sumy" to 0.0 and see if that helps.

Dear ijack Thank you very much for your help. the uninitialized of sumy=0.0 is big mistake in the code and I really I did not notice that. I am really happy with your help and I wish you all the best in your life.

with all the respect

physicist

Ask Your Question

Weekly Poll

Do you think Microsoft's new Surface Go will be a hit?

Discuss in The Lounge

Poll History