Adding two arrays? - c++

In the Arduino IDE, I'd like to add the contents of two existing arrays like this:
#define L0 { {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0} }
#define L1 { {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0} }
should become
int myarray[3][4] = { {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 0} }
How would I go about this?
Thanks!

Thy this;
const int a[3][4] = { {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0} };
const int b[3][4] = { {0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0} };
int c[3][4];
const int* pa = &a[0][0];
const int* pb = &b[0][0];
int* pc = &c[0][0];
for(int i = 0; i < 3 * 4; ++i)
{
*(pc + i) = *(pa + i) + *(pb + i);
}

I think you are confused about how to go access the arrays L0 and L1 since they are defined as macros. Just assign them to arrays since the preprocessor will simply replace them:
int l[][4]=L0;
int m[][4]=L1;
Preprocessor will replace L0 and L1 with their values and compiler will only see them as:
int l[][4]={ {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 2, 0, 0} };
int m[][4]={ {0, 0, 0, 5}, {0, 0, 0, 6}, {0, 0, 7, 0} };
Now, you can use l & m to access the elements of array. Should easy enough from here to add two arrays :)

Related

Moving triangles to a new column with triangle strip

I'm having problems moving triangles to another column. I'm able to successfully add them to right direction (in the picture), but when I want to move them into a new column it takes points from old points (furthest on the right) and I would like to have them connected to the upper triangles as I elevate the mesh, the strip will make the mesh rise.
// triangles without a new column
std::vector<triangle_vertices_t> vertices = {
{ {0, 0, 0}, {1, 0, 0}, {0, 0, 1} },
{ {1, 0, 0}, {0, 0, 1}, {1, 0, 1} },
{ {0, 0, 0 + 1}, {1, 0, 0 + 1}, {0, 0, 1 + 1} },
{ {1, 0, 0 + 1}, {0, 0, 1 + 1}, {1, 0, 1 + 1} }
};
// vertices with a new column
std::vector<triangle_vertices_t> vertices = {
{ {0, 0, 0}, {1, 0, 0}, {0, 0, 1} },
{ {1, 0, 0}, {0, 0, 1}, {1, 0, 1} },
{ {0, 0, 0 + 1}, {1, 0, 0 + 1}, {0, 0, 1 + 1} },
{ {1, 0, 0 + 1}, {0, 0, 1 + 1}, {1, 0, 1 + 1} },
{ {1 - 1, 0, 0}, {1 - 1, 0, 1}, {0 - 1, 0, 1} },
{ {-1, 0, 0}, {1 - 1, 0, 0}, {-1, 0, 1} },
};
//drawing the triangles
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3 * amount_of_triangles);
glBindVertexArray(0);
So my question is, how can I add a column while connecting to the upper triangles?
On possibility is to use Primitive Restart inidces.
Another simple trick is to add the last vertex and the first vertex of a strip twice:
A C E G
+---- +-----+-----+
| / | / | / |
| / | / | / |
B +-----+-----+-----+
| / | D F H
| / |
+-----+
I J
Triangle strip:
A - B - C - D - E - F - G - H - H - B - B - I - D - J
This works because with a triangular primitive with 2 identical points nothing at all is rasterized. Hence the "triangles" G - H - H, H - H - B, H - B - B and B - B - I do not generate any fragments.

I want to know how to add names to the edges? I'm new with Dijkstra algorithm

I'm suppose to to give the shortest path from one place to another. I'm totally new with this, so I'm starting small. Just like 10 intersections around my place. However, I got how to print out the path from one place to another. I'm trying to figure out how to assign the names to this edges. And I don't know if using adjacency matrix is the best option. What if I have more than 100 nodes? How can I do it?
// A C++ program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
#include <iostream>
#include <limits.h>
#include <iomanip>
using namespace std;
// Number of vertices in the graph
#define V 14
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printPath(int parent[], int j)
{
// Base Case : If j is source
if (parent[j] == -1)
return;
printPath(parent, parent[j]);
cout << " --> " << j;
}
// A utility function to print the constructed distance array
void printSolution(int dist[], int n, int parent[])
{
printPath(parent, n);
cout << "\nThe distance from Source to Destination is: "<< dist[n] << endl;
}
// Function that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src, int destination)
{
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest path tree or shortest distance from src to i is finalized
int parent[V]; // Parent array to store shortest path tree
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++) {
dist[i] = INT_MAX;
sptSet[i] = false;
parent[i] = -1;
}
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++)
{
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in the first iteration.
int u = minDistance(dist, sptSet);
if (u == destination)
break;
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
parent[v] = u;
dist[v] = dist[u] + graph[u][v];
}
}
// print the constructed distance array
cout << "From " << src;
printSolution(dist, destination, parent);
}
// driver program to test above function
int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = { {0, 5, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0},
{5, 0, 6, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0},
{0, 6, 0, 3, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 4, 0, 0, 0, 0, 0, 0, 0, 7},
{0, 0, 7, 0, 4, 0, 4, 0, 0, 0, 0, 0, 6, 0},
{0, 8, 0, 0, 0, 4, 0, 4, 0, 0, 0, 5, 0, 0},
{0, 0, 0, 0, 0, 0, 4, 0, 3, 0, 4, 0, 0, 0},
{8, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 4, 0, 3, 0, 4, 0, 0},
{0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 4, 0, 4, 0},
{0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 4, 0, 4},
{0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 4, 0}
};
dijkstra(graph, 0, 13);
return 0;
}

Accessing Lua global tables with C++

How can I access a global table that already exists in Lua using C++ ?
Below are code which I tried. I tried creating a global variable and try modifying a that local to the local in Lua but things dont seem to work
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
// lua_createtable(lua_state, 0, 81);
// for (int i = 1; i <= 81; i++)
// {
// lua_pushnumber(lua_state, i);
// lua_pushnumber(lua_state, grid_[i - 1]);
// lua_settable(lua_state, -3);
// }
//
// lua_setglobal(lua_state, "arg");
// lua_createtable(lua_state, 81, 1);
//
// for (int i = 1; i <= 81; i++)
// {
// lua_pushnumber(lua_state, i);
// lua_pushnumber(lua_state, grid_[i - 1]);
// lua_settable(lua_state, -3);
// }
// lua_setglobal(lua_state, "arg");
luaL_loadfile(lua_state, "main.lua");
lua_call(lua_state, 0, 0);
int t = 2;
/* table is in the stack at index 't' */
lua_pushnil(lua_state); /* first key */
while (lua_next(lua_state, t) != 0) {
/* uses 'key' (at index -2) and 'value' (at index -1) */
printf("%s - %s\n",
lua_typename(lua_state, lua_type(lua_state, -2)),
lua_typename(lua_state, lua_type(lua_state, -1)));
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(lua_state, 1);
}
Lua
problem =
{
{9, 0, 0, 1, 0, 0, 0, 0, 5},
{0, 0, 5, 0, 9, 0, 2, 0, 1},
{8, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 2, 6, 0, 0, 9},
{2, 0, 0, 3, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 0, 9, 0, 0},
{0, 0, 1, 9, 0, 4, 5, 7, 0},
}
Update 1
int main()
{
lua_State *lua_state = luaL_newstate();
luaL_openlibs(lua_state);
luaL_loadfile(lua_state, "main.lua");
lua_getglobal(lua_state, "problem");
//lua_pushglobaltable(lua_state); // Get global table
lua_pushnil(lua_state); // put a nil key on stack
while (lua_next(lua_state, -2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
std::string name = lua_tostring(lua_state, -2); // Get key(-2) name
std::cout << name << std::endl;
lua_pop(lua_state, 1); // remove value(-1), now key on top at(-1)
}
lua_pop(lua_state, 1); // remove global table(-1)
lua_call(lua_state, 0, 0);
return 0;
}
Lua
problem =
{
{9, 0, 0, 1, 0, 0, 0, 0, 5},
{0, 0, 5, 0, 9, 0, 2, 0, 1},
{8, 0, 0, 0, 4, 0, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 2, 6, 0, 0, 9},
{2, 0, 0, 3, 0, 0, 0, 0, 6},
{0, 0, 0, 2, 0, 0, 9, 0, 0},
{0, 0, 1, 9, 0, 4, 5, 7, 0},
}
print("Lua Works")
user_input = io.read();
You don't have any values to iterate on Lua stack.
That int t=2; doesn't reflect anything, and your script doesn't return values to be left on stack.
See PIL book: 25.1 – Table Manipulation for examples on accessing global table.

Assigning to an array from an initializer list [duplicate]

This question already has answers here:
Error: Assigning to an array from an initializer list
(2 answers)
Closed 9 years ago.
I've checked on SO already for a simple way to fix this error. I didn't get this when compiling on another computer but suddenly now it's not compiling on my PC. Here's the error I'm getting:
Error: Assigning to an array from an initializer list
And here's the code:
int maze[12][12];
void print(bool playing);
int main()
{
printMaze(false);
playGame();
return 0;
}
void print(bool playing)
{
if (!playing) maze = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
}
It might also be worth mentioning that I get a warning on the same line:
Warning: Extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
I know that clearly means I have to use one of these two to use extended initializer lists, but have no idea what to do to resolve the matter.
Edit:
Having g++ follow the C++11 ISO C++ language standard in the settings removes the warning, but not the error.
What do your compilations steps look like? The warning is fairly clear: you are trying to use a feature that requires -std=c++11 or -std=gnu++11, and although that is apparently enabled by default, it is possible that you have overridden it (i.e. explicitly turned it off) somehow. You should examine your compilation process closer and make sure you aren't preventing that feature from being allowed.
A workaround is to use the old-style C function memcpy. This will work with older compilers.
int maze[12][12];
void printMaze(bool playing);
int main()
{
printMaze(false);
playGame();
return 0;
}
void printMaze(bool playing)
{
static int maze1[12][12] = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{2, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 3},
{1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
if (!playing) memcpy(maze, maze1, 12*12*sizeof(int));
}

Checking whether a number exist in the same row or column

I have been staring at this code for few hours, tried walkthrough,debugging with autos and breakpoints and it's no solution so far. Maybie someone's fresh look would help me ;) .
#include <iostream>
using namespace std;
int matrix[9][9] = {{0, 0, 6, 0, 0, 0, 1, 0, 5},
{0, 4, 0, 7, 0, 6, 0, 3, 9},
{2, 0, 0, 9, 3, 0, 6, 0, 0},
{7, 0, 0, 1, 8, 0, 5, 0, 4},
{0, 0, 4, 0, 6, 0, 9, 0, 0},
{1, 0, 9, 0, 5, 2, 0, 0, 3},
{0, 0, 1, 0, 9, 3, 0, 0, 7},
{6, 7, 0, 5, 0, 8, 0, 9, 0},
{9, 0, 8, 0, 0, 0, 4, 0, 0}};
bool check(int column ,int row,int checkedValue)
{
//column check
for(int i=0; i<9; i++)
{
if(i==row)continue;
if(checkedValue==matrix[column][i]) return false;
}
//row check
for(int i=0; i<9; i++)
{
if(i==column) continue;
if(checkedValue==matrix[i][row]) return false;
}
return true;
}
int main()
{
cout<<check(4,0,4); //Why does it output 0? There is no "4" in the 5th column and the 1st row.
system("pause");
return 0;
}
The function check(column,row,value) was designed to return 0 when number occurs at least once in the "matrix" two dimensional table. This program is a chunk of sudoku solver.
You mixed the indices up in the if statements. They should be:
if(checkedValue==matrix[i][column]) return false; // not matrix[column][i]
and
if(checkedValue==matrix[row][i]) return false; // not matrix[i][row]
The reason is that the first dimension is the row. You can check this by printing matrix[2][0].
For your matrix, you will get 2 (and not 6).