
28.05.2010, 18:52
|
|
Познающий
Регистрация: 22.11.2009
Сообщений: 53
С нами:
8667664
Репутация:
0
|
|
Всем привет. Проблема в следующем.
Код:
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
void Diagonal(double **A, int NumberOfEquation) {
double *temp_massiv;
int i, j;
double max, replace;
temp_massiv = new double[NumberOfEquation];
for(i = 0;i < NumberOfEquation;i++){
max = A[i][0];
for(j = 0;j < NumberOfEquation;j++){
if(A[i][i] != 0) continue;
else{
if(A[i][j] >= max) max = A[i][j];
temp_massiv[i] = max;
}
}
}
for(i = 0;i < NumberOfEquation;i++){
for(j = 0;j < NumberOfEquation;j++){
if(A[i][j] == temp_massiv[i]){
replace = A[i][i];
A[i][i] = A[i][j];
A[i][j] = replace;
}
}
}
delete []temp_massiv;
}
int SimpleIteration(double **A, double *B, int NumberOfEquation, double *solution, double fault_input) {
int i, j, step = 1;
double *TempSolution;
TempSolution = new double[NumberOfEquation];
double fault = fault_input + 1;
for(i = 0;i < NumberOfEquation;i++) {
for(j = 0;j < NumberOfEquation;j++) {
if(i != j) {
A[i][j] = -A[i][j] / A[i][i];
}
}
B[i] = B[i] / A[i][i];
A[i][i] = 0;
}
for(i = 0;i < NumberOfEquation;i++) {
solution[i] = B[i];
}
while(fault > fault_input && step <= 1000) {
for(j = 0;j < NumberOfEquation;j++) {
TempSolution[j] = 0;
}
for(i = 0;i < NumberOfEquation;i++) {
for(j = 0;j < NumberOfEquation;j++) {
TempSolution[i] = TempSolution[i] + A[i][j]*solution[j];
}
TempSolution[i] = TempSolution[i] + B[i];
}
fault = 0.0;
for(j = 0;j < NumberOfEquation;j++) {
fault = fault + (solution[j] - TempSolution[j])*(solution[j] - TempSolution[j]);
}
fault = sqrt(fault);
for(j = 0;j < NumberOfEquation;j++) {
solution[j] = TempSolution[j];
}
step++;
}
delete []TempSolution;
return step;
}
void file_input_output(){
int i, j, row = 0, column = 0;
double **A, *B, *solution, **temp_matrix, fault_input;
char k[128];
//вычисляем размерность введенной расширенной матрицы системы
ifstream in("input_data.txt");
while(true) {
in.read(k, 1);
if(k[0] == ' ') column++;
if(k[0] == '\n'){
row++;
column++;
}
if(in.eof()){
row++;
column++;
break;
}
}
column = column/row;
in.close();
//выделяем память под матрицу коэффициентов при неизвестных
A = new double*[row];
for(i = 0;i < row;i++){
A[i] = new double[row];
}
//выделяем память под вектор свободных членов
B = new double[row];
//выделяем память под вспомогательную матрицу
temp_matrix = new double*[row];
for(i = 0;i < column;i++){
temp_matrix[i] = new double[row];
}
//выделяем память под вектор неизвестных
solution = new double[row];
//считываем матрицу во временный массив
ifstream fin("input_data.txt");
for(i = 0;i < row;i++){
for(j = 0;j < column;j++){
fin >> temp_matrix[i][j];
}
}
fin.close();
//из временного массива выбираем нужные элементы
//для матрицы коэффициентов при неизвестных
for(i = 0;i < row;i++){
for(j = 0;j < column - 1;j++){
A[i][j] = temp_matrix[i][j];
}
}
//для вектора свободных членов
for(i = 0;i < row;i++){
for(j = column - 1;j > column - 2;j--){
B[i] = temp_matrix[i][j];
}
}
Diagonal(A, row);
cout << "Введите точность вычислений: ";
cin >> fault_input;
int steps = SimpleIteration(A, B, row, solution, fault_input);
if(steps > 1000) {
cout << "Ошибка!!! Возможные причины:\n";
cout << "1)Слишком большое количество итераций.\n";
cout << "2)Какие-либо строки(столбцы) линейно зависимы.\n";
}
else {
ofstream out("solution.txt");
out << "Решение СЛАУ:\n";
for(j = 0;j < row;j++) {
out << solution[j] << "\n";
}
out << "Количество итераций: " << steps;
cout << "Результат вычислений записан в файл 'solution.txt'.\n";
out.close();
system("solution.txt");
}
//очищаем выделенную память, здесь все нормально
for(i = 0;i < row;i++) {
delete []A[i];
}
delete []A;
delete []B;
delete []solution;
//а вот на этом месте,при очистки памяти выделенной под временный
//массив, прога при работе выдает ошибку(компилится все норм)
for(i = 0;i < column;i++){
delete []temp_matrix[i];
}
delete []temp_matrix;
}
int main() {
cout << "Данная программа предназначена для решения СЛАУ методом простых итераций.\n";
file_input_output();
system("pause");
return 0;
}
Вот сама ошибка, среда VS2010.
Код:
Debug Assertion Failed!
...
Expression:_CrtIsValidHeapPointer(pUserData)
...
Плиз, помогите разобраться что не так, просто раньше норм все работало, а щас че т не хочет.
|
|
|