2 개의 .txt 파일을 2 5 * 5 배열로 읽는 코드를 작성해야합니다. 여러 섹션에서 코드를 검사하여 작동하는지 확인하고 데이터 형식을 .txt 파일을 내 2 배열에 저장하는 데 문제가없는 경우 문제는 이제 2 행을 행렬로 사용하여 함께 곱하면됩니다. 3 배열/매트릭스를 만듭니다. 나는이 작업을 수행하는 방법에 대해 몇 시간 동안 인터넷과 포럼을 검색해 왔으며, 내가 발견 한 것을 기반으로 아래 코드를 만들었습니다. 나에게 코드는 (내가 초보자 오전 비록 내가 C++에 올 때) 소리를 보이지만 내 배열 3.C++에서 배열을 사용하는 행렬 곱셈
using namespace std;
#include<iostream>
#include<fstream>
#include<string>
int main()
{ int count1,count2;
int i,j,m,n;
int myarray1[5][5];
int myarray2[5][5];
int myarray3[5][5];
string file1,file2,mystring1,mystring2;
cout<<"please enter the name of the first file"<<endl;
cin>>file1;
cout<<"please enter the name of the second file"<<endl;
cin>>file2;
ifstream inFile;
inFile.open(file1.c_str());
for (count1=0;count1<26;++count1)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
while (!inFile.eof())
{
getline(inFile,mystring1,',');
int value1 = atoi(mystring1.c_str());
myarray1[i][j]=value1;
cout<<myarray1[i][j];
}
}
}
}
system("pause");
inFile.close();
system("pause");
inFile.open(file2.c_str());
for (count2=0;count2<26;++count2)
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
while (!inFile.eof())
{
getline(inFile,mystring2,',');
int value2 = atoi(mystring2.c_str());
myarray2[i][j]=value2;
cout<<myarray2[i][j];
}
}
}
}
inFile.close();
system("pause");
for(m=0;m<5;m++)
{
for(n=0;n<5;n++)
{
myarray3[m][n]=(myarray1[0][m]*myarray2[n][0])
+(myarray1[1][m]*myarray2[n][1])
+(myarray1[2][m]*myarray2[n][2])
+(myarray1[3][m]*myarray2[n][3])
+(myarray1[4][m]*myarray2[n][4]);
cout<<myarray3[m][n]<<endl;
}
system("pause");
}
를 표시 할 때 나는 이상한 출력을 얻고이 출력 내가 얻을 :
please enter the name of the first file
C:\matrix1.txt
please enter the name of the second file
C:\matrix2.txt
1234554321234543212345432Press any key to continue . . .
Press any key to continue . . .
5678923412457892562112345Press any key to continue . . .
-1546188214
1030792152
1030792152
1030792152
1030792152
-1546188228
-858993456
-858993456
-858993456
-858993456
-1546188228
-858993456
-858993456
-858993456
-858993456
-1546188228
-858993456
-858993456
몇 가지 문제 :'for (count1 = 0; count1 <26; ++ count1)'는 for (count1 = 0; count1 <25; ++ count1)'이 아니어야합니까? 'while (! inFile.eof())'이 Q & A를 참고하십시오 : [iostream :: eof가 루프 상태에있는 것이 잘못된 이유는 무엇입니까?] (http://stackoverflow.com/questions/5605125/why-is-iostreameof- inside-a-loop-condition-considered-wrong) –
데이터 예제를 제공해 주시겠습니까? (출력이 "이상한"방법은 무엇입니까? 계산 후, 즉 나중에 다시 인쇄하려고 할 때 myarray3을 인쇄 할 때 이상한가요?) –