Related
I have a 2D extern array "the_board[17][25]". I am fairly new to C and believe I have some linking issue. in my board.c file I initialized the board, giving every cell a value. when calling the board in my other functions, the values change, even though I have made no edits to the array since initializing it.
header file to declared array:
#define BOARD_SIZE_X 25
#define BOARD_SIZE_Y 17
extern int the_board[BOARD_SIZE_Y][BOARD_SIZE_X];
#define DA 0 /* disallowed */
#define RE 1 /* red */
#define GR 2 /* green */
#define EM 7 /* empty */
board.c file containing initialization:
#include "my_header.h"
int the_board[BOARD_SIZE_Y][BOARD_SIZE_X] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7},
{0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0},
{0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0},
{0, 0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0, 0},
{0, 0, 0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0, 0, 0},
{0, 0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0, 0},
{0, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 0},
{0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0},
{7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
Sorry for the large block of code it has to be manually defined. This is where the problem I run into. When calling the_board[][], the values become screwed up. For an example this is what happens when I print the_board[BOARD_SIZE_Y][BOARD_SIZE_X] in main.c:
img of array: https://i.stack.imgur.com/KLQqT.png
#include "my_header.h"
int main() {
int i, j;
int the_board[BOARD_SIZE_Y][BOARD_SIZE_X];
for (i = 0; i < BOARD_SIZE_Y; i++){
for (j = 0; j < BOARD_SIZE_X; j++) {
printf(" %d ", the_board[i][j]);
}
}
return 0;
}
You have one initialised global the_board, defined in board.c.
You have one non-initialised local the_board defined inside main().
That second one hides the first one.
Delete the line which defines it, i.e.
int the_board[BOARD_SIZE_Y][BOARD_SIZE_X];
I'm trying to wrap an io.ReaderCloser, which in production would come from a request handler, with a custom reader that can be passed into a JSON decoder.
I created the below
import (
"io"
)
// RemoveNull is a stream wrapper that should remove null bytes from the byte stream
type RemoveNull struct {
Reader io.ReadCloser
}
// NewRemoveNullStream creates a new RemoveNull reader which passes the stream through a null check first
func NewRemoveNullStream(reader io.ReadCloser) RemoveNull {
return RemoveNull{
Reader: reader,
}
}
// Read wraps a Reader to remove null bytes in the stream
func (null RemoveNull) Read(p []byte) (n int, err error) {
n, err = null.Reader.Read(p)
if err != nil {
return n, err
}
nn := 0
for i := range p {
if p[i] != 0 {
p[nn] = p[i]
nn++
}
}
p = p[:nn]
// fmt.Println(p) i can see the value of p changing and all the null bytes are removed
return n, nil
}
// Close closes the internal reader
func (null RemoveNull) Close() error {
return null.Close()
}
When I run the following I can see from the print statement that indeed all the null bytes are removed and the len(p) == the size of all the expected good bytes. I wrote the test below to see if the code if working as I intended, and that's where I realized it's not.
Here is the full test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"testing"
"github.com/francoispqt/gojay" // can be replaced with the std json lib, code still doesn't work
)
func TestRemoveNull_Read(t *testing.T) {
type fields struct {
Reader io.ReadCloser
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "should remove null bytes",
fields: fields{
Reader: ioutil.NopCloser(bytes.NewReader([]byte{123, 34, 98, 111, 100, 121, 34, 58, 34, 102, 101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 97, 108, 101, 34, 125})),
},
want: "female",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := tt.fields.Reader
reader = NewRemoveNullStream(tt.fields.Reader) // wrapper the reader above in the nullByte reader
// passed the reader into this JSON unmarshaller
decoder := gojay.BorrowDecoder(reader)
defer decoder.Release()
var v _testStruct
err := decoder.DecodeObject(&v)
if err != nil {
t.Fatalf("ReadAll failed %v", err)
}
bb, _ := json.Marshal(v)
fmt.Println(string(bb)) // all the null bytes are still present
fmt.Println(len(v.Body), len(tt.want))
if v.Body != tt.want {
t.Fatalf("DecodeObject() unexpected value, got %s want %s", v.Body, tt.want)
}
})
}
}
type _testStruct struct {
Body string `json:"body"`
}
func (v *_testStruct) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
switch k {
case "body":
err := dec.String(&v.Body)
return err
}
return nil
}
// NKeys returns the number of keys to unmarshal
func (v *_testStruct) NKeys() int { return 0 }
From the test I can see that all the null bytes are still present when decoding, yet in the RemoveNull reader I can see that all the null bytes have been removed from the underline array. Any thoughts on whats wrong and how i can achieve the goal of removing the bytes from the stream to avoid having the decoder decode the null bytes?
There are errors in your Read implementation. It terminates prematurely in case of io.EOF, where there is both error and data. It returns the wrong number of bytes read. The last part where you assign the slice is also meaningless as it doesn't update the slice passed into the function.
Try this:
func (null RemoveNull) Read(p []byte) (n int, err error) {
n, err = null.Reader.Read(p)
nn := 0
for i:=0;i<n;i++ {
if p[i] != 0 {
p[nn] = p[i]
nn++
}
}
return nn, err
}
l have 5 adjacency matrices (nump arrays) : A, B, C, D, E. each of dimension [20,20].
Given A, B, C, D, E, l would like to build F which stacks the 5 adjacency matrices. Since we have 5 2D arrays of [20,20] then F is of dimension [20*5,20*5] as follow :
F=np.zeros((100,100))
F=[
[A,0,0,0,...,0],
[0,...,B,...,0],
[0,...,..,C,0],
[0,.........D,..,0],
[0,...........,E],
]
such that :
A is indexed at F[0][:20]
B is indexed at F[1][20:40]
C is indexed at F[2][40:60]
D is indexed at F[3][60:80]
E is indexed at F[4][80:100]
What is the efficient numpy way to do that for larage number of adjacency matrices ?. Let's, we have n adjacency matrices to stack in a diagonal of new 2D array of [n*20,n*20]
You could use scipy.sparse.block_diag:
>>> AtoE = np.add.outer(np.arange(5, 10), np.zeros((3, 3), int))
>>> scipy.sparse.block_diag(AtoE).A
array([[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9]], dtype=int64)
Sparse storage may be a good idea, anyway.
Alternatively, here is a more direct method in case you definitely want to use dense arrays:
>>> A = AtoE[0]
>>> N, N = A.shape
>>> k = len(AtoE)
>>> out = np.zeros((k, N, k, N), A.dtype)
>>> np.einsum('ijik->ijk', out)[...] = AtoE
>>> out.reshape(k*N, k*N)
array([[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 6, 6, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 7, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9]])
I am at the beginning of an attempt to make a "minesweeper" game. I have an 8 x 8 array of 0's. I would like to substitute 8 random 0's within the array with the value 1 (to represent "mines"). I have no clue where to begin. Here is my code:
import numpy as np
import sys
import random
a = np.array([(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 0, 0)])
for random.item in a:
item.replace(1)
print(a)
row = int(input("row "))
column = int(input("column "))
print(a[row - 1, column - 1])
How do I replace 8 random 0's within the array with 1's?
Use np.random.choice without replacement option -
In [3]: a # input array of all zeros
Out[3]:
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
# Generate unique flattened indices and on a flattened view of
# input array assign those as 1s
In [8]: a.flat[np.random.choice(a.size,8,replace=False)] = 1
# Verify results
In [9]: a
Out[9]:
array([[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
I have a 3 large groups of integers that I would like to add to different rows of an array. These integers are defined as follows:
#define APARTMENT1_USAGES {0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.189, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.111, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.065, 0.167, 0, 0, 0, 0.048, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0}
#define APARTMENT2_USAGES {0, 0, 0, 0, 0, 0, 0, 0.130, 0, 0, 0, 0, 0, 0.176, 0, 0.125, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.161, 0.000, 0.039, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0.000, 0.109, 0, 0.032, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.152, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0.135, 0, 0, 0, 0, 0, 0, 0, 0, 0.100, 0, 0, 0.063, 0, 0, 0, 0, 0.000, 0, 0.025, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0.000, 0, 0, 0, 0, 0, 0.378, 0, 0.147, 0.229}
#define APARTMENT3_USAGES {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.048, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0, 0, 0, 0, 0, 0.000, 0, 0, 0, 0.01, 0.01, 0.02, 0.03, 0.03, 0, 0, 0, 0, 0.088, 0, 0}
I have shortened these just to show the idea, they are usually 30x24 elements.
What I am trying to do is load each of these into a row of a 2D array, but am getting a syntax error without an explanation. I have tried it as follows:
double apartmentUsage[3][30*24];
apartmentUsage[1][30*24] = APARTMENT1_USAGES;
apartmentUsage[2][30*24] = APARTMENT2_USAGES;
apartmentUsage[3][30*24] = APARTMENT3_USAGES;
One of the errors you are facing is as follows:
If you define array[3], you can have three values stored in array[0] , array[1] and array[2].
In your code. its defined as apartmentUsage[3][..] but starts with 1.
Should be like this-
int multiply=30*24;
apartmentUsage[0][multiply] = APARTMENT1_USAGES;
apartmentUsage[1][multiply] = APARTMENT2_USAGES;
apartmentUsage[2][multiply] = APARTMENT3_USAGES;
**BUT** this too won't completely solve your problem. Try doing it in the initialization itself as pointed in the comments -
double apartmentUsage[3][multiply] = {APARTMENT1_USAGES, APARTMENT2_USAGES, APARTMENT3_USAGES};