Jumat, 31 Mei 2013

Bahasa C++

 Program Mencari Modus Dengan C++

Berikut adalah sintax  Program Mencari Modus Dengan C++ selengkapnya  pada Borland C++ 5.02 :

  1. #include<iostream.h>
  2. #include<conio.h> 
  3. int x;
  4. void findmod(float bil[]int n, float mod[])
  5. {
  6. int total[100];
  7. int k=1;
  8. x=0;
  9. //untuk mengurutkan secara ascending
  10. for(int c=0;c<n;c++)
  11. {
  12. for(int i=(n-1);i>=0;i--)
  13. {
  14. if(bil[i]<bil[i-1])
  15. {
  16. int temp;
  17. temp=bil[i];
  18. bil[i]=bil[i-1];
  19. bil[i-1]=temp;
  20. }
  21. }
  22. }
  23. //menghitung berapa kali muncul tiap angka
  24. for(int c=0;c<n;c++)
  25. {
  26. total[c]=0;
  27. for(int i=0;i<n;i++)
  28. {
  29. if(bil[c]==bil[i])
  30. {
  31. total[c]++;
  32. }
  33. }
  34. }
  35. //menentukan nilai yang paling sering muncul
  36. for(int c=0;c<n;c++)
  37. {
  38. if(total[c]>k)
  39. {
  40. k=total[c];
  41. }
  42. }
  43. //jika modus lebih dari satu
  44. for(int c=0;c<n;c++)
  45. {
  46. if(x==0)
  47. mod[x]=0;
  48. else
  49. mod[x]=mod[x-1];
  50. if(total[c]==k)
  51. {
  52. if(bil[c]!=mod[x])
  53. {
  54. mod[x]=bil[c];
  55. x++;
  56. }
  57. }
  58. }

  59. //Jika Semua angka muncul sama banyak
  60. int z=0;
  61. for(int c=0;c<n;c++)
  62. {
  63. if(total[c]==k)
  64. {
  65. z++;
  66. }
  67. }
  68. if(z==n)
  69. {
  70. x=0;
  71. }
  72. }
  73. void main()
  74. {
  75. int n;
  76. float bil[100];
  77. float mod[100];
  78. cout<<"Banyak N : ";cin>>n;
  79. for(int c=0;c<n;c++)
  80. { 
  81. cout<<"Nilai "<<(c+1)<<" : ";cin>>bil[c];
  82. }
  83. cout<<endl;
  84. findmod(bil,n,mod);
  85. if (x==0)
  86. cout<<"Tidak Ada Modus!"<<endl;
  87. else
  88. {
  89. cout<<"Modus : ";
  90. for(int c=0;c<x;c++)
  91. {
  92. cout<<mod[c]<<" ";
  93. }
  94. }
  95. getch();
  96. }
Dan, hasil Outputnya adalah sebagai berikut:




 
      

Tidak ada komentar:

Posting Komentar