-3

In this code, I created files with matrix entries with names such as 1.txt, 2.txt, 3.txt and containing a total of 2 matrices. The program reads the 2 matrices in each file and multiplies the pairs of matrices it reads and prints the resulting matrix to a text file called output.txt. But when printing, it only multiplies the values in the 3.txt file, which is the last text file, and creates an output.txt file containing only those values. I want it to print the results for each input file to the same output file. But I couldn't do that. How can I do that ?

`#include <iostream>
#include <fstream>
#include <filesystem>
#include <pthread.h>
using namespace std;

#define MAX 5
#define MAX_THREAD 5

int matA[MAX][MAX];
int matB[MAX][MAX];
int matC[MAX][MAX];
int mc_rows = 0;

void* multi(void* arg)
{
    int i = mc_rows++; // i denotes row number of resultant matC

    for (int j = 0; j < MAX; j++)
        for (int k = 0; k < MAX; k++)
            matC[i][j] += matA[i][k] * matB[k][j];

    pthread_exit(0);
}

void ReadMatricesFromFile(const std::filesystem::path& directory)
{
    int fileCount = 0;
    for (const auto& entry : std::filesystem::directory_iterator(directory))
    {
        if (entry.is_regular_file() && entry.path().extension() == ".txt")
        {
            ifstream inputFile(entry.path());
            if (!inputFile)
            {
                cout << "Failed to open " << entry.path() << endl;
                continue;
            }

            cout << "Reading " << entry.path() << endl;

            for (int i = 0; i < MAX; i++) {
                for (int j = 0; j < MAX; j++) {
                    inputFile >> matA[i][j];
                    cout << matA[i][j] << " ";
                }
                cout << endl;
            }

            cout << endl;

            for (int i = 0; i < MAX; i++) {
                for (int j = 0; j < MAX; j++) {
                    inputFile >> matB[i][j];
                    cout << matB[i][j] << " ";
                }
                cout << endl;
            }

            cout << endl;

            inputFile.close();
            fileCount++;
        }
    }

    if (fileCount == 0)
    {
        cout << "No .txt files found in the directory: " << directory << endl;
    }
}

int main()
{
    std::filesystem::path directoryPath = "/mnt/c/users/monster/desktop/matris/txt_files"; // Klasör yolunu buraya girin
    ReadMatricesFromFile(directoryPath);

    pthread_t threads[MAX_THREAD];

    for (int i = 0; i < MAX_THREAD; i++) {
        int* p;
        pthread_create(&threads[i], NULL, multi, (void*)(p));
    }

    for (int i = 0; i < MAX_THREAD; i++)
        pthread_join(threads[i], NULL);

    // Write the result matrix to output.txt
    ofstream outputFile("output.txt");
    if (!outputFile) {
        cout << "Failed to create output.txt" << endl;
        return 1;
    }

    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < MAX; j++) {
            outputFile << matC[i][j] << " ";
        }
        outputFile << endl;
    }

    outputFile.close();

    return 0;
}`

1.txt file:

enter image description here

2.txt file:

enter image description here

3.txt file:

enter image description here

4.txt file:

enter image description here

enter image description here

OUTPUT FILE RESULTS BUT FOR ONLY 4.txt FILE :(

enter image description here

cigien
  • 57,834
  • 11
  • 73
  • 112
Fatih
  • 23
  • 3
  • 2
    Oddly when I run your program and feed it the images of the test files it completely fails. – user4581301 Jun 01 '23 at 20:45
  • Digging through the code I see `int* p; pthread_create(&threads[i], NULL, multi, (void*)(p));`. Since `multi`'s `arg` parameter is never used, you can safely get rid of `p` and pass in `NULL`: `pthread_create(&threads[i], NULL, multi, NULL);`. – user4581301 Jun 01 '23 at 20:49
  • 1
    Let me start and type in the file data: 1, 1, 1, 1, 1, and then 1,1,1,1,...forget it. Please post the file data as *text*, not as images. – PaulMcKenzie Jun 01 '23 at 20:50
  • Looks like the program reads each file into `matA` and `matB`, overwriting whatever's in those arrays from the last file, then the program processes what was read. Think about that for a minute. How can you reasonably expect anything other than the last file to be in `matA` and `matB`? – user4581301 Jun 01 '23 at 20:54
  • 1
    Recommendation: Step through the program with a debugger and watch what's happening. You should have the problem figured out a few steps into the second iteration of `for (const auto& entry : std::filesystem::directory_iterator(directory))` – user4581301 Jun 01 '23 at 20:57
  • Can you please fixed this code and share with me. Because my brain is freezed :/. I am new this topic. This is hard to understand for me. And my english is not very good. Sorry for my bad english. – Fatih Jun 01 '23 at 21:28
  • 1
    You may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) – Andreas Wenzel Jun 01 '23 at 21:45
  • 1
    `int i = mc_rows++;` is UB. Also [please do not post screenshots of text](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). – n. m. could be an AI Jun 01 '23 at 21:55
  • I think i dont need to use a debugger. I think there is logical error. İf i use a debugger. I will not see anything about logical error. – Fatih Jun 01 '23 at 23:13
  • You need to use a debugger precisely because it is a problem with the logic. The debugger shows you exactly how the program runs. It does not care about your intent, your assumptions, or anything else. It will show you what is. Nothing more and nothing less. You approach a debugger with a set of expectations. You then step through the program watching what happens. As soon as you spot the program doing something that is unexpected, you have found a bug, and from your expectations you know what it should have done instead. You then adjust the code accordingly (often not as easy as it sounds) – user4581301 Jun 01 '23 at 23:19
  • Say you have two pieces of paper. These represent `matA` and `matB`. You look at the file, read out the numbers and write them onto your two pieces of paper. When you reach the end of the file and want to start reading a new file what would you do? You would reach for two new pieces of paper to write the numbers from the new file onto. Now you have 4 pieces of paper. Your program does not do this. It uses the same two pieces of paper and writes over the numbers from the first file, replacing the first file's numbers with the new numbers from the new file. – user4581301 Jun 01 '23 at 23:23

1 Answers1

1

Your file is named "output.txt" for all matrices, and when opening a file by default it cleans and delete its content.

You have two options :

  1. Specify your code to not delete the inside of the output.txt file : something like this probably

  2. have an output file per matrix : output1.txt, output2.txt etc...

Hope that helps

Thibault Cimic
  • 320
  • 1
  • 9