samedi 27 juin 2015

Printing whats inside the array

import java.io.*;
public class redtry4 {
    public static void main(String[]args)throws IOException{
        BufferedReader IN = new BufferedReader(new InputStreamReader(System.in));
        int[]numx = new int[10];
        System.out.println("Enter 10 different numbers:");
        for(int b=0; b<10; b++)
        {
            System.out.print("Number"+(b+1)+":");
            numx[b]=Integer.parseInt(IN.readLine());


        }
        System.out.print("Accepted numbers are:"+"\n");

    }
}

This is a working code. I wonder how would your print all the numbers youve inputed inside the array like for example:

Accepted numbers are: 1 2 3 4 5 6 7 8 9 12

and can i have ideas on how can i only put numbers that are not the same when inputed.

thanks!

C Program crashes when accessing specific array element of struct field

I have this code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct game_s {
  bool isOccupied[20][10];
};

int main() {
  struct game_s* game_p;
  game_p->isOccupied[0][8] = false;
  printf("0,8 works\n");
  game_p->isOccupied[2][8] = false;
  printf("2,8 works\n");
  game_p->isOccupied[1][7] = false;
  printf("1,7 works\n");
  game_p->isOccupied[1][9] = false;
  printf("1,9 works\n");
  game_p->isOccupied[1][8] = false; // crashes the program
  printf("1,8??");
}

As you can see from the comment, the program crashes when I try to access a specific element of the array. (More specifically, Windows tells me that "a.exe has stopped working" with the attached information.) If I use something other than 10 for the second dimension, the element will be another one. If I don't use a struct, it doesn't crash. If I use int instead of bool, it doesn't crash. If I make a variable of the struct instead of a pointer, it doesn't crash.

I'm compiling this with gcc main.c on Windows. If I use ideone, it runs without a problem.

Can someone tell me what's going on here?


Additional information Windows provides about the crash:

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: a.exe
  Application Version:  0.0.0.0
  Application Timestamp:    558f50c8
  Fault Module Name:    KERNELBASE.dll
  Fault Module Version: 6.1.7600.17206
  Fault Module Timestamp:   50e6605e
  Exception Code:   c0000005
  Exception Offset: 00011bcd
  OS Version:   6.1.7600.2.0.0.768.3
  Locale ID:    1031
  Additional Information 1: 0a9e
  Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
  Additional Information 3: 0a9e
  Additional Information 4: 0a9e372d3b4ad19135b953a78882e789

Read our privacy statement online:
  http://ift.tt/WDU7ZV

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

N-dimensional std::vector to N-dimensional C-array

How do you convert a N-dimensional std::vector to the corresponding N-dimensional C-array?

E.g. std::vector<std::vector<int>> to int arr[n][m], where n is the dimension of the outer vector and m of the inner vector?

Memory corrupted across single-line function call?

I am getting a seg fault from some code which accesses an array consisting of strings. The odd thing is I lose the memory across a one-line function call.

So this is my code:

class A {

    void method1(){

        std::cout << _myArray[ID] << std::endl;               //This outputs fine
        _pointerToObjectB->method2(ID, side);
    }

    std::array<std::string, 30000> _myArray;
    B* _pointerToObjectB;
};

In a different class:

class B {

    void method2(const int16_t ID, const int8_t Side) {
        std::cout << _objectA._myArray[ID] << std::endl;          //Seg fault
    }

    A _objectA;
};

GDB reports a seg fault, backtrace:

#0  0x00007ffff05cd81c in std::string::_M_data() const () from /debug/bin/../lib/libstdc++.so.6
#1  0x00007ffff05cd85a in std::string::_M_rep() const () from /debug/bin/../lib/libstdc++.so.6
#2  0x00007ffff05cdeb8 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /debug/bin/../lib/libstdc++.so.6
#3  0x00007fffe32f8bb1 in B<(short)-2, (short)1, (short)30000>::method2 (this=0x0, ID=362, Side=0 '\000')
#4  0x00007fffe32eafdf in A<(short)-2, (short)1, (short)30000>::method1 (this=0x2754400, ID=362, Side=0 '\000')

The pointer must be fine as it was able to invoke method2(). What else could be wrong?!

Its as if _objectA has been deleted across the call?

This seg fault is consistent, it happens every time I run the program. I'm at a bit of a loss what to do next.

Convert Python string to array in JavaScript

I have a JavaScript script which receives information from a Python server. The Python server outputs a list, which is converted to a string prior to being sent to the JavaScript script.

I'd like to be able to convert the received string into a form that can be indexed with JavaScript. Here's an example output string from the Python server:

var Message = [['Word1A', 'Word1B'], ['Word2A', 'Word2B'], ['Word3A', 'Word3B']];

Given the above example, I'd like to be able to query the received string as as an indexed array:

var x;
for (x in Message) {
    alert(x[0]};

The above example should return:

Word1A
Word2A
Word3A

What's the best way to go about this?

Overwritting null character in C array

Consider the case:

char s1[] = "abc";
s1[3] = 'x';
printf("%s", s1);

As I know, printf prints characters until it finds the null character and then stops.

When I overwrite the null character by 'x', why does printf print the s1 array correctly? How does it find the null character?

php array_filter dynamic filters from json

I got the two following arrays as input data:

$filters = [ { "Key": "ao", "Value": "5", "FilterComperator": ">=", "FilterOperator": " && " }, { "Key": "name", "Value": "Joe", "FilterComperator": "<>", "FilterOperator": " && " }, { "Key": "ao", "MySQLOP": "<=", "Value": "10", "FilterOperator": " && " } ]

$arr = [ { "ao": 13 }, { "ao": 10 }, { "ao": 6 } ]

What I am trying to achieve is use the filters from $filters array so I can filter $arr without using php eval

return array_filter($arr, function($k){
   return $k->ao >= '5' && $k->name <> 'Joe' && $k->ao <= '10';
});

Is there any suggestion? perhaps I could use create_function() instead or anything else that could do the job.