Posts

Showing posts with the label arrays

Enumerate the basic data types in C so that the sizes can be easily accessed

Enumerate the basic data types in C so that the sizes can be easily accessed Is it possible to get sizes of all basic datatypes in C using a for loop? For example, can we do something like this? #include <datatypes.h> /* or something else which defines get_data_types() */ #include <stdio.h> int main() { for (int x = 0; x < len(get_data_types()); x++) { printf("Size of %s is %d", get_data_types()[x], sizeof(get_data_types()[x])); } } I could enumerate all the datatypes by replacing the for loop and writing individual statements for int , long , etc. However, I am wondering if it is possible to have a for loop to do the same? int long Essentially, I am trying to avoid the following: #include <stdio.h> int main() { printf("Size of int is %d", sizeof(int); printf("Size of unsigned int is %d", sizeof(unsigned int); printf("Size of long is %d", sizeof(long); /* etc. */ return 0; } Data types of ...

how to sort my array of Custom Point class?

how to sort my array of Custom Point class? I am working on an algorithm that has to identify all pairs of 4 or more co-linear points and form the line segments between them without repetition. I am using a Point class to represent a point(x,y). It looks like this: public class Point { private final int x; // x-coordinate private final int y; // y-coordinate . ... ..... ...... } I have an array of Point class entities from which I have to identify whether the array has duplicate points(same points repeated more than once). I know there are so many ways like nesting for loops of depth 2. But I am interested in the sorting of that array in such a way that all the equal points should come first followed by unequal points in ascending order. Is it possible using the Comparator interface? Comparator Again: it's not possible with just sorting. You can sort your points, and then move blocks of equal points to the fron...

Replacing maximum value with minimum value in a array [on hold]

Replacing maximum value with minimum value in a array [on hold] First input is n numbers then the numbers itself, then find the maximum value and minimum value and last, replace the maximum value with the minimum one. But this code doesn't work first test case ( 5 1 3 3 3 4 ), it returns 3 3 3 3 3. But it works on second test case ( 8 5 4 2 2 4 2 2 5 ). What is wrong with this code ? I have checked the loop which check for maximum and minimum value, nothing wrong. import java.util.Scanner; class Main { public static void main(String args){ Scanner sc = new Scanner(System.in); int marks = new int[sc.nextInt()]; int max = 0; int min = 100; for(int i=0;i<marks.length;i++){ marks[i] = sc.nextInt(); } for(int i=0;i<marks.length;i++){ if(min>marks[i]){ min = marks[i]; } } for(int i=0;i<marks.length;i++){ if(max<marks[i]){ max = marks[i]; } } for(int i=0;...

PHP join 2 arrays of same key value

PHP join 2 arrays of same key value I'm wondering how to solved this. I have 3(three) arrays that contained nested values in the "data" key like this: array:3 [ 0 => array:3 [ "status" => "Terkirim" "warna" => "lightgreen" "data" => array:12 [ 0 => 0 1 => 0 2 => 0 3 => 0 4 => 0 5 => 0 6 => 3 7 => 0 8 => 0 9 => 0 10 => 0 11 => 0 ] ] 1 => array:3 [ "status" => "Selesai" "warna" => "lightblue" "data" => array:12 [ 0 => 0 1 => 0 2 => 0 3 => 0 4 => 0 5 => 0 6 => 3 7 => 0 8 => 0 9 => 0 10 => 0 11 => 0 ] ] 2 => array:3 [ "status" => "Selesai" "warna" => ...

Range of indexes not default initialized in arrays in C++

Range of indexes not default initialized in arrays in C++ The following is the code I am trying to run #include<bits/stdc++.h> using namespace std; int main() { bool x[101010]; for(int i=0;i<101010;i++) { if(x[i]) cout<<i<<" "; } return 0; } As far as I know, the default value of boolean type variables is false . However, for the above code from index 94758-101008 value of i is being printed which means they are default initialized as true. Can anyone please help me in figuring out where am I going wrong? 2 Answers 2 Your problem can be reduced to this: bool x; std::cout << x; A boolean is a fundamental type. Default initializing automatic variables of a fundamental type leaves them with indeterminate values. Not false , but indeterminate. Using those values leads to undefined behavior. This is what you are seeing. false ...

Last letters remove form array in foreach loop

Last letters remove form array in foreach loop I want to remove the last few letters from an array in a for-each loop. I am trying to show bl_date without /2018 . Now its showing 07/10/2018 & 06/30/2018 . How can echo like this 07/10 & 06/30 ? bl_date /2018 07/10/2018 & 06/30/2018 07/10 & 06/30 Array Array ( [0] => stdClass Object ( [id] => 18 [bl_user] => 61 [bl_date] => 07/10/2018 ) [1] => stdClass Object ( [id] => 17 [bl_user] => 61 [bl_date] => 06/30/2018 ) ) PHP $resultstr = array(); foreach ($billings as $billing) { $resultstr = $billing->bl_date; } echo implode(" & ",$resultstr); 2 Answers 2 One option is to use substr() to remove the last 5 characters of the string substr() Like: $resultstr = array(); foreach ($billings a...

create structured numpy array in python with strings and int

create structured numpy array in python with strings and int i have this: >>> matriz [['b8:27:eb:d6:e3:10', '0.428s', '198'], ['b8:27:eb:d6:e3:10', '0.428s', '232'], ['b8:27:eb:07:65:ad', '0.796s', '180'], ['b8:27:eb:07:65:ad', '0.796s', '255'], dtype='<U17']` but i need the column `matriz[:, [2]] : [['198'], ['232'], ['180'], ['255']]` to be int and the other columns to be strings, i was trying with structured numpy array but i have this error message, ValueError: invalid literal for int() with base 10: 'b8:27:eb:d6:e3:10' TypeError: a bytes-like object is required, not 'str' i used matriz=np.array(matriz, dtype='U17,U17,i4') i'm using numpy version '1.12.1' for raspberry pi 3, i don't know what i'm doing wrong. thanks a lot ...

JS: Forward to a site with random parts in the URL

JS: Forward to a site with random parts in the URL by opening a HTML-site, I want to redirect to the following page: www.xyz.de?user=default&f1= OPTION1 &f2= OPTION2 &f3= OPTION3 &f4= OPTION4 &f5= OPTION5 &... etc. The value for the "OPTIONS" should be randomly selected from a list (array) for every OPTION (1,2,3,4,5,..). How can I do this with Javascript? kind regards is the problem to redirect or to generate the string? – Nina Scholz Jun 30 at 18:25 1 Answer 1 Hopefully this will help you, you didn't provide much details or a demo code, so this is from my point of understanding only. <p id="Link">Link Here</p> <p id="randoms">Randomly Sortde Array</p> <script> var o...

How to declare and add items to an array in Python?

How to declare and add items to an array in Python? I'm trying to add items to an array in python. I run array = {} Then, I try to add something to this array by doing: array.append(valueToBeInserted) There doesn't seem to be a .append method for this. How do I add items to an array? .append 4 Answers 4 {} represents an empty dictionary, not an array/list. For lists or arrays, you need . {} To initialize an empty list do this: my_list = or my_list = list() To add elements to the list, use append append my_list.append(12) To extend the list to include the elements from another list use extend extend extend my_list.extend([1,2,3,4]) my_list --> [12,1,2,3,4] To remove an element from a list use remove remove my_list.remove(2) Dictionaries represent a collection of key/value pairs also known as an associative array or a map. To initialize an empty dictionary use {} or dict() {} dict() D...

How to change bytes of array into image?

Image
How to change bytes of array into image? I have a csv file, which was extracted from MS SQL database with image column included. My task is to convert the contents of each row in image column into image and save it as image file. Here is the content of a row- https://drive.google.com/open?id=1J_jz5vN8ATxJ5Qw_c8l0qqKFrIee8kHF As the contents length exceeds the limited word count, I uploaded it to google drive. I changed it to bytearray and try to write it to new file and read it as an image but I haven't got the image. https://drive.google.com/open?id=1h-7cRiTWErZzmMJYmc6XmLmeLK982WZR How could I convert that bytearray to the image?? I have tried writing it to new file and reading it with io. I have spent 8+ hours on this and have tried every solutions found on stackoverflow. Please help me find the solution. Here is my code - df = (pd.Series(df['image'])) df = df.dropna() x = df[3] print (x) bytes = base64.b64decode(x) print (bytes) image = open("new.jpeg",...

How to sort an associative array by its values in Javascript?

How to sort an associative array by its values in Javascript? I have the associative array: array["sub2"] = 1; array["sub0"] = -1; array["sub1"] = 0; array["sub3"] = 1; array["sub4"] = 0; What is the most elegant way to sort (descending) by its values where the result would be an array with the respective indices in this order: sub2, sub3, sub1, sub4, sub0? Since object properties have no language defined order - you can't (except, perhaps, in some JS engines by depending on the particular way that they implemented properties). – Quentin Mar 4 '11 at 22:23 10 Answers 10 Javascript doesn't have "associative arrays" the way you're thinking of them. Instead, you simply have the abi...