Wie ktoś może jak to zrobić.
Wywaliłem do funkcji z maina całe czytanie z pliku tekstowego. Nazywa się ta funkcja
ReaderValues
Jednakże funkcja
Multiplication
używa tychże dwóch tablic i teraz mam problem, bo jak miałem to w mainie to po prostu przesyłałem je jako argument i tyle. Najlepiej, żeby obyło się to bez wskaznikow, jezeli sie nie da, to moga w ostatecznosci byc te wskaźniki :D
Czyli działa to tak: W funkcji ReaderValues są czytane wartości z pliku i przypiswane do tablic, z których korzysta pozniej funkcja Multiplication.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
if(argc < 3)
{
printf("Za malo argumentow");
return 0;
}
int rowlength,collength,row2length,col2length;
int row=0,col=0;
printf("Input number of rows for the first array!");
scanf("%d",&rowlength);
printf("Input number of columns for the first array!");
scanf("%d",&collength);
printf("Input number of rows for the second array!");
scanf("%d",&row2length);
printf("Input number of columns for the second array!");
scanf("%d",&col2length);
printf("Elements in the text file:\n\n");
FILE* file = fopen(argv[1], "r");
ReaderValues(file,rowlength,collength,row2length,col2length);
fclose(file);
Multiplication(rowlength,collength,row2length,col2length, tab, tab1,argv);
return 0;
}
void ReaderValues(FILE* file, int rowlength, int collength, int row2length, int col2length)
{
int tab[rowlength][collength],tab1[row2length][col2length];
int row=0,col=0;
while( fscanf( file, "%d,", &tab[row][col] ) != EOF ) //WCZYTYWANIE WARTOŚCI Z PLIKU TXT DO 2 TABLIC
{
printf("%d ", tab[row][col]);
col++;
if(col==collength && row==rowlength-1)
{
printf("\n\n");
row=0;
col=0;
while( fscanf( file, "%d,", &tab1[row][col] ) != EOF )
{
printf("%d ", tab1[row][col]);
col++;
if(col==col2length)
{
printf("\n");
col=0;
row++;
}
}
}
if(col==collength)
{
printf("\n");
col=0;
row++;
}
}
printf("\n");
}
int Multiplication(int rowlength, int collength, int row2length, int col2length, int tab[rowlength][collength],int tab1[row2length][col2length],char* argv[]) // MNOŻENIE MACIERZY
{
int i,j,k;
int tab2[rowlength][col2length];
printf("Result of multiplication:\n\n");
for(i=0; i<rowlength; i++)
{
for(j=0; j<col2length; j++)
{
tab2[i][j] = 0;
for(k=0; k<row2length; k++)
{
tab2[i][j] += tab[i][k] * tab1[k][j];
}
}
}
for(i=0; i<row2length; i++)
{
for(j=0; j<col2length; j++)
{
printf("%d ", tab2[i][j]);
}
printf("\n");
}
Save(row2length, col2length, tab2, argv);
}
void Save(int row2length, int col2length, int tab2[row2length][col2length], char* argv[]) // ZAPISYWANIE MACIERZY WYNIKOWEJ DO PLIKU
{
int i,j;
FILE* file2;
file2 = fopen(argv[2], "w");
if(file2==NULL) printf("Can't open the file!");
else
{
fprintf(file2,"The result: \n");
for(i=0; i<row2length; i++)
{
for(j=0; j<col2length; j++)
{
fprintf(file2,"%d ", tab2[i][j]);
}
fprintf(file2,"\n");
}
fclose(file2);
}
}