#What's wrong with my code?

21 messages · Page 1 of 1 (latest)

tame inletBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#
sbdswr /good orc is dead orc/
Please Do Not Send Screenshots!

They're hard to read and prevent copying and pasting.

#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
pure shuttle
#

It's too long but I'll try

#
#include <iostream>
#include <stdlib.h>
using namespace std;

// Define method to free matrices;
void freeMatrix(int** matrix, int rows);

int main() {
    int rows, cols, ** matrix = nullptr, * vectr = nullptr, * resVectr = nullptr;
    cout << "Input rows and columns of the matrix: ";
    cin >> rows >> cols;

    // Attempt to initialize the matrix and the vector and calculate the result; Using try-catch for easier error-handling.
    try { 
        matrix = new int* [rows];
        for (int i = 0; i < rows; i++) {
            matrix[i] = new int[cols];
            for (int j = 0; j < cols; j++) {
                printf("Input matrix element (%d, %d): ", i + 1, j + 1);
                cin >> matrix[i][j];
            }
        }

        vectr = new int[rows];
        for (int i = 0; i < rows; i++) {
            printf("Input vector element (%d): ", i + 1);
            cin >> vectr[i];
        }

        resVectr = new int[rows];
        for (int i = 0; i < rows; i++) {
            int num = 0;
            for (int j = 0; j < cols; j++) {
                num += matrix[i][j] * vectr[i];
            }
            resVectr[i] = num;
        }
    }
    catch (const bad_alloc& memoryException) {
        // Free allocated memory;
        cerr << "Memory allocation failed: " << memoryException.what() << endl;
        freeMatrix(matrix, rows);
        delete[] vectr;
        delete[] resVectr;
        return 1;
    }
    
    // Print resul;
    cout << "Result: ";
    for (int i = 0; i < rows; i++)
    {
        cout << resVectr[i] << " ";
    }
    cout << endl;

    // Free allocated memory;
    freeMatrix(matrix, rows);
    delete[] vectr;
    delete[] resVectr;
    return 0;
}

// Initialize the matrix-freeing method.
void freeMatrix(int** matrix, int rows) {
    for (int i = 0; i < rows; i++) {
        delete[] matrix[i];
    }
    delete[] matrix;
}
#

Okay nvm worked this time, I guess I couldn't in the post because it had text too

crystal wave
#

regarding void for main your professor should maybe go and learn C themselves, the standard format of main returns int

#

well, also I'm not sure if you mean C or C++, posting in C forum and showing C++ code

#

but at least regarding return type of main it makes no difference

#

those are two separate languages, C and C++

pure shuttle
#

I can move it to cpp help

pure shuttle
#

ye alright will move it there

#

Just didn't see the other category and forgot this server has c too

#

sorry

pallid hamlet
#

ah

tame inletBOT
#

@pure shuttle

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

pallid hamlet
#

!solved

tame inletBOT
pure shuttle
#

!solved