-3
#include <stdio.h>
/*this puts the numbers fomr the file into two matrices*/
void readMatrices(FILE*numbers, int array[4][4], int array2[4][4])
{
int i,j,num;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
fscanf(numbers,"%d",&array[i][j]);
}
}
for(i=0;i<=4;i++)
{
for(j=0;j<4;j++)
{
fscanf(numbers,"%d",&array2[i][j]);
}
}
}
void printMatrices(int array[4][4],int array2[4][4]) /* prints out the matrices*/
{
int i,j,num;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",array[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<=4;i++)
{
for(j=0;j<4;j++)
{
printf("%d ",array2[i][j]);
}
printf("\n");
}
printf("\n");
}
/*multiplies the matrices*/
void multiplyMatrices(int array[4][4],int array2[4][4],int result[4][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
result[i][j] = (array[i][0]*array2[0][j])+(array[i][1]*array2[1][j])+(array[i][2]*array2[2][j])+(array[i][3]*array2[3][j]);
printf("%d ",result[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
int array[4][4],array2[4][4],results[4][4];
FILE*numbers;
numbers = fopen("numbers.txt", "r");
readMatrices(numbers,array,array2);
while(array[0][0]!=0)
{
printMatrices(array,array2);
multiplyMatrices(array,array2,results);
readMatrices(numbers,array,array2);
}
fclose(numbers);
return 0;
}
Here is the file。これはファイルを読み込みますが、そうでなければならないような順序ではなく、4つのランダムなグループで読み込みます。これは問題であり、ファイル内の0
が無限ループする原因となります。ファイルからランダムに読み込むのはなぜですか?
'w +'の代わりに 'r'を使って開いたのでです。 –
w + doで正確に何が開きますか? – tehflamex
私はw +でそれを開こうとしました、そして、ちょうどトンの乱数を得ました;それでもまだループしていません – tehflamex