T
T
tmtimuchin2021-01-16 21:12:12
C++ / C#
tmtimuchin, 2021-01-16 21:12:12

What is wrong with C++ code?

There is a simple C++ program where I find the scalar product of matrix vectors under some conditions. Here is the code:

#include <iostream>

using namespace std;

int main() {
    // Задаю матрицу
    float a[3][3] = {{1,2,3},
                    {4,5,6},
                    {6,0,4}};
    int min = a[0][0];
    int imin = 0;
    int jmin = 0;
    
    for (int j = 0; j < 3; j++) {
        cout << a[0][j] << " | ";
    }
    cout << "\n";
    for (int j = 0; j < 3; j++) {
        cout << a[1][j] << " | ";
    }
    cout << "\n";
    for (int j = 0; j < 3; j++) {
        cout << a[2][j] << " | ";
    }
    
    // Ищу минимальный элемент и его индексы
    for (int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            if (min > a[i][j]) {
                imin = i;
                jmin = j;
            }
        }
    }
        
    cout << "\nimin: " << imin+1 << endl;
    cout << "jmin: " << jmin+1 << endl;
    
    
    // Беру нужные векторы
    int ai[3];
    int aj[3];
    
    cout << "Строка с минимальным элементом: ";
    for (int j = 0; j < 3; j++) {
        ai[j] = a[imin][j];
    }
    for (int j = 0; j < 3; j++) {
        cout << ai[j] << " ";
    }
    
    cout << "\nСтолбец с минимальным элементом: ";
    for (int i = 0; i < 3; i++) {
        aj[i] = a[i][jmin];
    }
    for (int i = 0; i < 3; i++) {
        cout << aj[i] << " ";
    }
    
    
    //Скалярное произведение
    float scal = 0;
    
    for (int i = 0; i < 3; i++) {
        scal += ai[i] * aj[i];
    }
    cout << "\nСкалярное произведение: " << scal;
    
    return 0;
}


The program is fully working, everything is in order with it. I'm trying to rewrite it in a functional paradigm - that's where the difficulties arise:
#include <iostream>

using namespace std;

float fobr(float *af, int nf);

int main() {
    // Задаю матрицу
    int n = 3;
    float a[n][n] = {{1,2,3},
                    {4,5,6},
                    {6,0,4}};
    float sc;
    
    for (int j = 0; j < n; j++) {
        cout << a[0][j] << " | ";
    }
    cout << "\n";
    for (int j = 0; j < n; j++) {
        cout << a[1][j] << " | ";
    }
    cout << "\n";
    for (int j = 0; j < n; j++) {
        cout << a[2][j] << " | ";
    }
    
    sc = fobr(a,n);
}

//---------------------------------------------------------------------------

float fobr(float *af, int nf) {
    int min = af[0][0];
    int imin = 0;
    int jmin = 0;
    
    // Ищу минимальный элемент и его индексы
    for (int i = 0; i < nf; i++) {
        for(int j = 0; j < nf; j++) {
            if (min > af[i][j]) {
                imin = i;
                jmin = j;
            }
        }
    }
        
    cout << "\nМинимальный элемент находится в строке " << imin+1 << endl;
    cout << "Минимальный элемент находится в столбце " << jmin+1 << endl;
    
    
    // Беру нужные векторы
    int ai[nf];
    int aj[nf];
    
    cout << "Строка с минимальным элементом: ";
    for (int j = 0; j < nf; j++) {
        ai[j] = af[imin][j];
    }
    for (int j = 0; j < nf; j++) {
        cout << ai[j] << " ";
    }
    
    cout << "\nСтолбец с минимальным элементом: ";
    for (int i = 0; i < nf; i++) {
        aj[i] = af[i][jmin];
    }
    for (int i = 0; i < nf; i++) {
        cout << aj[i] << " ";
    }
    
    
    //Скалярное произведение
    float scal = 0;
    
    for (int i = 0; i < nf; i++) {
        scal += ai[i] * aj[i];
    }
    
    cout << "\nСкалярное произведение: " << scal;
    
    return scal;
}


When run, it gives errors "main.cpp:27:18: error: cannot convert 'float (*)[n]' to 'float*' for argument '1' to 'float fobr(float*, int)' sc = fobr (a,n);" and "main.cpp:33:22: error: invalid types 'float[int]' for array subscript int min = af[0][0];".
I changed float to int, it gives the same result. Apparently the problem is with the matrix. Please help find and fix the error.

PS Before that, I just rewrote another program in the functional paradigm, where there was work with three one-dimensional arrays, everything works there. I wrote it exactly EXACTLY the same way as this one, only the variables are different. I'll leave it as an example in the comments.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-01-16
@tmtimuchin

float fobr(float **af, int nf) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question