Facebook
From Colossal Tortoise, 6 Years ago, written in C.
This paste is a reply to Untitled from Gray Tapir - go back
Embed
Viewing differences between Untitled and Re: Untitled
#include 
#include 

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);

    int tab[rowlength][collength],tab1[row2length][col2length];
    int tab2[rowlength][col2length];

    printf("Elements in the text file:\n\n");

    FILE* file = fopen(argv[1], "r");

    ReaderValues(file,rowlength,collength,row2length,col2length, tab, tab1);

    fclose(file);

    Multiplication(rowlength,collength,row2length,col2length, tab, tab1,tab2,argv);

    Save(rowlength, col2length, tab2, argv);

    return 0;
}


void ReaderValues(FILE* file, int rowlength, int collength, int row2length, int col2length, int tab[rowlength][collength], int 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],int tab2[rowlength][col2length]) // MNOŻENIE MACIERZY
{
    int i,j,k;
    printf("Result of multiplication:\n\n");

    for(i=0; i     {
        for(j=0; j         {
            tab2[i][j] = 0;
            for(k=0; k             {
                tab2[i][j] += tab[i][k] * tab1[k][j];
            }
        }
    }

    for(i=0; i     {
        for(j=0; j         {
            printf("%d ", tab2[i][j]);
        }
        printf("\n");
    }

}
void Save(int rowlength, int col2length, int tab2[rowlength][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         {
            for(j=0; j             {
                fprintf(file2,"%d ", tab2[i][j]);
            }

            fprintf(file2,"\n");
        }

        fclose(file2);
    }
}