Advertisement
alvinfnaldi

meamed1.cpp

Apr 25th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.85 KB | None | 0 0
  1. //*****************************************
  2. //Program : Mean, Median, Mode dan range!.
  3. //*****************************************
  4.  
  5. /*header iostream berisi prototype untuk standar input dan output
  6. jadi agar perintah cout dapat digunakan, maka header ini harus ditulis*/
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. /*header stdlib berisi prototype yang berfungsi salah satunya untuk alokasi memori, dan random number*/
  12. #include <stdlib.h>
  13. //header math.h berisi prototype yang berfungsi untuk fungsi matematika
  14. #include <math.h>
  15.  
  16. double avg(double *nums, unsigned int choice);    //cari average
  17. double sort(double *nums, unsigned int choice);   //urutin nomor
  18. double median(double *nums, unsigned int choice); //cari median
  19. void mode(double *nums, unsigned int choice);     //menentukan mode
  20. double range(double *nums, unsigned int choice);  //hitung range
  21. void work(double *nums, unsigned int choice);
  22. int main()
  23. {
  24.     unsigned int choice = 1;
  25.     double *nums = NULL; //pointer dikosongkan terlebih dahulu
  26.     char yn = 'y';
  27.     cout << endl
  28.          << endl
  29.          << "\t\t\tMean, Median, Mode\n\n\n";
  30.     cout << "berapa banyak angka yang akan dicari rata-ratanya? ";
  31.     cin >> choice;
  32.     unsigned int temp = choice;
  33.     if (choice < 2) /*jika angka yang diinput lebih kecil dari 2, maka tidak bisa cari average*/
  34.  
  35.     {
  36.         cout << endl
  37.              << "Error: masukkan minimal 2 angka." << endl;
  38.  
  39.         exit(0);
  40.         /*berfungsi untuk keluar dari program, dimana kita memberikan nilai 0, misalnya exit(0), pernyataan exit dapat dijalankan apabila kita menambahkan header stdlib.h */
  41.     }
  42.     if (!(nums = new double[choice])) //pointernya dialamatkan ke new double array
  43.     {                                 // jika pointernya tidak bisa masuk ke memori maka programnya keluar
  44.  
  45.         cout << endl
  46.              << "alokasi memori gagal.";
  47.  
  48.         exit(1);
  49.     }
  50.     for (int x = 0; temp > 0; x++)
  51.     {
  52.         cout << "\n\n input nilai untuk cari averagenya (satu persatu): ";
  53.  
  54.         cin >> nums[x];
  55.         temp--;
  56.     }
  57.     cout << "\n\nThanks.\n\n";
  58.  
  59.     sort(nums, choice);
  60.     cout << endl
  61.          << endl
  62.          << " \t\t\t Mean, Median, Mode v1 \n\n";
  63.     cout << "Mean:\t\t" << avg(nums, choice) << "\n";
  64.     cout << "Median:\t\t" << median(nums, choice) << "\n";
  65.     mode(nums, choice);
  66.     cout << "Range:\t\t" << range(nums, choice) << "\n\n";
  67.     work(nums, choice);
  68.     delete[] nums;
  69.     cout << "\n\nmau lakukan lagi gak?(y or n): ";
  70.     cin >> yn;
  71.     if (yn == 'y' || yn == 'Y')
  72.     {
  73.  
  74.         main();
  75.     }
  76.     return 0;
  77. }
  78. //Tahap Penulisan Program Paragraf Rata-Rata
  79.  
  80. double avg(double *nums, unsigned int choice)
  81. {
  82.     unsigned int y = 0;
  83.     double x;
  84.     for (x = 0; choice > y; y++)
  85.     {
  86.         x += nums[y];
  87.     }
  88.     return (x / choice); //dibagi berapa banyak angka yang dimasukkan
  89. }
  90.  
  91. //Tahap Penulisan Program Paragraf Median
  92.  
  93. double median(double *nums, unsigned int choice)
  94. {
  95.     double n = 0;
  96.     long hold = 0;
  97.     long other = 0;
  98.     double holder = 0;
  99.     double m = 0;
  100.     if (choice % 2) //jika angkanya ganjil maka angka yang tengah adalah median
  101.     {
  102.         n = (choice / 2); //cari angka tengahnya berapa
  103.         n += .5;          //menambahkan 0.5
  104.  
  105.         holder = nums[hold]; //holder = the # disimpan di nums[hold]
  106.     }
  107.     else
  108.     {
  109.         hold = (choice / 2);    //kalau nilai choice genap maka nilai hold=angka tengah yang paling besar
  110.         other = (hold - 1);     //other adalah angka tengah yang paling kecil
  111.         n = nums[hold];         //n itu menyimpan nilainya hold
  112.         m = nums[other];        //m itu menyimpan nilainya other
  113.         holder = ((n + m) / 2); //menghitung hasil rata-ratanya
  114.     }
  115.     return holder; //mengembalikan nilai average
  116. }
  117. //Tahap Penulisan Program untuk fungsi pengurutan
  118.  
  119. double sort(double *nums, unsigned int choice)
  120. {
  121.     unsigned int n = 0, k = 0;
  122.     double swap = 0;
  123.     for (n = choice - 1; n > 0; n--)
  124.     {
  125.         for (k = 0; k < n; k++)
  126.         {
  127.             if (nums[k] > nums[k + 1])
  128.             {
  129.                 swap = nums[k];
  130.                 nums[k] = nums[k + 1];
  131.                 nums[k + 1] = swap;
  132.             }
  133.         }
  134.     }
  135.     return *nums;
  136. }
  137.  
  138. //Tahap Penulisan program fungsi mode
  139.  
  140. void mode(double *nums, unsigned int choice)
  141. {
  142.     unsigned int index;
  143.     int count = 0;
  144.     int max = 0;
  145.     for (index = 0; index < choice; index++)
  146.     {
  147.         if (nums[index] == nums[index + 1])
  148.             count++;
  149.         else
  150.         {
  151.             if (count > max)
  152.                 max = count;
  153.             count = 0;
  154.         }
  155.     }
  156.     count = 0;
  157.     for (index = 0; index < choice; index++)
  158.     {
  159.         if (nums[index] == nums[index + 1])
  160.             count++;
  161.         else
  162.         {
  163.             if (count == max)
  164.                 cout << "Mode(s):\t" << nums[index] << "\n";
  165.             count = 0;
  166.         }
  167.     }
  168. }
  169. double range(double *nums, unsigned int choice)
  170. {
  171.     double range = (nums[choice - 1] - nums[0]); //range = angka yang paling besar kurangin sama angka yang paling kecil
  172.     return range;
  173. }
  174.  
  175. //Tahap Penulisan Program untuk fungsi work
  176.  
  177. void work(double *nums, unsigned int choice)
  178. {
  179.     cout << "\nWork:\n";
  180.     for (unsigned int x = 0; x < choice; x++)
  181.     {
  182.         if (x != choice - 1)
  183.         {
  184.             if (x == 0)
  185.             {
  186.                 cout << "\t";
  187.             }
  188.             if (x % 5 == 0)
  189.                 cout << endl
  190.                      << "\t";
  191.             cout << nums[x] << ", ";
  192.         }
  193.         if (x == choice - 1)
  194.         {
  195.             if (x % 5 == 0)
  196.                 cout << endl
  197.                      << "\t";
  198.             cout << nums[x] << ".";
  199.         }
  200.     }
  201.     cout << "\n\nRange\nWork:";
  202.     cout << "   " << nums[choice - 1] << " - " << nums[0] << " = " << nums[choice - 1] - nums[0] << "\n";
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement