C Sharp :: Comparing For Common Elements In Multiple Lists Using Intersect In C#
Mar 20, 2013
I have a dynamic number of lists coming in from a different method . i need to find out the common elements present in all of the lists .
How do i do it with intersect construct in C#.
Placing it in a loop gets me the elements common in the last comparison , but never gets me elements commong in all lists.
View 1 Replies
ADVERTISEMENT
Nov 6, 2013
we conform to the ISO C standard and this snippet of code : Code: vector<tree*> *leaves = new vector<tree*>[num_threads]; where num_threads is specified from command line arguments so not dynamically allocating it violates the standard.
Let's also assume num_threads is greater than one.
What I want to do is scan each vector in leaves for duplicates. If any two vectors in the set have matching addresses, they both immediately go onto the "unsafe" pile and will no longer be subject for testing.
If a vector clears one vector, we test it against the others in the set.
So if we have 3 vectors, A, B and C we test A against B then A against C. For efficiency, we then then just test B against C.
Like I said, I want a "safe" and "unsafe" pile. Every vector in "safe" is fully unique while every vector in "unsafe" is not unique.
I thought about just using a for-loop to loop through leaves and then iterate through each element but I'm not sure if that'll work just right out of the box.
View 14 Replies
View Related
Apr 29, 2013
I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.
It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.
template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;
[Code]......
View 10 Replies
View Related
Jan 23, 2015
So I'm trying to make two arrays, then the third array that will include common elements of those two arrays. I did make a code for that, but the problem is I don't know how to include that the elements do not repeat.
For example, if there are two arrays,
Elements of the first one: 2, 3
Elements of the second one: 2, 3, 2
The third array is going to be: 2 2
While I want it to be only 2.
#include <iostream>
using namespace std;
int* intersection(int* n1, int d1, int* n2, int d2, int& d3) {
d3=0;
for (int i=0; i<d1; i++) {
[Code] ....
View 14 Replies
View Related
Sep 2, 2014
If I have 2 arrays, say array1 = {2, 2, 4, 5, 6} which has sorted array elements and array2 = {4, 2, 6, 2, 5} that has same elements as array1 but not in sorted condition. How can I compare that both arrays have same elements. I have written the following code which works in all conditions except when array1 has two same elements and array2 has all different elements.
counter=0;
for (i=0; i<5; i++) {
for (int j=0; j<5; j++)
if (array2[i] == array1[j]) {
counter++;
array1[j]=0;
[Code] ....
View 7 Replies
View Related
Feb 17, 2015
int result = 1;
for (int j=0;j<N;j++) {
bool found = false;
for (int i=0;i<N && !found;i++) {
if (a[j] == b[i]) found = true;
}
if (!found) return 0;
}
I need to create a code that compares two in arrays without sorting them. They have to be the same length and contain the same elements in any order.
every integer in a[] is also in b[]
every integer in b[] is also in a[]
all such common values appear exactly the same number of times in both a[] and b[]
EX: a = {1, 2, 3}, b = {2, 3, 4} return 0 a = {1, 2, 3}; b = {2, 3, 1} return 1 a = {1, 2, 2}; b = {2, 2, 1} return 1 a = {1, 2, 2}; b = {2, 1, 1} return 0 a = {1, 1, 2, 2, 2}; b = {2, 1, 2, 1, 2} return 1
This is all i have...
View 8 Replies
View Related
May 1, 2013
I have a problem like this: I do not know how to count the number of elements in a list of lists. I have the following list of list.
#define VAR(V, init) __typeof(init) V=(init)
#define FOR_EACH(I,C) for(VAR(I, (C).begin()), ite = (C).end();
(I) != ite;
++(I))
std::vector<std::vector<GLdouble> > contours;
[Code] ....
I do not know how to write the part indicated in Bold that represents the number of elements of contour. As seen from the code used for the tesselation OpenGL.
View 3 Replies
View Related
Jan 6, 2015
I am reading a book currently on data structures in c++. The questions I have is how I would be able to swap two adjacent elements by adjusting only the links (not the data) using, a) singly linked lists, doubly linked lists.
For the single linked list which I am somewhat familiar with (by the content of the book), I would consider taking the Node A, and copying its data into a new Node temp, then re-routing the pointer from whatever connected to Node A, now to Node temp. now I want to re-route the pointer of Node B to Node temp and Node temp to whatever Node was being connected from Node B. Is this the correct approach?
View 1 Replies
View Related
Sep 21, 2013
I am writing a Windows Form program, and currently it has 3 Lists. I need to loop through all three, and use each possible combination, so am using three nested for loops.
I found that if the outer loop had less elements than one of the inner loops/lists, some were missed off, as I need to save all the possible combinations of list a + b + c.
This is what I wrote:
Code:
public int CalculateOrderToProcessLists()//for three lists
{
int order = 0;
if (sListOne.Count() > sListTwo.Count() && sListTwo.Count() > sListThree.Count())
{
order = 123;
[Code ....
My problem is I would like to be able to have 4 (or more if possible) lists, and to calculate what order to process the lists will be exponentially more complex.
Any better way of comparing the sizes of the lists, so that I can use the nested loops of say 4 lists, but being able to use N number of lists would be awesome.
View 2 Replies
View Related
Jul 4, 2014
I have an array of const char's that are randomly selected in a loop from a list and id like to compare every newly selected choice to be tested against all the others to make sure the same choice isn't given more than once, however the names are lengthy and, for example, using:
for (int x = 0; x<10;x++)//initial loop
while ((choice[x]== choice[x-1] || (choice[x] == choice[x-2]) || etc...)
//^given that I have 10 variables
Would be messy and a painful sight. What would a more convinient way to check each choice?
Edit:It should be said that each choice would then be randomized again and then checked again, and that each newly selected choice is then immediantly used after this. It'll also be assumed that not all the choices have been made when this part runs(ergo choice[3] may not exist yet) as it is in a loop
View 1 Replies
View Related
Mar 1, 2012
Say I have 5 vectors of unsigned char each of size 5. I want to take the max of each index and store it in a new vector. What is the most optimal way to accomplish this?
My slow code:
Code:
vector<unsigned char> vec1;
vector<unsigned char> vec2;
vector<unsigned char> vec3;
vector<unsigned char> vec4;
[Code]....
View 3 Replies
View Related
Mar 12, 2013
Im working on a script compiler and i need to handle different types of data. Actually different categories of items.
Let's say i have two categories: cat's and bird's. They are different and stored in different lists. And let's say there is a simple command: GIVE_FOOD_TO(animal_type, food_type) Animal type here can be either from birds category or cat's category.
And also let's say user gives command: GIVE_FOOD_TO(cat1, fish)
and also for example: GIVE_FOOD_TO(bird1, birdfood)
Variable names could be anything, but im storing each variable name in std::map so later i can figure out with what animal current command is used.
When im parsing the script then i must know if user supplied either cat or bird. If i just would have cat's or bird's category then i would have only 2 lists and not a big problem to loop through either cats list or birds list and find out in what list the "cat1" is or in what particular list the "bird1" is. It would be in one or another.
But i have a lot of categories and looping through all of these lists (or std::maps) is slow and doesn't seem like a good. Just to find out in what list it's stored. I can't rely on variable names, they could be anything.
Big picture atm:
1) I have one BIG box which stores all of the categories
2) When i need to find out to which category the variable (animal_type) point's to i must get it quickly, dunno, std::map in std::map or something?
What i need basically is: I have different lists (each one is just a category for either birds or cats in this example) And when i have variable name, i must find out quickly in what particular category this item is stored. So i can work with it.
View 4 Replies
View Related
Apr 13, 2014
I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.
However, Is designing a complete class just for a few functions over kill?
Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?
View 4 Replies
View Related
Sep 27, 2014
I used to use map to access elements. map has a good feature that it sort the element automatically. Now I need a map which can access element through multiple key values. So I choosed boost::multi_index_container. I defined a container as follows.
struct elem {
int a,b;
elem(int aa,int bb):a(aa),b(bb) {}
};
typedef multi_index_container <
[Code] ....
What I am wondering is whether boost::multi_index_container can sort elements automatically. Specifically, are all elements extracted through iterator from begin to end shown below having b values between 2 and 100?
test t;
test::iterator begin = t.lower_bound(make_tuple(1,2));
test::iterator end = t.upper_bound(make_tuple(1,100));
View 3 Replies
View Related
Jan 11, 2015
I have array of 100 elements and I'm trying to write elements which index is smaller than the index of the maximum element of the array. My problem is that I can't determine how big this array should be . I found which is my biggest index.
Code:
#include <visual_2013.h>
#include <stdio.h>
#include <stdlib.h>
#define DIM 10
void enterArray(int x[]);
void array2(int x[]);
int maxAndIndex(int x[], int n, int *index);
int main()
[Code]...
View 3 Replies
View Related
Jan 20, 2014
am trying to create a program that asks the user personal questions.
std::vector<std::string> name, age, favsinger;
std::cout << "Hello, what is your Name Age Favorite_Singer? ";
std::cin << name; //i want to store the user's info along with the sibling's
[Code]....
View 4 Replies
View Related
Feb 8, 2013
I've written some code below to check if two line segments intersect and if they do to tell me where. As input I have the (x,y) coordinates of both ends of each line. It appeared to be working correctly but now in the scenario where
line A (532.87,787.79)(486.34,769.85)
and
line B (490.89,764.018)(478.98,783.129)
it says they intersect at (770.136, 487.08) when the lines don' intersect at all.
Code:
dy[0] = y2 - y1;
dx[0] = x2 - x1;
dy[1] = y4 - y3;
dx[1] = x4 - x3;
m[0] = dy[0] / dx[0];
m[1] = dy[1] / dx[1];
b[0] = y1 - m[0] * x1;
b[1] = y3 - m[1] * x3;
[Code] ....
View 7 Replies
View Related
Apr 19, 2013
I have a txt file that looks like this:
Student ID:97707; Grades: 87.73, 90.41, 91.74, 95.04, 99.13; Name:Davis, Artur
Student ID:23628; Grades: 58.09, 65.18, 68.62, 68.62, 98.05; Name:Davis, Susan
Student ID:49024; Grades: 18.37, 66.06, 68.07, 80.91, 96.47; Name:DeGette, Diana
-I need to read the id, grades and names;
-The id to a separate array
-The grades to a 2-d array
-And the names to a c style string.
Where do I start, im having trouble read the dummy to start
int main() {
char filename[15] = "ex.txt";
string names[MAX_NAMES];
double grades[MAX_ROWS][MAX_COLS];
int id[MAX_IDS];
int index = 0;
ifstream fin;
[Code] .....
View 3 Replies
View Related
Feb 20, 2014
I'm trying to find the fastest method to find whether or not two rectangles intersect in an effort to streamline my code. I've built this function into a rectangle class I created below.
struct rectangle{
rectangle() :X1(0), Y1(0), X2(0), Y2(0) {};
rectangle(int X1, int Y1, int X2, int Y2)
:X1(X1), Y1(Y1), X2(X2), Y2(Y2) {};
[Code].....
View 3 Replies
View Related
Aug 27, 2014
I am trying to query an Informix database using a List<T> collection's elements as variables. I can build the list and connect to the database, but I am unsure how to iterate through the list and query the database for each item in the collection.
My list is of type string, and contains Order Numbers. I want to query item information for each order number in the list.
View 1 Replies
View Related
Jun 3, 2013
So I've got a button that has to use 2 textboxes to search for 2 columns in my database.
private void btnFilter_Click(object sender, EventArgs e) {
SQLiteDataAdapter adap = new SQLiteDataAdapter("select * from Test", GetConnection());
DataSet ds = new DataSet();
adap.Fill(ds);
dgvTable.DataSource = ds.Tables[0];
[Code] ....
My Current code for it.
The GetConnection(); and the CloseConnection(); Methods are just making a new connection and closing that connection, if needed I can post that code too later.
But basically, It only "filters" 1 textbox but if I say I want to search the Channel and the Log in which it is in, it doesn't search anything and returns all data in the database.
View 2 Replies
View Related
Nov 15, 2012
So I have an application that needs to send the current IP Address to a service. My problem is that I have a couple of machines that show two IPV4 addresses on the same NIC. Unfortunately the standard .net calls for the IP Address always appear to show the IP Addresses in numerically sorted order and it will be random as to which one of multiple IP Addresses my application uses.
Any way to force Windows 7 and XP to always put a specific IP address as the first one regardless of the numerical order?
View 1 Replies
View Related
Nov 29, 2010
I have a tcp client - server implementation running in the same program, on different background worker threads. There will be instances of this program on multiple computers so they can send and receive files between each other. I can send files sequentially between computers using network stream, but how would I send multiple files at the same time from computer A to B.
Sending multiple files over one connection ( socket ) is fine, but having multiple network streams sending data to a client, the client doesn't know which chunk of data is apart of which file ?
View 3 Replies
View Related
Nov 22, 2012
how l can add multiple records when using the console app. This is the excercise l have done so far:
string name;
string surname;
int score;
Console.Write("Enter Name:");
[Code]....
l want to be able to add more student records and display the student with the top score.
View 4 Replies
View Related
Aug 14, 2012
I'm having a bit of trouble with something. What I'm trying to do is create a new Access table by combining other already existing Tables.
For instance, let's consider Table1 (with columns A, B and C), Table2 (with columns A, D and E) and Table3 (with columns A, F and G), where Table1.A = Table2.A = Table3.A.
From these, I want to create a new table TableResult, with columns A, B, C, D, E, F and G, with all the data contained in them. There's one catch: I want the code to get the names of the other columns, apart from column A.
I've thought about using Union, but I need to actually create a new table by combining the previous tables.
View 11 Replies
View Related
Sep 18, 2013
Need to create multiple word documents on the run.
My problem is its creating the last one only.
View 1 Replies
View Related