Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - //*****************************************
 - //Program : Mean, Median, Mode dan range!.
 - //*****************************************
 - /*header iostream berisi prototype untuk standar input dan output
 - jadi agar perintah cout dapat digunakan, maka header ini harus ditulis*/
 - #include <iostream>
 - using namespace std;
 - /*header stdlib berisi prototype yang berfungsi salah satunya untuk alokasi memori, dan random number*/
 - #include <stdlib.h>
 - //header math.h berisi prototype yang berfungsi untuk fungsi matematika
 - #include <math.h>
 - double avg(double *nums, unsigned int choice); //cari average
 - double sort(double *nums, unsigned int choice); //urutin nomor
 - double median(double *nums, unsigned int choice); //cari median
 - void mode(double *nums, unsigned int choice); //menentukan mode
 - double range(double *nums, unsigned int choice); //hitung range
 - void work(double *nums, unsigned int choice);
 - int main()
 - {
 - unsigned int choice = 1;
 - double *nums = NULL; //pointer dikosongkan terlebih dahulu
 - char yn = 'y';
 - cout << endl
 - << endl
 - << "\t\t\tMean, Median, Mode\n\n\n";
 - cout << "berapa banyak angka yang akan dicari rata-ratanya? ";
 - cin >> choice;
 - unsigned int temp = choice;
 - if (choice < 2) /*jika angka yang diinput lebih kecil dari 2, maka tidak bisa cari average*/
 - {
 - cout << endl
 - << "Error: masukkan minimal 2 angka." << endl;
 - exit(0);
 - /*berfungsi untuk keluar dari program, dimana kita memberikan nilai 0, misalnya exit(0), pernyataan exit dapat dijalankan apabila kita menambahkan header stdlib.h */
 - }
 - if (!(nums = new double[choice])) //pointernya dialamatkan ke new double array
 - { // jika pointernya tidak bisa masuk ke memori maka programnya keluar
 - cout << endl
 - << "alokasi memori gagal.";
 - exit(1);
 - }
 - for (int x = 0; temp > 0; x++)
 - {
 - cout << "\n\n input nilai untuk cari averagenya (satu persatu): ";
 - cin >> nums[x];
 - temp--;
 - }
 - cout << "\n\nThanks.\n\n";
 - sort(nums, choice);
 - cout << endl
 - << endl
 - << " \t\t\t Mean, Median, Mode v1 \n\n";
 - cout << "Mean:\t\t" << avg(nums, choice) << "\n";
 - cout << "Median:\t\t" << median(nums, choice) << "\n";
 - mode(nums, choice);
 - cout << "Range:\t\t" << range(nums, choice) << "\n\n";
 - work(nums, choice);
 - delete[] nums;
 - cout << "\n\nmau lakukan lagi gak?(y or n): ";
 - cin >> yn;
 - if (yn == 'y' || yn == 'Y')
 - {
 - main();
 - }
 - return 0;
 - }
 - //Tahap Penulisan Program Paragraf Rata-Rata
 - double avg(double *nums, unsigned int choice)
 - {
 - unsigned int y = 0;
 - double x;
 - for (x = 0; choice > y; y++)
 - {
 - x += nums[y];
 - }
 - return (x / choice); //dibagi berapa banyak angka yang dimasukkan
 - }
 - //Tahap Penulisan Program Paragraf Median
 - double median(double *nums, unsigned int choice)
 - {
 - double n = 0;
 - long hold = 0;
 - long other = 0;
 - double holder = 0;
 - double m = 0;
 - if (choice % 2) //jika angkanya ganjil maka angka yang tengah adalah median
 - {
 - n = (choice / 2); //cari angka tengahnya berapa
 - n += .5; //menambahkan 0.5
 - holder = nums[hold]; //holder = the # disimpan di nums[hold]
 - }
 - else
 - {
 - hold = (choice / 2); //kalau nilai choice genap maka nilai hold=angka tengah yang paling besar
 - other = (hold - 1); //other adalah angka tengah yang paling kecil
 - n = nums[hold]; //n itu menyimpan nilainya hold
 - m = nums[other]; //m itu menyimpan nilainya other
 - holder = ((n + m) / 2); //menghitung hasil rata-ratanya
 - }
 - return holder; //mengembalikan nilai average
 - }
 - //Tahap Penulisan Program untuk fungsi pengurutan
 - double sort(double *nums, unsigned int choice)
 - {
 - unsigned int n = 0, k = 0;
 - double swap = 0;
 - for (n = choice - 1; n > 0; n--)
 - {
 - for (k = 0; k < n; k++)
 - {
 - if (nums[k] > nums[k + 1])
 - {
 - swap = nums[k];
 - nums[k] = nums[k + 1];
 - nums[k + 1] = swap;
 - }
 - }
 - }
 - return *nums;
 - }
 - //Tahap Penulisan program fungsi mode
 - void mode(double *nums, unsigned int choice)
 - {
 - unsigned int index;
 - int count = 0;
 - int max = 0;
 - for (index = 0; index < choice; index++)
 - {
 - if (nums[index] == nums[index + 1])
 - count++;
 - else
 - {
 - if (count > max)
 - max = count;
 - count = 0;
 - }
 - }
 - count = 0;
 - for (index = 0; index < choice; index++)
 - {
 - if (nums[index] == nums[index + 1])
 - count++;
 - else
 - {
 - if (count == max)
 - cout << "Mode(s):\t" << nums[index] << "\n";
 - count = 0;
 - }
 - }
 - }
 - double range(double *nums, unsigned int choice)
 - {
 - double range = (nums[choice - 1] - nums[0]); //range = angka yang paling besar kurangin sama angka yang paling kecil
 - return range;
 - }
 - //Tahap Penulisan Program untuk fungsi work
 - void work(double *nums, unsigned int choice)
 - {
 - cout << "\nWork:\n";
 - for (unsigned int x = 0; x < choice; x++)
 - {
 - if (x != choice - 1)
 - {
 - if (x == 0)
 - {
 - cout << "\t";
 - }
 - if (x % 5 == 0)
 - cout << endl
 - << "\t";
 - cout << nums[x] << ", ";
 - }
 - if (x == choice - 1)
 - {
 - if (x % 5 == 0)
 - cout << endl
 - << "\t";
 - cout << nums[x] << ".";
 - }
 - }
 - cout << "\n\nRange\nWork:";
 - cout << " " << nums[choice - 1] << " - " << nums[0] << " = " << nums[choice - 1] - nums[0] << "\n";
 - }
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment