[問題] 有時候記憶體會找不到位置
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Xcode
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
std
問題(Question):
我的問題在於,呼叫printmatrix函式時,有時候記憶體會無法讀到位置,有時候卻可以
找到並執行,但明明Input都是一模一樣的,只需要告訴我原因即可,謝謝
餵入的資料(Input):
一樣的矩陣跟資料輸入數次,卻是不同的結果
2
2 3
4 5
3 5
6 8
預期的正確結果(Expected Output):
A =
2 3
4 5
B =
3 5
6 8
A+B =
5 8
10 13
A-B =
-1 -2
-2 -3
A*B =
24 34
42 60
(這是有時候跑出來的執行結果,有時候卻跑不出來)
錯誤結果(Wrong Output):
有時候跑不出來,我完全沒頭緒@@
程式碼(Code):(請善用置底文網頁, 記得排版)
//
// Matrix.h
// L4 HW
//
// Created by 葉柏宏 on 2016/7/1.
// Copyright 2016年 Dannel Apple. All rights reserved.
//
#ifndef Matrix_h
#define Matrix_h
template<class _Tx>
void swap(_Tx &a, _Tx &b) {
_Tx t = a;
a = b;
b = t;
}
class Matrix{
size_t n;
double *mtxptr=new double;//a obj with algo idx
public:
void assignDimension(int dim){
this->n=dim;
}
void assignElements(){
for(size_t i=0;i<(n*n);i++) cin>>*(mtxptr+i);
}
void printMatrix(){
for(size_t i=0;i<n*n;i++){
if(i%n==0) cout<<"\n";
std::cout<<*(mtxptr+i)<<" ";
}
std::cout<<std::endl;
}
void assignMatrix(Matrix A){
this->n=A.n;
for(size_t i=0;i<n*n;i++) *((this->mtxptr)+i)=*((A.mtxptr)+i);
}
void transposeMatrix(){
for(size_t i=0;i<n*n;i++)if((i%n)>(i/n)) swap(*((this->mtxptr)+i), *((this->mtxptr)+(i%n)*n+i/n));}
void addMatrix(Matrix A,Matrix B){setzero();
for(size_t i=0;i<n*n;i++) *(mtxptr+i)=*(A.mtxptr+i)+*(B.mtxptr+i);
}
void subtractMatrix(Matrix A,Matrix B){setzero();
for(size_t i=0;i<n*n;i++) *(mtxptr+i)=*(A.mtxptr+i)-*(B.mtxptr+i);
}
void multiplyMatrix(Matrix A,Matrix B){setzero();
for(size_t i=0;i<n*n;i++){
for(size_t j=0;j<n;j++){
*((this->mtxptr)+i)+=*((A.mtxptr)+i/n*n+j)**((B.mtxptr)+n*j+i%n);
}
}
}
void setzero(){
for(size_t i=0;i<n*n;i++) *((this->mtxptr)+i)=0;
}
};
#endif /* Matrix_h */
補充說明(Supplement):
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.114.72.205
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1467358056.A.BDE.html
→
07/01 15:28, , 1F
07/01 15:28, 1F
推
07/01 15:54, , 2F
07/01 15:54, 2F
→
07/01 15:55, , 3F
07/01 15:55, 3F
→
07/01 15:55, , 4F
07/01 15:55, 4F
→
07/01 15:59, , 5F
07/01 15:59, 5F
→
07/01 16:02, , 6F
07/01 16:02, 6F
→
07/01 16:06, , 7F
07/01 16:06, 7F
→
07/01 16:08, , 8F
07/01 16:08, 8F
推
07/01 16:18, , 9F
07/01 16:18, 9F
→
07/02 11:34, , 10F
07/02 11:34, 10F
噓
07/02 13:13, , 11F
07/02 13:13, 11F
推
07/03 00:24, , 12F
07/03 00:24, 12F
推
07/03 00:27, , 13F
07/03 00:27, 13F
→
07/03 00:27, , 14F
07/03 00:27, 14F
推
07/03 06:08, , 15F
07/03 06:08, 15F
→
07/03 06:09, , 16F
07/03 06:09, 16F
→
07/03 06:09, , 17F
07/03 06:09, 17F
→
07/03 06:10, , 18F
07/03 06:10, 18F
→
07/03 06:11, , 19F
07/03 06:11, 19F
→
07/03 06:12, , 20F
07/03 06:12, 20F
→
07/03 06:13, , 21F
07/03 06:13, 21F
→
07/03 06:13, , 22F
07/03 06:13, 22F