C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

データセット内のモードを見つけるためのC++プログラム


これは、データセット内のモードを見つけるためのC++プログラムです。

アルゴリズム

Begin
   function insertinset() to insert data in the set.
   Create newnode and temp(t) node.
   Node to be inserted in the list using newnode.
   If head is null then
      assign new node to head and increase the count.
   During insertion perform insertion sort for sorting data.
   If the newnode->data is equal to any of the element present in the set,
      then just increment count.
End
をインクリメントするだけです。

#include <iostream>
using namespace std;
struct set // a structure set to declare variables
{
   int data;
   int cnt;
   set *n;
};
set* insertinset(set *head, int n) {
   set *newnode = new set; //to use structure set’s variables.
   set *t = new set;
   newnode->data = n;
   newnode->cnt = 0;
   newnode->n = NULL;
   if(head == NULL) {
      head = newnode;
      head->cnt++;
      return head;
   } else {
      t = head;
      if(newnode->data < head->data) {
         newnode->n = head;
         head = newnode;
         newnode->cnt++;
         return head;
      } else if(newnode->data == head->data) {
         head->cnt++;
         return head;
      }
      while(t->n!= NULL) {
         if(newnode->data == (t->n)->data) {
            (t->n)->cnt++;
            return head;
         }
         if(newnode->data < (t->n)->data)
            break;
         t=t->n;
      }
      newnode->n = t->n;
      t->n = newnode;
      newnode->cnt++;
      return head;
   }
}
int main() {
   int n, i, num, max = 0, c;
   set *head = new set;
   head = NULL;
   cout<<"\nEnter the number of data element to be sorted: ";
   cin>>n;
   for(i = 0; i < n; i++) {
      cout<<"Enter element "<<i+1<<": ";
      cin>>num;
      head = insertinset(head, num); //call the function
   }
   cout<<"\nSorted Distinct Data ";
   while(head != NULL) // if head is not equal to null
   {
      if(max < head->cnt) {
         c = head->data;
         max = head->cnt;
      }
      cout<<"->"<<head->data<<"("<<head->cnt<<")"; //return the count of the data.
      head = head->n;
   }
   cout<<"\nThe Mode of given data set is "<<c<<" and occurred "<<max<<" times.";
   return 0;
}

出力

Enter the number of data element to be sorted: 10
Enter element 1: 1
Enter element 2: 2
Enter element 3: 0
Enter element 4: 2
Enter element 5: 3
Enter element 6: 7
Enter element 7: 6
Enter element 8: 2
Enter element 9: 1
Enter element 10: 1
Sorted Distinct Data ->0(1)->1(3)->2(3)->3(1)->6(1)->7(1)
The Mode of given data set is 1 and occurred 3 times.

  1. C++で三角形の図心を見つけるプログラム

    この問題では、三角形の3つの頂点の座標を示す2D配列が与えられます。私たちのタスクは、C++で三角形のセントロイドを見つけるプログラムを作成することです。 セントロイド 三角形の3つの中央値は、三角形の3つの中央値が交差する点です。 中央値 三角形の頂点は、三角形の頂点とその反対側の線の中心点を結ぶ線です。 問題を理解するために例を見てみましょう 入力 (-3, 1), (1.5, 0), (-3, -4) 出力 (-3.5, -1) 説明 Centroid (x, y) = ((-3+2.5-3)/3, (1 + 0 - 4)/3) = (-3.5, -1) ソリューションアプロ

  2. C++で平行四辺形の面積を見つけるプログラム

    この問題では、平行四辺形の底と高さを表す2つの値が与えられます。私たちのタスクは、C++で平行四辺形の領域を見つけるプログラムを作成することです。 平行四辺形 は、反対側が等しく平行な4辺の閉じた図形です。 問題を理解するために例を見てみましょう 入力 B = 20, H = 15 出力 300 説明 平行四辺形の面積=B* H =20 * 15 =300 ソリューションアプローチ この問題を解決するために、平行四辺形の面積の幾何学的公式を使用します。 Area = base * height. ソリューションの動作を説明するプログラム 例 #include <io