My program doesn't gets stopped
May 22, 2013 at 10:15am UTC
Hi All,
I have displayed a code. In this code at the last part of program I need to stop the program once when the condition is satisfied. But My program keeps on running not stopping once the condition is satisfied. I have used break statement. But not working.
Needs to be fixed. Your help is needful to me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
/*Grid Point declarations*/
int x,y;
double left,right,top,bottom;
double phi[100][100],uphi[100][100];
cout<<"Enter no. of grid points" <<endl<<"x:" ;
cin>>x;
cout<<"y:" ;
cin>>y;
/*Read & Define Boundary Conditions*/
cout<<endl<<endl<<"Enter value for left:" ;
cin>>left;
cout<<endl<<"Enter value for right:" ;
cin>>right;
cout<<endl<<"Enter value for top:" ;
cin>>top;
cout<<endl<<"Enter value for right:" ;
cin>>bottom;
int nit;
cout<<"Enter required iterations: " ;
cin>>nit;
cout<<'\n' ;
for (int i=0;i<=x-1;i++)
{
phi[0][i]=left;
}
for (int i=0;i<=x-1;i++)
{
phi[x-1][i]=right;
}
for (int i=0;i<=x-1;i++)
{
phi[i][y-1]=top;
}
for (int i=0;i<=x-1;i++)
{
phi[i][0]=bottom;
}
cout<<'\n' ;
cout<<"First Iteration values" <<endl<<'\n' ;
/*First Guess it=1*/
for (int i=1;i<=x-2;i++)
{
for (int j=1;j<=y-2;j++)
{
phi[i][j]=(left+right+top+bottom)/4;
cout<<"1" <<'\t' <<phi[i][j]<<endl;
}
}
cout<<'\n' ;
/* for (int i=0;i<=x-1;i++)
{
for (int j=0;j<=y-1;j++)
{
cout<<i<<'\t'<<j<<'\t'<<phi[i][j]<<endl;
}
}*/
/*Iteration from it=2*/
for (int it=2;it<=nit;it++)
{
for (int i=1;i<=x-2;i++)
{
for (int j=1;j<=y-2;j++)
{
uphi[i][j]=phi[i][j];
phi[i][j]=(phi[i+1][j]+phi[i-1][j]+phi[i][j+1]+phi[i][j-1])/4;
cout<<it<<'\t' <<phi[i][j]<<'\t' <<uphi[i][j]<<endl;
if ((uphi[i][j]-phi[i][j])<=0.001&&(uphi[i][j]-phi[i][j])>0)
{
cout<<"Solution Converged" <<'/n' ;
break ;
}
}
}
cout<<'\n' ;
}
getch();
}
May 22, 2013 at 10:19am UTC
break ;
will only break out of the innermost loop.
May 22, 2013 at 10:23am UTC
What should I do to stop my whole program?
May 22, 2013 at 10:48am UTC
May 22, 2013 at 11:00am UTC
Could You please help me with this. Because I'm a Beginner in C++.
I could understand that I should use return but I'm stuck with how?
May 22, 2013 at 11:12am UTC
May 22, 2013 at 11:14am UTC
Thanks For your Help. I used goto statement to bring my process out of the loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
/*Grid Point declarations*/
int x,y;
double left,right,top,bottom;
double phi[100][100],uphi[100][100];
cout<<"Enter no. of grid points" <<endl<<"x:" ;
cin>>x;
cout<<"y:" ;
cin>>y;
/*Read & Define Boundary Conditions*/
cout<<endl<<endl<<"Enter value for left:" ;
cin>>left;
cout<<endl<<"Enter value for right:" ;
cin>>right;
cout<<endl<<"Enter value for top:" ;
cin>>top;
cout<<endl<<"Enter value for right:" ;
cin>>bottom;
int nit;
cout<<"Enter required iterations: " ;
cin>>nit;
cout<<'\n' ;
for (int i=0;i<=x-1;i++)
{
phi[0][i]=left;
}
for (int i=0;i<=x-1;i++)
{
phi[x-1][i]=right;
}
for (int i=0;i<=x-1;i++)
{
phi[i][y-1]=top;
}
for (int i=0;i<=x-1;i++)
{
phi[i][0]=bottom;
}
cout<<'\n' ;
cout<<"First Iteration values" <<endl<<'\n' ;
/*First Guess it=1*/
for (int i=1;i<=x-2;i++)
{
for (int j=1;j<=y-2;j++)
{
phi[i][j]=(left+right+top+bottom)/4;
cout<<"1" <<'\t' <<phi[i][j]<<endl;
}
}
cout<<'\n' ;
/* for (int i=0;i<=x-1;i++)
{
for (int j=0;j<=y-1;j++)
{
cout<<i<<'\t'<<j<<'\t'<<phi[i][j]<<endl;
}
}*/
/*Iteration from it=2*/
for (int it=2;it<=nit;it++)
{
for (int i=1;i<=x-2;i++)
{
for (int j=1;j<=y-2;j++)
{
uphi[i][j]=phi[i][j];
phi[i][j]=(phi[i+1][j]+phi[i-1][j]+phi[i][j+1]+phi[i][j-1])/4;
cout<<it<<'\t' <<phi[i][j]<<'\t' /*<<uphi[i][j]*/ <<endl;
if ((uphi[i][j]-phi[i][j])<=0.001&&(uphi[i][j]-phi[i][j])>0)
{
goto last;
}
}
}
cout<<'\n' ;
}
last:
cout<<endl<<"Solution is Converged" ;
getch();
}
May 22, 2013 at 11:15am UTC
Now it will always print "Solution is Converged". ;)
May 22, 2013 at 11:33am UTC
You were pointed to return and you somehow found goto ?
Topic archived. No new replies allowed.