首页 > 解决方案 > 带有边界的拉普拉斯方程的 C++ 输出错误

问题描述

我已经以 C 格式成功运行了此代码,但是当我将其更改为 C++ 时,我无法获得相同的输出 这是在 C++ 中

#include <stdio.h>
#include<iostream>
#include <math.h>
#include <cstdlib>
#define S 4

using namespace std;
float u[S+1][S+1];
void entcol(int i,float u[S+1][S+1]) //to enter the boundary value
{
 int j;
cout<<"\n Enter the value of u["<<i<<"],j],j=0,"<<S<<endl;
for(j=0;j<=S;j++)
    {cin>>u[i][j];}
}
void entrow(int j, float u[S+1][S+1]) //to enter the boundary value
{
int i; 
cout<<"\nEnter the value of u[i,"<<j<<"],i=1,"<<S-1<<endl;
    for(i=1;i<=(S-1);i++)
    {cin>>u[i][j];}
}
void oput(float u[S+1][S+1]) //print array
{
int i,j;
for(i=i;i<=S;i++)
{
    for(j=1;j<=S;j++)
    cout<<i<<j<<u[i][j]<<endl;

}
 }
 int main()
{
float u[S+1][S+1];
float mer, ar, e, t;
int i,j,itr, maxitr;
for(i=0;i<=S;i++)
 for(j=0;j<=S;j++)
u[i][j]=0;
cout<<"\n  Enter the Boundary Condition\n";
entcol(0,u); 
entcol(S,u);
entrow(0,u); 
entrow(S,u);

for(i=0;i<=S;i++)
   {
   for(j=0;j<=S;j++)
   {

       cout<<"["<<i<<"]["<<j<<"]="<<u[i][j]<<endl;
   }} 

cout<<" Enter the allowed error and maximum number of iteration : ";
cin>>ar>>maxitr;
for(itr=1;itr<=maxitr;itr++)
{
   mer=0;
   for(j=1;j<=(S-1);j++)
   {
   for(i=1;i<=(S-1);i++)
     {              
      
       t=(u[i][j+1]+u[i][j-1]+u[i-1][j]+u[i+1][j])*0.25; 
       e=fabs(u[i][j]-t); 
       mer=e;
       u[i][j]=t; 
          }
   cout<<" Iteration Number"<<itr<<endl;
   oput(u);
   if(mer<=ar)

   {
       cout<<" After"<<itr<<" iteration \n The solution :"<<endl;
        oput(u);
      }
  }
 cout<<" Sorry! The number of iteration is not sufficient"<<endl;
}
return 0;
}

我可以知道我哪里出错了吗?这是C中的代码

#include<stdio.h>
#include<math.h>
#define S 4
typedef float newvar[S+1][S+1];
    void entcol(int i,newvar u)
   {
        int j;
        printf("\n Enter the value of u[%d,j],j=0,%d\n",i,S);
        for(j=0;j<=S;j++)
        scanf("%f",&u[i][j]);
   }
  void entrow(int j, newvar u)
{
int i;
printf("Enter the value of u[i,%d],""i=1,%d\n",j,S-1);
for(i=1;i<=S-1;i++)
scanf("%f",&u[i][j]);
}
void oput(newvar u)
{
    int i,j;
for(i=i;i<=S;i++)
{
    for(j=1;j<=S;j++)
    printf("[%d],[%d],%f\n",i,j, u[i][j]);
    printf("\n");

    }
}
main()
{
    newvar u;
    float mer, ar, e, t;
    int i,j,itr, maxitr;
    for(i=0;i<=S;i++)
    for(j=0;j<=S;j++)
    u[i][j]=0;
    printf("\n  Enter the Boundary Condition\n");
    entcol(0,u); 
    entcol(S,u);
    entrow(0,u); 
    entrow(S,u);

  printf(" Enter the allowed error and maximum number of iteration : ");
  scanf("%f%f",&ar,&maxitr);
   for(itr=1;itr<=maxitr;itr++)
 {
     mer=0;
     for(j=1;j<=S-1;j++)
     {
     for(i=1;i<=S-1;i++)
     {          
       t=(u[i][j+1]+u[i][j-1]+u[i-1][j]+u[i+1][j])/4;
       e=fabs(u[i][j]-t);
       if(e>mer)
       mer=e;
       u[i][j]=t;
       //printf("[%d][%d]%f\n",i,j,t);
   }
   printf(" Iteration Number %d\n",itr);
   oput(u);
   if(mer<=ar)
   {
       printf(" After %d iteration \n The solution : \n",itr);
        oput(u);
     }
 }
 printf(" Sorry! The number of iteration is not sufficient");
   }
return 0;
}

当我将代码从 C 更改为 C++ 时,我哪里出错了?该代码用于计算带边界的拉普拉斯方程。当我运行 C++ 代码时,我可以输入所有边界值,并将值传输到主程序没有问题。但是在计算中出错了。

标签: c++pde

解决方案


推荐阅读