How to fill x[i] from array[8][8]?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    srand (time(NULL));
    
    int pole[8][8] = {0};
    pole[1][2] = 1;
    pole[2][2] = 1;
    pole[3][2] = 1;
    pole[4][2] = 1;
    pole[4][3] = 1;
    pole[4][4] = 1;
    pole[3][4] = 1;
    pole[5][4] = 1;
    pole[4][5] = 1;
    pole[6][4] = 1;
	for (int nRow=0; nRow < 8; nRow++)
    {
        for (int nCol = 0; nCol < 8; nCol++)
        cout << pole[nRow][nCol] << " ";
        cout << endl;
    }

    double activation = 0;
    for (int i=0; i<64; i++)
    {
        double x[i],w[i];
        activation += x[i] * w[i];
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


I want to fill x[i] with numbers from my array. Like x[10] will be assigned with number 1. Thanks.
Got it. But can it be written like this?

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    srand (time(NULL));
    
    int pole[8][8] = {0};
    pole[1][2] = 1;
    pole[2][2] = 1;
    pole[3][2] = 1;
    pole[4][2] = 1;
    pole[4][3] = 1;
    pole[4][4] = 1;
    pole[3][4] = 1;
    pole[5][4] = 1;
    pole[4][5] = 1;
    pole[6][4] = 1;
	for (int nRow=0; nRow < 8; nRow++)
    {
        for (int nCol = 0; nCol < 8; nCol++)
        cout << pole[nRow][nCol] << " ";
        cout << endl;
    }

    double activation = 0;
    for (int i=0; i<64; i++)
    {
        double x[i],w[i];
        for (int nRow=0; nRow < 8; nRow++)
        {
            for (int nCol = 0; nCol < 8; nCol++)
            x[i] = pole[nRow][nCol];
        }
        activation += x[i] * w[i];
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Or is there any better way? °¸°
It seems that you are programming a neural network? Looks strange though.

Anyway, check out the STL algorithms for_each() and generate(). See http://www.cplusplus.com/reference/algorithm/. I would use generate() to initialize the pole matrix, and then I would use for_each() to calculate x[] and activation.
Thanks for reply. But I do not understand how to write it to generate 8×8 cells. And you're right. It will be a simple neural network, which will recognize number 4 from that. If it'll be 4, then it will return 1. If not = 0. Every cell represent one input.

00000000
00100000
00100000
00101000
00111100
00001000
00001000
00000000
Last edited on
Are you just trying to use for loops to convert between 1D and 2D arrays?

You can calculate the indicies with something like this:

1
2
3
4
5
6
// let C represent the row-size of the 2D array
for( int y = 0; y < v2d.size(); ++y ) {
    for( int x = 0; x < C; ++x ) {
        v1d[y * C + x] = v2d[y][x];
    }
}

Or (going the other way):

1
2
3
for( int i = 0; i < v1d.size(); ++i ) {
    v2d[i / C][i % C] = v1d[i];
}

Topic archived. No new replies allowed.