answersLogoWhite

0

Cannot be solved without knowing what an 'Element' is.

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

ReneRene
Change my mind. I dare you.
Chat with Rene
ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan

Add your answer:

Earn +20 pts
Q: C program to duplicate element from a set of elements?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is code of finding all subset in a set in c?

Each element of C can either be in a particular subset or not in it - two options for each element of C. So if there are n elements in C, then the number of subsets of C is 2n. These include the null set and C itself.


Write a program to detect duplicate elements in array?

The simplest way of doing this is not very efficient, but provides a quick development time. You want to take all of the elements of the array, add them to a Set, then retrieve them as an array again. Note that this will probably not preserve the order of elements in the array. { Object[] originalArray; // let's pretend this contains the data that // you want to delete duplicates from Set newSet = new HashSet(); for (Object o : originalArray) { newSet.add(o); } // originalArray is now equal to the array without duplicates originalArray = newSet.toArray(); } Now the efficient, and more correct, solution. This one will create a new array of the correct size without duplicates. { Object[] originalArray; // again, pretend this contains our original data // new temporary array to hold non-duplicate data Object[] newArray = new Object[originalArray.length]; // current index in the new array (also the number of non-dup elements) int currentIndex = 0; // loop through the original array... for (int i = 0; i < originalArray.length; ++i) { // contains => true iff newArray contains originalArray[i] boolean contains = false; // search through newArray to see if it contains an element equal // to the element in originalArray[i] for(int j = 0; j <= currentIndex; ++j) { // if the same element is found, don't add it to the new array if(originalArray[i].equals(newArray[j])) { contains = true; break; } } // if we didn't find a duplicate, add the new element to the new array if(!contains) { // note: you may want to use a copy constructor, or a .clone() // here if the situation warrants more than a shallow copy newArray[currentIndex] = originalArray[i]; ++currentIndex; } } // OPTIONAL // resize newArray (using _newArray) so that we don't have any null references Object[] _newArray = new Object[currentIndex]; System.arraycopy(newArray, 0, _newArray, 0, currentIndex); }


How can you make a turbo c program by using one for loop to sort 3 numbers in ascending order?

Use the insertion sort algorithm. Insertion sort works on the premise that a set of 1 can always be regarded as a sorted set, thus in a set of n elements, we can divide the set in two, with a sorted set at the beginning (with 1 element) and an unsorted set at the end (with all other elements). We then take the first element of the unsorted set and insert it into the sorted set, repeating until the unsorted set is empty. We take the first element out of the unsorted set by copying it to a temporary variable, thus creating a gap in its place. We then examine the element to the left of the gap and, if the value is greater than the temporary, we copy it into the gap, effectively moving the gap one place to the left. We stop moving the gap when we reach the start of the array or the element to the left of the gap is not greater than the temporary. We then copy the temporary into the gap, increasing the sorted set by one element and reducing the unsorted set by one element. We can implement this algorithm using just one for loop: void sort (int* a, unsigned s) { // assume a refers to an array of length s for (int i=2; i<s; ++i) { int t = a[i]; // temporarily store a[i] outside the array int g = i; // index g is the "gap" in the array while (0<g && t<a[g-1]) { a[g]=a[g-1]; --g; } // move the gap to the correct insertion point a[g] = t; // insert the temporary } } Thus, to sort 3 numbers: int a[3] = {2, 3, 1}; // unsorted sort (a, 3); assert (a[0]==1); assert (a[1]==2); assert (a[2]==3);


How to program set Cardinality in C plus plus?

The cardinality of a set is simply the number of elements in the set. If the set is represented by an STL sequence container (such as std::array, std::vector, std::list or std::set), then the container's size() member function will return the cardinality. For example: std::vector<int> set {2,3,5,7,11,13}; size_t cardinality = set.size(); assert (cardinality == 6);


The average the sum of the elements divided by the number of elements?

Yes, average is the sum of elements divided by the number of elements Exp:- x1=10, x2=20, x3=30, x4=10. then the average is 10+20+30+10 ------------------ 4 => 17.5 Average = sum of total of element ---------------------------------- no. of elements