게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[본삭금]C++ 행렬 동적할당 관련 질문하나드립니다..ㅠㅠ
게시물ID : programmer_6074짧은주소 복사하기
작성자 : ABCDEFG컵
추천 : 0
조회수 : 692회
댓글수 : 15개
등록시간 : 2014/10/19 22:24:15
옵션
  • 본인삭제금지
#include <iostream> 
using namespace std; 

struct matrix{ 

int m; // number of rows 
int n; // number of columns 
float **p; // pointer to 2D array 

}; 
int main() 
struct matrix A,B,C;
// 1. matrix A 의 사이즈 (m-by-n) 을 읽어들이고, 적당한 메모리를 동적으로 할당한후, 각각의 엔트리를 읽어들이시오. 
cout <<"Type value of rows and columns of what you want to create a matrix A." <<endl;
cin >>A.m >>A.n;
A.p = new float *[A.m];
for ( int i=0; i<(A.m); i++)
{
A.p[i] = new float [A.n];
for ( int j=0; j<(A.n); j++)
{
A.p[i][j] = i+j;
cout <<A.p[i][j] <<" ";
}
cout <<endl;
}
// 2. matrix B 의 사이즈 (m-by-n) 을 읽어들이고, 적당한 메모리를 동적으로 할당한후, 각각의 엔트리를 읽어들이시오. 
cout <<"Type value of rows and columns of what you want to create a matrix B." <<endl;
cin >>B.m >>B.n;
B.p = new float *[B.m];
for ( int i=0; i<(B.m); i++)
{
B.p[i] = new float [B.n];
for ( int j=0; j<(B.n); j++)
{
B.p[i][j] = i+j;
cout <<B.p[i][j] <<" ";
}
cout <<endl;
}
// 3. matrix C 의 사이즈 (m-by-n) 을 설정하고, 적당한 메모리를 동적으로 할당한후, C = A * B 를 계산하여 출력 하시오.
cout <<"matrix C is sized by rows of matrix A, and columns of matrix B." <<endl;
C.m = A.m;
C.n = B.n;
C.p = new float *[C.m];
for ( int i=0; i<(C.m); i++)
{
C.p[i] = new float [C.n];
for ( int j=0; j<(C.n); j++)
{
for ( int k=0; k<(A.n); k++)
{
C.p[i][j] = (A.p[i][k]*B.p[k][j]);
}
cout <<C.p[i][j] <<" ";
}
cout <<endl;
delete C.p[i];
}
delete C.p;
// 4. 동적으로 할당된 모든 메모리를 해제하시오. 

system("pause");
return 0; 
}

현재 이상태인데,

54번째줄, 그러니까 C행렬 세번째 for 구문에서

C.p[i][j] = (A.p[i][k]*B.p[k][j]); 이부분이

C.p[i][j] = C.p[i][j] + (A.p[i][k]*B.p[k][j]); 가 되어야 A,B의 곱이 되는건 알겠는데

그렇게입력하면 자꾸 쓰레기값으로 출력되네요...ㅠㅠ 무슨문제인지 알수가없어 질문합니다..
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호