vendredi 31 juillet 2015

Why we have used pointer in this C program?

Why we have used pointer in this C code? Basically, I am just searching a string in the array, but without pointer, it is unable to run. But why is that?

int main() {

    char *x[] = {"ab", "bc", "cd", 0};

    char *s = "ab";

    int i = 0;

    while(x[i]) {

            if(strcmp(x[i], s) == 0) {
                    printf("Gotcha!\n");
                    break;
            }

            i++;
    }
}

numpy - meshgrid for multiple dimensions

numpy has a beautiful function which generate multidimensional grid. It is easy to work with it when number of dimension is low and is known in advance, but what to do when number of dimension is only known at time of execution or simply big and it takes too long to type. I guess I am looking for something like

 import numpy as np

 x = np.meshgrid(y)

where y is an array of arrays of evaluation points, for example

y = [array([-3.,  0.,  3.]) array([-3.,  0.,  3.]) array([-3.,  0.,  3.])]

Suggestions?

MongoDB find by ids array on params order

I have a dataset, such as:

   {
      "_id" : ObjectId("559a9b9c9d9e9f9g1"),
      "title": "First"
    },
    {
      "_id" : ObjectId("559a9b9c9d9e9f9g2"),
      "title": "Second" 
    },
    {
      "_id" : ObjectId("559a9b9c9d9e9f9g3"),
      "title": "Third"
    },
    {
      "_id" : ObjectId("559a9b9c9d9e9f9g4"),
      "title": "Fourth"
    }  

When I try to find with an array of items, like:

db.snippets.find( { _id: { $in: [ObjectId("559a9b9c9d9e9f9g4"), 
                                 ObjectId("559a9b9c9d9e9f9g1"), 
                                 ObjectId("559a9b9c9d9e9f9g3") ] } } )

Instead of returning the query result in the given array of ids order, it returns based on the persisted data order.

Expected:

    { "_id" : "559a9b9c9d9e9f9g4", ...}, (item 4)
    { "_id" : "559a9b9c9d9e9f9g1", ...}, (item 1)
    { "_id" : "559a9b9c9d9e9f9g3", ...}  (item 3)

Result:

    { "_id" : "559a9b9c9d9e9f9g1", ...}, (item 1)
    { "_id" : "559a9b9c9d9e9f9g3", ...}, (item 3)
    { "_id" : "559a9b9c9d9e9f9g4", ...}  (item 4)

Is there a way to force the returned query to follow the same given array order?

Printing after typecasting with %d or %i gives unexpected outputs

I am rounding off some values and then printing them. When I use %f option, they are printed correctly, but using the %d or %i option (even after casting the rounded values to int) is giving a weird output, and I am not able to figure the why of it out.

Any help is much appreciated!

When I use %f:

i = 0;

while(i < n_shapes)
{
    ll_x[i] = (int)round((ll_x[i] - min_x)/pitch_x);
    ll_y[i] = (int)round((ll_y[i] - min_y)/pitch_y);
    ur_x[i] = (int)round((ur_x[i] - min_x)/pitch_x);
    ur_y[i] = (int)round((ur_y[i] - min_y)/pitch_y);
    printf("%f,%f,%f,%f\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
    i++;
}

Output:

115.000000,94.000000,115.000000,101.000000
116.000000,51.000000,117.000000,58.000000
116.000000,60.000000,117.000000,67.000000
116.000000,69.000000,117.000000,75.000000
116.000000,77.000000,117.000000,84.000000
116.000000,86.000000,117.000000,93.000000
116.000000,94.000000,117.000000,101.000000

Now, with %d (or %i):

i = 0;

while(i < n_shapes)
{
    ll_x[i] = (int)round((ll_x[i] - min_x)/pitch_x);
    ll_y[i] = (int)round((ll_y[i] - min_y)/pitch_y);
    ur_x[i] = (int)round((ur_x[i] - min_x)/pitch_x);
    ur_y[i] = (int)round((ur_y[i] - min_y)/pitch_y);
    printf("%d,%d,%d,%d\n", ll_x[i], ll_y[i], ur_x[i], ur_y[i]);
    i++;
}

Output:

1079590912,0,6,-1
1078788096,0,5,-1
1079033856,0,6,-1
1079164928,0,6,-1
1079312384,0,6,-1
1079459840,0,6,-1
1079590912,0,6,-1

Thank you!

Edit: Yes, I realize that using (int) in the printf gives me the right output. I was curious about the values I got when I didn't do so. What does my output when I use %d without casting inside the printf mean?

Solving a simple matrix in row-reduced form in C++

Okay, I am pulling out all my hair on this one, though, as a noob, I am sure there are several problems. I want to take a matrix and, by sing elementary row operations, reduced it to row-reduced echelon form. We assume (1) it is solvable and (2) a unique solution. There is no checking for zeros or anything; it just does row operations. Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

void printmatrix(float A[][4]);
void RowReduce (float A[][4]);


int main()
{


     float A[3][4] = {{5, -6, -7, 7}, {3, -2, 5, -17}, {2, 4, -3, 29}}; //answer should be {2, 4, -3}

    printmatrix(A);
    RowReduce(A);
 }

 void printmatrix(float A[][4]){ // Outputs the matrix

int p=3;
int q=4;

 for (int i=0; i<p; i++)
    {
        for (int j=0; j<q; j++)
        {
            cout << A[i][j] << " ";
        }
        cout << endl;
    } 
}

void RowReduce (float A[][4]){
  int p=3;  //rows
  int q=4;  //columns

  int lead = 0; //the determines the column we are at which holds the diagonal, the basis for all elimination above and below

    cout << endl;
    while (lead<q-1)
    {
        for (int i=0; i<p; i++) //for each row . . .
        {
           if (i!=lead) // ignore the diagonal, and we will not have a tree rref as the diagonal will not be divided by itself. I can fix that.
                {
                    cout << A[lead][lead] << "  " << A[i][lead];
                    for (int j=0; j<q; j++)
                    {
                        A[i][j]=A[lead][lead]*A[i][j]; //here is the math . . . . probably where the problem is?
                        A[i][lead]=A[i][lead]*A[lead][j];
                        A[i][j]=A[i][j]-A[i][lead];
                    }
                cout << endl;
                }
    }
lead++;  // now go to the next pivot
cout << endl;

}

}

I tried doing it by hand, but what I get is, of course, the right answer, but this gets a diagonal matrix--which is great--but the wrong answer!

Accessing Public 2D Array Variables via Script in Unity

So I figured out how to make a 2D array that is viewable in the inspector via the code below.

[System.Serializable]
 public class QuestPhase {
     [HideInInspector]
     public string name;
     public int[] Phase = new int[5];
 }
 public QuestPhase[] questPhase    = new QuestPhase[5];

That works just fine, but I'm trying to figure out how to access that information via script and I can't figure it out.

 questVariable = questPhase[1,1];

That returns an error of "Expected 1 index, got 2", and just using one index gives me "Cannot convert type QuestPhase into type int." I'm sure the answer is obvious, but if anyone could answer it for me I would be most appreciative.

how are arrays actually created under the hood ? so if I I'm creating my own implementation?

I would actually like to understand the implementation of them if I was writing a raw class to emulate the behavior. I understand there is a reference variable that points to multiple objects, but not sure how to model that in code. Any language appreciated: please do not answer the question like you make an array as such : int []i. Thanks in adance