Small square button in GTK+ - c++

I need to populate GtkGrid with many buttons, but they are too large and cannot fit on the screen. How can I make it 10px * 10px? set_size_request does not work in this case.
for(int i{0}; i < 128; i++){
for(int j{0}; j < 8; j++){
paint_btn_arr[i][j] = gtk_button_new();
}
}
up_grid = gtk_grid_new();
down_grid = gtk_grid_new();
for(int i{0}; i < 64; i++){
for(int j{0}; j < 8; j++){
gtk_grid_attach(GTK_GRID(up_grid), paint_btn_arr[i][j], i, j, 1, 1);
}
}
for(int i{0}; i < 64; i++){
for(int j{0}; j < 8; j++){
gtk_grid_attach(GTK_GRID(down_grid), paint_btn_arr[i + 64][j], i, j, 1, 1);
}
}

Related

How to correctly divide square matrix?

const int n = 4, m = 4;
int i, j, k, sum;
srand(time(NULL));
int mat[n][m];
printf("Matrix( %d, %d): \n",n,m);
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
mat[i][j] = rand()%100-50;
printf("%4d", mat[i][j]);
}
cout<<endl;
}
for(i = 0; i < n/2; i++)
{
for(j = 0; j < m/2; j++)
{
mat[i][j] = 0;
}
}
How to divide square matrix on 4 equal square blocks? My code divide only 1st part. I think I need to use n/2 < n, but how to do it in cycle?
First block:
for (int i=0; i<n/2; i++) {
for(int j=0; j<m/2; j++) {
// your code
}
}
Second block:
for (int i=0; i<n/2; i++) {
for(int j=m/2; j<m; j++) {
// your code
}
}
Third block:
for (int i=n/2; i<n; i++) {
for(int j=0; j<m/2; j++) {
// your code
}
}
Forth block:
for (int i=n/2; i<n; i++) {
for(int j=m/2; j<m; j++) {
// your code
}
}
Here is a more compact code that can fetch your matrix blocks by rows sequentially:
for (int p = 0; p <= 1, p++)
{
for (int q = 0; q <= 1, q++)
{
for(i = p*n/2; i < (p+1)*n/2; i++)
{
for (j = q*m/2; j < (q+1)*m/2; j++)
{
mat[i][j] = 0;
}
}
}
}

Multi Dimensional vectors

Require a 2D-vector with a pair(int, int) as elements.The following code gives SIGSEGV on running.How can it be resolved ?
int main()
{
vector< vector<pair<int, int> > >v;
//vector< vector<pair<int, int> > >v(3), problem is resolved, but how ?
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
v[i].push_back(make_pair(i, j));
for(int i = 0; i < 3; ++i)
{
cout<<"\n";
for(int j = 0; j < 3; ++j)
cout<<"{"<<v[i][j].first<<", "<<v[i][j].second<<"} ";
}
return 0;
}
In the beginning, v contains solely nothing, so the SIGSEGV if received at
v[0].push_back(make_pair(0, 0)); // First loop
If you initialize v with a length of 3, then v[0] is a valid statement and won't cause a segmentation fault.
The following code should work if you don't initialize v with a size.
for(int i = 0; i < 3; ++i){
vector<pair<int,int> > t;
for(int j = 0; j < 3; ++j)
t.push_back(make_pair(i, j));
v.push_back(std::move(t));
}
Thanks to Zereges for code improvement
You are inserting wrong.
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
v[i].push_back(make_pair(i, j));
Change your code of insertion like below to work properly:
for(int i = 0; i < 3; ++i)
{
vector<pair<int, int>> vctr;
for(int j = 0; j < 3; ++j)
{
vctr.push_back(make_pair(i, j));
}
v.push_back(vctr);
}
create a vector of pair, lets say: (vctr), then insert pair<i,j> in (vctr).
and then insert vctr to vector (v).

how to improve performance of 2d array in C++

I have a low-level function that will be called millions of times, so it should be very efficient. When I use "gprof" in Linux, I found that a part of the code takes 60% of the total computation of the function (the rest part is to solve the roots of a cubic equation). Here Point is a data structure has x and v, which will be converted to a matrix for later use. The idea is to subtract each row by the first row. The code shows like below
double x[4][3] = {0}, v[4][3] = {0};
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = Point[i]->v[j];
x[i][j] = Point[i]->x[j];
}
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = v[0][j] - v[i][j];
x[i][j] = x[0][j] - x[i][j];
}
}
Can anyone show me the problem of this code? Why it performs so badly?
You can do it all in one pass:
double x[4][3] = {
{ Point[0]->x[0], Point[0]->x[1], Point[0]->x[2] }
};
double v[4][3] = {
{ Point[0]->v[0], Point[0]->v[1], Point[0]->v[2] }
};
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] = x[0][j] - Point[i]->x[j];
v[i][j] = v[0][j] - Point[i]->v[j];
}
}
You could even take that to the next level and put the entire thing into the initializers for x and v.
Or, if x and v in Point are each contiguous arrays:
double x[4][3], v[4][3]; // no init
// fill entire arrays
for (int i = 0; i < 4; ++i){
memcpy(x[0], Point[0]->x, sizeof(x[0]));
memcpy(v[0], Point[0]->v, sizeof(v[0]));
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] -= Point[i]->x[j];
v[i][j] -= Point[i]->v[j];
}
}

std::thread undefined behaviour

Is it possible for the code bellow to produce undefined behaviour?
unsigned int total_threads = 10;
vector<thread> t(total_threads);
unsigned int *nums = (unsigned int*)calloc(total_threads, sizeof(int));
for(unsigned int i = 0; i < 1000; i++)
{
for(unsigned int j = 0; j < total_threads; j++)
t[j] = thread(func_, std::ref(nums[j]));
for(unsigned int j = 0; j < total_threads; j++)
t[j].join();
for(unsigned int j = 0; j < total_threads; j++)
{
cout << nums[j] << " ";
nums[j] = 0;
}
}
Yes, because calloc may fail. Check for the return value or use std::vector .

Infinite Impluse Response (IIR) Function

I am trying to design a signal class which includes an IIR filter function. The following is my code:
void signal::IIRFilter(vector<double> coefA, vector<double> coefB){
double ** temp;
temp = new double*[_nchannels];
for(int i = 0; i < _nchannels; i++){
temp[i] = new double[_ninstances];
}
for(int i = 0; i < _nchannels; i++){
for(int j = 0; j < _ninstances; j++){
temp[i][j] = 0;
}
}
for(int i = 0; i < _nchannels; i++){
for (int j = 0; j < _ninstances; j++){
int sum1 = 0;
int sum2 = 0;
for(int k = 0; k < coefA.size(); k++){
if ((j-k) > 0 ){
sum1 += coefA.at(k)*temp[i][j-k-1];
}
}
for (int m = 0; m < coefB.size(); m++){
if(j >= m){
sum2 += coefB.at(m)*_data[i][j-m];
}
}
temp[i][j] = sum2-sum1;
}
}
for(int i = 0; i < _nchannels; i++){
for(int j = 0; j < _ninstances; j++){
_data[i][j] = temp[i][j];
}
}
}
_data contains my original signal, _ninstances is my number of samples, and _nchannels is the number of channels. The function compiles and works but the result I am getting is different from the result given by MATLAB. I even use the same coefficients given by MATLAB. Is there anything that I'm doing wrong in my function?
One issue that I can see is that you are declaring sum1 and sum2 as integers when they should be double. To avoid this kind of error in the future, you should try configuring your compiler to warn of implicit conversions. In g++, this is accomplished using the -Wconversion flag.