打印高斯消除算法的结果时出现分段错误 [英] Segmentation fault while printing the result for gauss elimination algorithm

查看:60
本文介绍了打印高斯消除算法的结果时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我从书中得到了高斯消除方法的跟随算法。但我不知道如何打印从以下算法获得的联立方程的根。算法如下:

  1 。程序Gaussian_Elimination(A,b,y)
2 开始
3 for k:= 0 to n- 1 执行
4 开始
5 for j:= k + 1 to n-1 执行
6 。 A [k,j]:= A [k,j] / A [k,k];
7 。 y [k]:= b [k] / A [k,k];
8 。 A [k,k]:= 1;
9 for i:= k + 1 to n-1 执行
10 开始
11 for j:= k + 1 to n-1 do
12 。 A [i,j]:= A [i,j] - A [i,k] * A [k,j];
13 。 b [i]:= b [i] -A [i,k] * y [k];
14 。 A [i,k]:= 0;
15 。 endfor; * // * Line 9 * /
/ * 16。 ENDFOR; * // *第3行* /
/ * 17。 结束





请检查我的代码我的代码并告知我,如果这个建议是正确的。我的代码如下:



 #include< iostream> 
#include< cmath>
// #include< mpi.h>
#include< fstream>
#include< ctime>
#include< vector>
#define n 3
using namespace std;

void GaussianElimination(double **,double * b,double * y);
int main(int argc,char * argv [])
{
/ * values
Row0 = 0,2,1; b0 = -8
Row1 = 1,-2,-3; b1 = 0
Row3 = -1,1,2; b2 = 3

答案= -4,-5,2 * /



< pre> double * A [n] = { NULL,NULL,NULL};
double * b = NULL;
double * y = NULL;

for(int i = 0; i< n; i ++){
A [i] = new double [n];
if(A [i] == NULL){
cout<< 内存分配问题:;
返回-2;
}
}
b = new double [n];
if(b == NULL){
cout<< 内存分配问题:;
返回-2;
}

y = new double [n];
if(y == NULL){
cout<< 内存分配问题:;
返回-2;
}

A [0] [0] = 0.0; A [0] [1] = 2.0; A [0] [2] = 1.0; // A [0] [3] = -8.0;
A [1] [0] = 1.0; A [1] [1] = -2.0; A [1] [2] = -3.0; // A [1] [3] = 0.0;
A [2] [0] = -1.0; A [0] [1] = 1.0; A [0] [2] = 2.0; // A [0] [3] = 3.0;
b [0] = -8.0;
b [1] = 0.0;
b [2] = 3.0;


GaussianElimination(A,b,y);
delete [] y;
delete [] b;
for(int i = 0; i< n; i ++)
delete [] A [i];
//删除[] A;无需删除A,因为A未动态创建
}
void GaussianElimination(double ** A,double * b,double * y){
for(int k = 0; k< n-1; ++ k){
for(int j = k + 1; j< n-1; ++ j)
A [k] [j] = A [k] [ j]的/ A [K] [K];
y [k] = b [k] / A [k] [k];
for(int i = k + 1; k< n-1; i ++){
for(int j = k + 1; j< n-1; j ++)
A [i ] [j] = A [i] [j] -A [i] [k] * A [k] [j];
b [i] = b [i] -A [i] [k] * y [k];
A [i] [k] = 0;
}
}

for(int i = 0; i< n-1; i ++)
cout<< y [i];
}


我正在打印y的内容,但它给我分段错误。有些人请指导我。
< pre> $ ./a.out
分段错误(核心转储)
$





Zulfi。



我尝试了什么:



我尝试过打印'b'的值也,但它也给我分段错误

解决方案

./ a.out
分段错误(核心转储)





Zulfi。



< b>我尝试了什么:



我尝试打印'b'的值但它也给我分段错误


Quote:

分段错误:如何打印高斯消除算法的解决方案



这意味着您的代码尝试使用它不拥有的内存空间,在数组结束后尝试读/写的地方。

学习工具可以帮助您找到bug自己,学习调试器。

----

你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器上逐步运行代码步骤,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有魔法调试器,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


Hi,
I got following algorithm of Gauss Elimination method from the book. But I don't know how to print the roots of the simultaneous equations obtained from the following algorithm. The algorithm is given below:

1. procedure Gaussian_Elimination(A, b, y)
  2. begin
  3.    for k:= 0 to n-1 do
  4.      begin
  5.       for j:=k + 1 to n-1 do
  6.          A[k,j] := A[k,j] /A[k,k];
  7.       y[k] : = b[k]/A[k,k];
  8.       A[k,k] :=1;
  9.       for i:= k + 1 to n-1 do
 10.       begin
 11.           for j:= k+1 to n-1 do
 12.              A[i,j]:= A[i,j] - A[i,k] * A[k, j];
 13.           b[i] := b[i] -A[i, k] * y[k];
 14.            A[i, k] :=0;
 15.       endfor;*//* Line 9 */
 /*16. endfor; *//*line 3*/
 /*17. end



Please check my code my code and tell me if the implmentation is correct or not. My code is given below:

#include <iostream>
#include <cmath>
//#include <mpi.h>
#include <fstream>
#include <ctime>
#include <vector>
#define n  3
using namespace std;

void GaussianElimination(double **,double *b ,double *y);
int main(int argc, char * argv[])
{
  /* values
  Row0 = 0, 2, 1; b0= -8
  Row1 = 1, -2, -3; b1 = 0
  Row3 = -1, 1, 2; b2= 3

  Ans = -4, -5, 2 */
 
  
  
 <pre>double *A[n]={NULL, NULL, NULL} ;
  double *b = NULL;
  double *y= NULL;

  for (int i=0; i < n; i++){
    A[i] = new double [n];
    if (A[i] == NULL){
       cout<< " problem with memory Allocation:";
       return -2;
    }
  }
  b = new double[n];
  if(b== NULL){
    cout<< " problem with memory Allocation:";
    return -2;
  }
 
  y = new double[n];
  if(y== NULL){
    cout<< " Problem with Memory Allocation:";
    return -2;
  }
  
  A[0][0] =0.0;A[0][1] =2.0; A[0][2] = 1.0; //A[0][3] = -8.0;
  A[1][0] =1.0;A[1][1] =-2.0; A[1][2] =-3.0; //A[1][3] = 0.0;
  A[2][0] =-1.0;A[0][1] =1.0; A[0][2] = 2.0; //A[0][3] = 3.0;
  b[0] = -8.0;
  b[1] = 0.0;
  b[2] = 3.0;


  GaussianElimination(A, b ,y);
  delete [] y;
  delete [ ]b;
  for(int i = 0; i < n; i++)
    delete [] A[i];
 // delete [ ] A; No need to delete A because A is not created dynamically  
}
  void GaussianElimination(double **A, double *b,double *y) {
     for(int k=0; k<n-1; ++k) {
        for(int j= k+1; j <n-1; ++j)
           A[k][j] = A[k][j]/A[k][k]; 
        y[k] = b[k]/A[k][k];
        for(int i=k+1; k<n-1; i++){
           for(int j=k+1; j<n-1; j++)
              A[i][j] = A[i][j] -A[i][k] *A[k][j];
           b[i]= b[i] -A[i][k] * y[k];
           A[i][k] = 0;
        }
     }

     for(int i=0; i<n-1; i++)
        cout<<y[i];
}        


I am printing the contents of y but its giving me segmentation fault. Some body please guide me.
<pre>$ ./a.out
Segmentation fault (core dumped)
$ 



Zulfi.

What I have tried:

I tried printing the value of 'b' also but it also gives me segmentation fault

解决方案

./a.out Segmentation fault (core dumped)




Zulfi.

What I have tried:

I tried printing the value of 'b' also but it also gives me segmentation fault


Quote:

Segmentation fault: how to print the solution for gauss elimination algorithm


This mean that your code try to use a memory space it don't own, somewhere you try to read/write after the end of an array.
Learn tool that will help you to find bug by yourself, learn debugger.
----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于打印高斯消除算法的结果时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆