An algorithm to delete all the multiple matching elements.

###An algorithm to delete all the multiple matching elements.
 Source Code in C++: 

#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
  int i,j,k,a[100],m;
  std::cout << "How many elements: " ;
  std::cin >> k;
  std::cout << "Take all the elements: " << '\n';
  for(i=0;i<k;i++){
    std::cin >> a[i];
  }
  std::cout << "Which element you wanna delete: " ;
  std::cin >> m;
  i=0;
  loop:do {
    if (m==a[i]) {
      i=i+1;
      goto loop;
    }
    std::cout << a[i] << '\n';
    i++;
  } while(i<k);

  return 0;
}


Output:
How many elements: 6
Take all the elements:
1
2
3
3
4
5
Which element you wanna delete: 3
1
2
4
5

Press any key to continue . . .




Explanation:
Here we were asked to delete all the multiple matching array elements.We first took an array then 
took the element we wanna delete.Then we did the trick here.We used do-while loop and then we applied the condition ( if (m==a[i])).
m=Element we wanna delete.
If the condition becomes true,we just break it and didn't print that value and we use "goto" to do that.and we increamented "i" for one time.This condition will be applied similarly if any element found for several times.
This is how we can delete all the multiple matching element in an array.










No comments

Powered by Blogger.