Search Results

Search found 3545 results on 142 pages for 'arrays'.

Page 8/142 | < Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >

  • C++ arrays select square number and make new vector

    - by John Smith
    I have to see which of the following from a vector is a square number then make another vector with only the square numbers For example: (4,15,6,25,7,81) the second will be (4,25,81) 4,25,81 because 2x2=4 5x5=25 and 9x9=81 I started like this: { int A[100],n,r,i; cout<<"Number of elements="; cin>>n; for(i=1;i<=n;i++) { cout<<"A["<<i<<"]="; cin>>A[i]; } for(i=1;i<=n;i++) { r=sqrt(A[i]); if(r*r==A[i]) } return 0; } but I am not really sure how to continue

    Read the article

  • Python lists/arrays: disable negative indexing wrap-around

    - by wim
    While I find the negative number wraparound (i.e. A[-2] indexing the second-to-last element) extremely useful in many cases, there are often use cases I come across where it is more of an annoyance than helpful, and I find myself wishing for an alternate syntax to use when I would rather disable that particular behaviour. Here is a canned 2D example below, but I have had the same peeve a few times with other data structures and in other numbers of dimensions. import numpy as np A = np.random.randint(0, 2, (5, 10)) def foo(i, j, r=2): '''sum of neighbours within r steps of A[i,j]''' return A[i-r:i+r+1, j-r:j+r+1].sum() In the slice above I would rather that any negative number to the slice would be treated the same as None is, rather than wrapping to the other end of the array. Because of the wrapping, the otherwise nice implementation above gives incorrect results at boundary conditions and requires some sort of patch like: def ugly_foo(i, j, r=2): def thing(n): return None if n < 0 else n return A[thing(i-r):i+r+1, thing(j-r):j+r+1].sum() I have also tried zero-padding the array or list, but it is still inelegant (requires adjusting the lookup locations indices accordingly) and inefficient (requires copying the array). Am I missing some standard trick or elegant solution for slicing like this? I noticed that python and numpy already handle the case where you specify too large a number nicely - that is, if the index is greater than the shape of the array it behaves the same as if it were None.

    Read the article

  • PHP Arrays: Pop an array of single-element arrays into one array.

    - by Rob Drimmie
    Using a proprietary framework, I am frequently finding myself in the situation where I get a resultset from the database in the following format: array(5) { [0] => array(1) { ["id"] => int(241) } [1] => array(1) { ["id"] => int(2) } [2] => array(1) { ["id"] => int(81) } [3] => array(1) { ["id"] => int(560) } [4] => array(1) { ["id"] => int(10) } } I'd much rather have a single array of ids, such as: array(5) { [0] => int(241) [1] => int(2) [2] => int(81) [3] => int(560) [4] => int(10) } To get there, I frequently find myself writing: $justIds = array(); foreach( $allIds as $id ) { $justIds[] = $id["id"]; } Is there a more efficient way to do this?

    Read the article

  • how to make an array of arrays

    - by yuliya
    hi, how would you write an array of values to other array. In instance, I have a list of IPs and list of requests. I want to have something like [{ip1, request1}, {ip2, request2}, ....]. It's how I would do it, but sure obj will change every time and array will have all the time the same values. ArrayList array = new ArrayList(); Object[] obj = new Object[2]; for (int i=0; i<listSize; i++){ obj[0] = ipList.get(i).toString(); obj[1] = requestList.get(i); array.add(obj);

    Read the article

  • Javascript Inheritance and Arrays

    - by Inespe
    Hi all! I am trying to define a javascript class with an array property, and its subclass. The problem is that all instances of the subclass somehow "share" the array property: // class Test function Test() { this.array = []; this.number = 0; } Test.prototype.push = function() { this.array.push('hello'); this.number = 100; } // class Test2 : Test function Test2() { } Test2.prototype = new Test(); var a = new Test2(); a.push(); // push 'hello' into a.array var b = new Test2(); alert(b.number); // b.number is 0 - that's OK alert(b.array); // but b.array is containing 'hello' instead of being empty. why? As you can see I don't have this problem with primitive data types... Any suggestions?

    Read the article

  • Concatinate integer arrays iteratively

    - by Ojtwist
    I have a methode in2.getImagesOneDim() which gives me an array of integers, to be more precise the pixel values of an image. Now i want to create one big array with all the pixel values of all the images. Therefore I have to call this method several times. Now I would like to concatenate the previous output to the current output until all images are read. In some kind of pseudo code, where the + is a concatination ... : for (int i = 1; i < 25; i++) { ConArray = ConArray + in2.getImagesOneDim("../images/"+i); } How would I do this in java ?

    Read the article

  • Help with PHP and associative arrays

    - by errata
    Hello. I have to do a simple calculator in php based on user's input and choice from select field, something like this: <?php $a = $_GET['a']; $b = $_GET['b']; $array = array( "option1" => 0.1, "option2" => 0.15, "option3" => 0.3, "option4" => 3, "option5" => 3, "option6" => 16, "option7" => 16, "option8" => 16 ); echo "<form action='calc.php' method='get'>"; echo "<input type='text' name='a' value='".$a."'> of "; echo "<select name='b'>"; foreach ($array as $k => $v) { echo "<option value='".$v."'>".$k."</option>"; } echo "</select> "; echo "<input type='submit' value='='> "; $total_volume = $a * $b; echo $total_volume; echo "</form>"; ?> Well, for now everything works fine, but the idea is that after user submits form, the page reloads with sent amount in input field and selected option which user actually selected... First thing is easy: I just put value="a" in my input field, but I'm not sure how to make a selected option in <select> field??? I started with this: foreach ($array as $k => $v) { echo "<option value='".$v."'"; if ($b == $v) { echo " selected "; } echo ">".$k."</option>"; } ...but this is obviously not working as expected... Please help me with this easy one :) Thanks!

    Read the article

  • looping and arrays

    - by user1838418
    Hi I'm trying to construct a loop to execute 16 states of the 8 4 2 1 code in (C++) while( condition) { double Bubble[16], Bubble1[16]; Bubble[0] = ( a-2 - (b-2) ) + ( c-2 - (d-2)); // represents 0000 Bubble[1] = ( a-2 - (b-2) ) + ( c-2 - (d+2)); // represents 0001 Bubble[2] = ( a-2 - (b-2) ) + ( c+2 - (d-2)); // represents 0010 Bubble[3] = ( a-2 - (b-2) ) + ( c+2 - (d+2)); //represents 0011 ....... Bubble[15] =(a+2 - (b+2) ) + ( c+2 - (d+2)); //represents 1111 } Is there an easy way of coding using for loops? instead of writing bubble[] every time? 0 stands for -2 and 1 stands for +2. So I have 4 variables and each one need to be incremented and/or decremented. Can this be done using for loop? Appreciate your help

    Read the article

  • Subtype polymorphism and arrays

    - by user133466
    Computer[] labComputers = new Computer[10]; with public class Computer { ... void toString(){ // print computer specs } } public class Notebook extends Computer{ ... void toString(){ // print computer specs + laptop color } } each subscripted variable labComputers[i] can reference either a Computer object or a Notebook object because Notebook is a subclass of Computer. For the method call labComputers[i].toString(), polymorphism ensures that the correct toString method is called. I wonder what if we do Notebook[] labComputers = new Notebook[10]; what kind or error would I get if I reference with Computer object and a Notebook object

    Read the article

  • PHP - Patterns within Arrays

    - by Toby
    I am trying to create a function which maps a recurring pattern of integers using an array. As an example if I have a starting array of (0,1,3) and I know that I want to stop the pattern when I hit 15. The pattern gets incremented by a fixed integer each time (lets say 4) so my final pattern should be.. 0 1 3 4 (0 + 4) 5 (1 + 4) 7 (2 + 4) 8 (4 + 4) 9 (5 + 4) 11(7 + 4) 12(8 + 4) 13(9 + 4) 15(11+ 4) Does anyone have any pointers on how this can be achieved? My current implementation works but is stupidly inefficient which something like this... $array = array(0,1,3); $inc = 4; $end = end($array); $final = 15; while($end < $final) { $tmp = array(); foreach($array AS $row) { $tmp = $row + $inc; } $array = merge($tmp, $array); $end = end($array); }

    Read the article

  • C++ Arrays manipulations (python-like operations)

    - by Linai
    Hi Guys, I'm trying to figure out the best C++ library/package for array manipulations in a manner of python. Basically I need a simplicity like this: values = numpy.array(inp.data) idx1 = numpy.where(values > -2.14) idx2 = numpy.where(values < 2.0) res1 = (values[idx1] - diff1)/1000 res1 = (values[idx2] - diff2)*1000 In python it's just 5 lines, but the simplest way in C++ i can think of is quite a number of nested loops. Pls advise..

    Read the article

  • Can Perl detect arrays?

    - by Sandra Schlichting
    I have this script #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %x1 = (); $x1{"a"} = "e"; my %x2 = (); $x2{"a"} = ["b","c"]; p(\%x1); p(\%x2); sub p { my $x = shift @_; print $x->{a}; print "\n"; } which outputs e ARRAY(0x2603fa0) The problem is I don't know when the input is an array or a scalar, and when it is an array I would like to print those values as well. Can p be modified to do this?

    Read the article

  • Comparing arrays with sql

    - by Nissim
    I want to perform a 'SELECT' statement with a byte array (binary) parameter as a condition. I tried to google it, but didn't find anything useful. In general, I keep information of files in the database. one of the properties is the file's hash (binary). I want to give a hash to the SELECT statement, and get all rows with the same hash value.

    Read the article

  • C Allocating Two Dimensional Arrays

    - by Jacob
    I am trying to allocate a 2D dimension array of File Descriptors... So I would need something like this fd[0][0] fd[0][1] I have coded so far: void allocateMemory(int row, int col, int ***myPipes){ int i = 0,i2 = 0; myPipes = (int**)malloc(row * sizeof(int*)); for(i = 0; i < row;i++){ myPipes[i] = (int*)malloc(col * sizeof(int)); } } How can I set it all too zeros right now I keep getting a seg fault when I try to assign a value... Thanks

    Read the article

  • Declaring arrays in c language without initial size

    - by user2534857
    this is the question-- Write a program to manipulate the temperature details as given below. - Input the number of days to be calculated. – Main function - Input temperature in Celsius – input function - Convert the temperature from Celsius to Fahrenheit.- Separate function - find the average temperature in Fahrenheit. how can I make this program without initial size of array ?? #include<stdio.h> #include<conio.h> void input(int); int temp[10]; int d; void main() { int x=0; float avg=0,t=0; printf("\nHow many days : "); scanf("%d",&d); input(d); conv(); for(x=0;x<d;x++) { t=t+temp[x]; } avg=t/d; printf("Avarage is %f",avg); getch(); } void input(int d) { int x=0; for(x=0;x<d;x++) { printf("Input temperature in Celsius for #%d day",x+1); scanf("%d",&temp[x]); } } void conv() { int x=0; for(x=0;x<d;x++) { temp[x]=1.8*temp[x]+32; } }

    Read the article

  • Loop crashing program having to do with 2D arrays

    - by user450062
    I am creating an encoding program and when I instruct the program to create a 5X5 grid based on the alphabet while skipping over letters that match up to certain pre-defined variables(which are given values by user input during runtime). I have a loop that instructs the loop to keep running until the values that access the array are out of bounds, the loop seems to cause the problem. This code is standardized so there shouldn't be much trouble compiling it in another compiler. Also would it be better to seperate my program into functions? here is the code: #include<iostream> #include<fstream> #include<cstdlib> #include<string> #include<limits> using namespace std; int main(){ while (!cin.fail()) { char type[81]; char filename[20]; char key [5]; char f[2] = "q"; char g[2] = "q"; char h[2] = "q"; char i[2] = "q"; char j[2] = "q"; char k[2] = "q"; char l[2] = "q"; int a = 1; int b = 1; int c = 1; int d = 1; int e = 1; string cipherarraytemplate[5][5]= { {"a","b","c","d","e"}, {"f","g","h","i","j"}, {"k","l","m","n","o"}, {"p","r","s","t","u"}, {"v","w","x","y","z"} }; string cipherarray[5][5]= { {"a","b","c","d","e"}, {"f","g","h","i","j"}, {"k","l","m","n","o"}, {"p","r","s","t","u"}, {"v","w","x","y","z"} }; cout<<"Enter the name of a file you want to create.\n"; cin>>filename; ofstream outFile; outFile.open(filename); outFile<<fixed; outFile.precision(2); outFile.setf(ios_base::showpoint); cin.ignore(std::numeric_limits<int>::max(),'\n'); cout<<"enter your codeword(codeword can have no repeating letters)\n"; cin>>key; while (key[a] != '\0' ){ while(b < 6){ cipherarray[b][c] = key[a]; if ( f == "q" ) { cipherarray[b][c] = f; } if ( f != "q" && g == "q" ) { cipherarray[b][c] = g; } if ( g != "q" && h == "q" ) { cipherarray[b][c] = h; } if ( h != "q" && i == "q" ) { cipherarray[b][c] = i; } if ( i != "q" && j == "q" ) { cipherarray[b][c] = j; } if ( j != "q" && k == "q" ) { cipherarray[b][c] = k; } if ( k != "q" && l == "q" ) { cipherarray[b][c] = l; } a++; b++; } c++; b = 1; } while (c < 6 || b < 6){ if (cipherarraytemplate[d][e] == f || cipherarraytemplate[d][e] == g || cipherarraytemplate[d][e] == h || cipherarraytemplate[d][e] == i || cipherarraytemplate[d][e] == j || cipherarraytemplate[d][e] == k || cipherarraytemplate[d][e] == l){ d++; } else { cipherarray[b][c] = cipherarraytemplate[d][e]; d++; b++; } if (d == 6){ d = 1; e++; } if (b == 6){ c++; b = 1; } } cout<<"now enter some text."<<endl<<"To end this program press Crtl-Z\n"; while(!cin.fail()){ cin.getline(type,81); outFile<<type<<endl; } outFile.close(); } } I know there is going to be some mid-forties guy out there who is going to stumble on to this post, he's have been programming for 20-some years and he's going to look at my code and say: "what is this guy doing".

    Read the article

  • abstract data type list. . .

    - by aldrin
    A LIST is an ordered collection of items where items may be inserted anywhere in the list. Implement a LIST using an array as follows: struct list { int *items; // pointer to the array int size; // actual size of the array int count; // number of items in the array }; typedef struct list *List; // pointer to the structure Implement the following functions: a) List newList(int size); - will create a new List and return its pointer. Allocate space for the structure, allocate space for the array, then initialize size and count, return the pointer. b) void isEmpty(List list); c) void display(List list); d) int contains(List list, int item); e) void remove(List list, int i) ; f) void insertAfter(List list,int item, int i); g) void addEnd(List list,int item) - add item at the end of the list – simply store the data at position count, then increment count. If the array is full, allocate an array twice as big as the original. count = 5 size = 10 0 1 2 3 4 5 6 7 8 9 5 10 15 20 30 addEnd(list,40) will result to count = 6 size = 10 0 1 2 3 4 5 6 7 8 9 5 10 15 20 30 40 h) void addFront(List list,int item) - shift all elements to the right so that the item can be placed at position 0, then increment count. Bonus: if the array is full, allocate an array twice as big as the original. count = 5 size = 10 0 1 2 3 4 5 6 7 8 9 5 10 15 20 30 addFront(list,40) will result to count = 6 size = 10 0 1 2 3 4 5 6 7 8 9 40 5 10 15 20 30 i) void removeFront(List list) - shift all elements to the left and decrement count; count = 6 size = 10 0 1 2 3 4 5 6 7 8 9 40 5 10 15 20 30 removeFront(list) will result to count = 5 size = 10 0 1 2 3 4 5 6 7 8 9 5 10 15 20 30 j) void remove(List list,int item) - get the index of the item in the list and then shift all elements to the count = 6 size = 10 0 1 2 3 4 5 6 7 8 9 40 5 10 15 20 30 remove(list,10) will result to count = 5 size = 10 0 1 2 3 4 5 6 7 8 9 40 5 15 20 30

    Read the article

  • How to find unique values in jagged array

    - by David Liddle
    I would like to know how I can count the number of unique values in a jagged array. My domain object contains a string property that has space delimitered values. class MyObject { string MyProperty; //e.g = "v1 v2 v3" } Given a list of MyObject's how can I determine the number of unique values? The following linq code returns an array of jagged array values. A solution would be to store a temporary single array of items, looped through each jagged array and if values do not exist, to add them. Then a simple count would return the unique number of values. However, was wondering if there was a nicer solution. db.MyObjects.Where(t => !String.IsNullOrEmpty(t.MyProperty)) .Select(t => t.Categories.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) .ToArray() Below is a more readable example: array[0] = { "v1", "v2", "v3" } array[1] = { "v1" } array[2] = { "v4", "v2" } array[3] = { "v1", "v5" } From all values the unique items are v1, v2, v3, v4, v5. The total number of unique items is 5. Is there a solution, possibly using linq, that returns either only the unique values or returns the number of unique values?

    Read the article

  • Delphi SetLength Custom Indexing

    - by Andreas Rejbrand
    In Delphi, it is possible to create an array of the type var Arr: array[2..N] of MyType; which is an array of N - 1 elements indexed from 2 to N. If we instead declare a dynamic array var Arr: array of MyType and later allocate N - 1 elements by means of SetLength(Arr, N - 1) then the elements will be indexed from 0 to N - 2. Is it possible to make them indexed from 2 to N (say) instead?

    Read the article

  • filtering search results with php

    - by fl3x7
    Hello, Cant really find any useful information on this through Google so hope someone here with some knowledge can help. I have a set of results which are pulled from a multi dimensional array. Currently the array key is the price of a product whilst the item contains another array which contains all the product details. key=>Item(name=>test, foo=>bar) So currently when I list the items I just order by the key, smallest first and it lists the products smallest price first. However I want to build on this so that when a user sees the results they can choose other ordering options like list all products by a name, certain manufacturer, colour, x ,y ,z etc etc from a drop down box(or something similar) This is where I need some guidance. Im just not sure how to go about it or best practise or anything. The only way I can think of is to order all the items by the nested array eg by the name, manufacturer etc. but how do I do that in PHP? Hope you understand what im trying to achieve(if not just ask). Any help on this with ideas, approaches or examples would be great. Thanks for reading p.s Im using PHP5

    Read the article

  • Visual C#, Large Arrays, and LOH Fragmentation. What is the accepted convention?

    - by Gorchestopher H
    I have an other active question HERE regarding some hopeless memory issues that possibly involve LOH Fragmentation among possibly other unknowns. What my question now is, what is the accepted way of doing things? If my app needs to be done in Visual C#, and needs to deal with large arrays to the tune of int[4000000], how can I not be doomed by the garbage collector's refusal to deal with the LOH? It would seem that I am forced to make any large arrays global, and never use the word "new" around any of them. So, I'm left with ungraceful global arrays with "maxindex" variables instead of neatly sized arrays that get passed around by functions. I've always been told that this was bad practice. What alternative is there? Is there some kind of function to the tune of System.GC.CollectLOH("Seriously") ? Are there possibly some way to outsource garbage collection to something other than System.GC? Anyway, what are the generally accepted rules for dealing with large (85Kb) variables?

    Read the article

< Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >