Error: Array bound not integer constant in Header - c

I have 2D arrays in header files, for which I declared the sizes of both:
int numPaths = 2;
int pathLength = 11;
double x[numPaths][pathLength] = {{62, 114, 0, 73, 55, 21, -28, -93, 0, 0, 0},{-90, 208, 0, 4, 7, 10, 12, 13, 11, -198, -147}};
double y[numPaths][pathLength] = {{55, 88, 0, -42, 12, 45, 54, 40, 0, 0, 0},{269, -117, 0, -10, -14, -17, -20, -24, -69, -82, 20}};
I get this error: Array bound not an integer constant.
My 2D arrays are not dynamically changes, and I've declared the sizes of these arrays (numPaths and pathLength). I'm not sure what the problem is?

numPaths and pathLength aren't constants, just like the error message says. You need:
#define numPaths 2
#define pathLength 11
Some compilers will let you get away with:
const int numPaths = 2;
const int pathLength = 11;
As an extension.

Related

How does the addbit bit manipulation function work in this permutation code sample

I have code to perform a permutation on a block of data (64 bit) and the code works but I don't understand how the addbit function works. This is the function that performs the permutation on a to and from bit position.
I understand that because if a bit gets over-written in the destination data block then if that previous bit needs to be permuted then it will be lost and that is why there is a source and destination data block.
But I dont understand the logic in addbit.
Why is FIRSTBIT used?
The code works, but I would like to understand why.
#include <stdint.h>
#include <stdio.h>
// FIRSTBIT is first bit in 64 bit data?
#define FIRSTBIT 0x8000000000000000 // 1000000000...
// eg move bit 64 in input data to bit position 1
// then move bit 63 in input data to bit position 2 etc
const int TestPermutation[64] = {
64, 63, 62, 61, 60, 59, 58, 57,
56, 55, 54, 53, 52, 51, 50, 49,
48, 47, 46, 45, 44, 43, 42, 41,
40, 39, 38, 37, 36, 35, 34, 33,
32, 31, 30, 29, 28, 27, 26, 25,
24, 23, 22, 21, 20, 19, 18, 17,
16, 15, 14, 13, 12, 11, 10, 9,
8, 7, 6, 5, 4, 3, 2, 1
};
// move data bit from 'from' at position_from to position_to in block
// How does this work?
void addbit(uint64_t *block, uint64_t from, int position_from, int position_to)
{
if (((from << (position_from)) & FIRSTBIT) != 0)
*block += (FIRSTBIT >> position_to);
}
// perform permutation based on TestPermutation array
void permute(uint64_t* data) {
uint64_t data_temp = 0;
for (int ii = 0; ii < 64; ii++)
{
addbit(&data_temp, *data, TestPermutation[ii] - 1, ii);
}
*data = data_temp;
}
void print_binary(uint64_t number) {
for (int i = sizeof(uint64_t) * 8 - 1; i >= 0; --i) {
printf("%c", ((number >> i) & 1) ? '1' : '0');
}
printf("\n");
}
int main() {
uint64_t data = 0xF0F0F0F0F0F0F0F0; // test block
print_binary(data); // 1111000011110000111100001111000011110000111100001111000011110000
permute(&data);
print_binary(data); // 0000111100001111000011110000111100001111000011110000111100001111
}

Merging two ascending arrays in CUDA with ascending order

I have two float arrays
a = {1, 0, 0, 22, 89, 100};
b = {2, 3, 5, 0, 77, 98};
Both are monotonically increasing; Both with same length; Both may/may not have 0s inside. What I am trying to get is the new array combing both arrays in ascending order but without 0s:
c = {1, 2, 3, 5, 22, 77, 89, 98, 100 };
I cannot figure out how to write in CUDA code, unless I do a serial for loop, which I am trying to avoid. Any suggestions? Thanks.
As Robert pointed out, thrust provides the basic building blocks for your needs.
merge.cu
#include <iostream>
#include <thrust/remove.h>
#include <thrust/merge.h>
int main()
{
float a[6] = {1, 0, 0, 22, 89, 100};
float b[6] = {2, 3, 5, 0, 77, 98};
float c[12];
thrust::merge(a,a+6,b,b+6,c);
float* newEnd = thrust::remove(c,c+12,0);
thrust::copy(c,newEnd, std::ostream_iterator<float>(std::cout, " "));
}
Compile and run:
nvcc -arch sm_20 merge.cu && ./a.out
Output:
1 2 3 5 22 77 89 98 100

Knapsack code - not working for some cases

My code is as the following:
#include<stdio.h>
int max(int a,int b)
{
return a>b?a:b;
}
int Knapsack(int items,int weight[],int value[],int maxWeight)
{
int dp[items+1][maxWeight+1];
/* dp[i][w] represents maximum value that can be attained if the maximum weight is w and
items are chosen from 1...i */
/* dp[0][w] = 0 for all w because we have chosen 0 items */
int iter,w;
for(iter=0;iter<=maxWeight;iter++)
{
dp[0][iter]=0;
}
/* dp[i][0] = 0 for all w because maximum weight we can take is 0 */
for(iter=0;iter<=items;iter++)
{
dp[iter][0]=0;
}
for(iter=1;iter<=items;iter++)
{
for(w=0;w<=maxWeight;w=w+1)
{
dp[iter][w] = dp[iter-1][w]; /* If I do not take this item */
if(w-weight[iter] >=0)
{
/* suppose if I take this item */
dp[iter][w] = max( (dp[iter][w]) , (dp[iter-1][w-weight[iter]])+value[iter]);
}
}
}
return dp[items][maxWeight];
}
int main()
{
int items=9;
int weight[/*items+1*/10]={60, 10, 20, 20, 20, 20, 10, 10, 10};
int value[/*items+1*/10]={73, 81, 86, 72, 90, 77, 85, 70, 87};
int iter;
int i;
int maxWeight=180;
for (i=0;i<10;i++){
value[i] = value[i]*weight[i];
}
printf("Max value attained can be %d\n",Knapsack(items,weight,value,maxWeight));
}
My knapsack code is working when
items=12;
int weight[/*items+1*/13]={60, 20, 20, 20, 10, 20, 10, 10, 10, 20, 20, 10};
int value[/*items+1*/13]={48, 77, 46, 82, 85, 43, 49, 73, 65, 48, 47, 51};
where it returned the correct output 7820.
But it doesn't returned the correct output when
items=9;
int weight[/*items+1*/10]={60, 10, 20, 20, 20, 20, 10, 10, 10};
int value[/*items+1*/10]={73, 81, 86, 72, 90, 77, 85, 70, 87};
where it returned the output 9730, the correct output should be 14110.
From observation, the program somehow skipped the 1st value (weight=60, value =73).
I have checked the code several times, but I just cant find what's wrong.
Can someone explain to me why? Thank you!
In your code, you are trying to access out of bounds index for weight and value array. When iter reaches value 9, weight[iter] and value[iter] becomes out of bounds index. I guess, in C you simply get some garbage value for out of index access, but in Java, that will throw an exception. Change the code in your inner for loop to:
dp[iter][w] = dp[iter-1][w]; /* If I do not take this item */
if(w-weight[iter - 1] >=0)
{
/* suppose if I take this item */
dp[iter][w] = maximum( (dp[iter][w]) , (dp[iter-1][w-weight[iter - 1]])+value[iter - 1]);
}
and it will work fine.
int weight[/*items+1*/10]={60, 10, 20, 20, 20, 20, 10, 10, 10};
int value[/*items+1*/10]={73, 81, 86, 72, 90, 77, 85, 70, 87};
Your arrays are of length 10, but you are filling only 9 entries. Hence the last entry gets filled to 0. How to initialize all members of an array to the same value?
int weight[/*items+1*/10]={60, 10, 20, 20, 20, 20, 10, 10, 10, 0};
int value[/*items+1*/10]={73, 81, 86, 72, 90, 77, 85, 70, 87, 0};
But you are trying to access the indices (1 to 9) in your algorithm.
Instead try filling all entries:
int weight[/*items+1*/10]={0, 60, 10, 20, 20, 20, 20, 10, 10, 10};
int value[/*items+1*/10]={0, 73, 81, 86, 72, 90, 77, 85, 70, 87};
EDIT:
The first case gives correct output since in that case the first entry is not included in the optimal solution.

How can I use Opencv SparseMatrix

I want to make a sparse matrix in OpenCV.
How can I do the basic operation for this matrix like:
Putting or accessing data from matrix elements.
Cheers
Using the C++ interface might be more appropriate. Notice that the example code in the documentation [1] lacks the modulo operation und thus fails.
const int dims = 2;
int size[] = {3, 20}; // rows and columns if in two dimensions
SparseMat sparse_mat(dims, size, CV_32F);
for(int i = 0; i < 1000; i++) {
// create a random index in dims dimensions
int idx[dims];
for(int k = 0; k < dims; k++)
idx[k] = rand() % size[k];
sparse_mat.ref<float>(idx) += 1.f;
}
cout << "bottom right element # (2,19) = " << sparse_mat.ref<float>(2,19) << "\n";
Mat dense;
sparse_mat.convertTo(dense, CV_32F);
cout << dense;
Gives the following output
bottom right element # (2,19) = 19
[9, 23, 13, 26, 18, 13, 18, 15, 13, 17, 13, 18, 19, 6, 20, 20, 12, 15, 15, 15;
17, 17, 14, 16, 12, 14, 17, 15, 15, 18, 24, 18, 13, 22, 18, 11, 18, 22, 17, 15;
19, 16, 14, 10, 18, 19, 10, 17, 18, 15, 24, 22, 18, 18, 18, 23, 21, 16, 14, 19]
[1] The OpenCV Reference Manual. Version 2.4.3. 2012, p. 46.
Sets and Sparse Matrices, page 23:
Sparse matrix in OpenCV uses CvSet for storing elements.
CvSparseMat* get_color_map( const IplImage* img )
{
int dims[] = { 256, 256, 256 };
CvSparseMat* cmap = cvCreateSparseMat(3, dims, CV_32SC1);
for( int i = 0; i < img->height; i++ ) for( int j = 0; j < img->width; j++ )
{
uchar* ptr=&CV_IMAGE_ELEM(img,uchar,i,j*3);
int idx[] = {ptr[0],ptr[1],ptr[2]};
((int*)cvPtrND(cmap,idx))[0]++;
}
// print the map
CvSparseMatIterator it;
for(CvSparseNode *node = cvInitSparseMatIterator( mat, &iterator );
node != 0; node = cvGetNextSparseNode( &iterator ))
{
int* idx = CV_NODE_IDX(cmap,node);
int count=*(int*)CV_NODE_VAL(cmap,idx);
printf( “(b=%d,g=%d,r=%d): %d\n”, idx[0], idx[1], idx[2], count );
}
return cmap;
}

multiplication of two numbers

Few days back I had an interview with Qualcomm. I was kinnda stucked to one question, the question thou looked very simple but neither me nor the interviewer were satisfied with my answers, if anyone can provide any good solution to this problem.
The question is:
Multiply 2 numbers without using any loops and additions and of course no multiplication and division.
To which I replied: recursion
He said anything else at very low level.
To which the genuine thought that came to my mind was bit shifting, but bit shifting will only multiply the number by power of 2 and for other numbers we finally have to do a addition.
For example: 10 * 7 can be done as: (binary of 7 ~~ 111)
10<< 2 + 10<<1 + 10
40 + 20 + 10 = 70
But again addition was not allowed.
Any thoughts on this issue guys.
Here is a solution just using lookup, addition and shifting. The lookup does not require multiplication as it is an array of pointers to another array - hence addition required to find the right array. Then using the second value you can repeat pointer arithmetic and get the lookup result.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/* Note:As this is an array of pointers to an array of values, addition is
only required for the lookup.
i.e.
First part: lookup + a value -> A pointer to an array
Second part - Add a value to the pointer to above pointer to get the value
*/
unsigned char lookup[16][16] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 },
{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45 },
{ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60 },
{ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75 },
{ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90 },
{ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105 },
{ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120 },
{ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135 },
{ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150 },
{ 0, 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132, 143, 154, 165 },
{ 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180 },
{ 0, 13, 26, 39, 52, 65, 78, 91, 104, 117, 130, 143, 156, 169, 182, 195 },
{ 0, 14, 28, 42, 56, 70, 84, 98, 112, 126, 140, 154, 168, 182, 196, 210 },
{ 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225 }
};
unsigned short answer, mult;
unsigned char x, y, a, b;
x = (unsigned char)atoi(argv[1]);
y = (unsigned char)atoi(argv[2]);
printf("Multiple %d by %d\n", x, y);
answer = 0;
/* First nibble of x, First nibble of y */
a = x & 0xf;
b = y & 0xf;
mult = lookup[a][b];
answer += mult;
printf("Looking up %d, %d get %d - Answer so far %d\n", a, b, mult, answer);
/* First nibble of x, Second nibble of y */
a = x & 0xf;
b = (y & 0xf0) >> 4;
mult = lookup[a][b];
answer += mult << 4;
printf("Looking up %d, %d get %d - Answer so far %d\n", a, b, mult, answer);
/* Second nibble of x, First nibble of y */
a = (x & 0xf0) >> 4;
b = y & 0xf;
mult = lookup[a][b];
answer += mult << 4;
printf("Looking up %d, %d get %d - Answer so far %d\n", a, b, mult, answer);
/* Second nibble of x, Second nibble of y */
a = (x & 0xf0) >> 4;
b = (y & 0xf0) >> 4;
mult = lookup[a][b];
answer += mult << 8;
printf("Looking up %d, %d get %d - Answer so far %d\n", a, b, mult, answer);
return 0;
}
Perhaps you could recursively add, using bitwise operations as a replacement for the addition operator. See: Adding Two Numbers With Bitwise and Shift Operators
You can separate your problems by first implementing the addition and then the multiplication based on the addition.
For the addition, implement what they do on processors at the gate level using the C bitwise operators:
http://en.wikipedia.org/wiki/Full_adder
Then for the multiplication, with the addition you implemented, use goto statements and labels so no loop statement (the for, while and do iteration statements) will be used.
What about russian peasant multiplication without using addition? Is there an easy way (a few lines, no loops) to simulate addition using only AND, OR, XOR and NOT?
You can implement addition by bits operators. But still, if you want to avoid loops, you should write a lot of code. (I used to implement multiplication without arithmetic operators, but I use loop, shifting the index until it became zero. If it can help you, tell me, and I will search the file)
You could use logarithms and subtraction instead.
log(a*b) = log(a) + log(b)
a+b = -(-a-b)
exp(log(a)) = a
round(exp(-(-log(a)-log(b))))
How about multiplication tables?
Question: Multiply 2 numbers without using any loops and additions and of course no multiplication and division.
Multiplication is defined in terms of addition. It is impossible not to find addition in an implementation of multiplication.
Arbitrary precision numbers cannot be multiplied without loop/recursion.
Multiplication of two numbers of fixed bit-lengths can be implemented via a table lookup. The problem is the size of the table. Generating the table requires addition.
The answer is: It cannot be done.

Resources