-
Notifications
You must be signed in to change notification settings - Fork 1
/
Soal 2.txt
253 lines (191 loc) · 7.06 KB
/
Soal 2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
**Tulis sebuah fungsi untuk memeriksa apakah suatu bilangan adalah hasil 2 pangkat. Contoh: Untuk masukan n = 4 keluarannya adalah 1 (betul), untuk n = 128 keluarannya adalah 1 (betul), tetapi untuk masukan n = 5 keluarannya adalah 0 (salah).
https://jsfiddle.net/rescenic/ybwzeudr/
<script>
// Fungsi untuk mengecek apakah
// x adalah hasil pangkat 2
function cekPangkatDua(n)
{
if (n == 0)
return false;
return parseInt( (Math.ceil((Math.log(n) / Math.log(2))))) == parseInt( (Math.floor(((Math.log(n) / Math.log(2))))));
}
// Test fungsi cekPangkatDua
document.write("Apakah 4 merupakan hasil dari pangkat 2? <br/>")
if (cekPangkatDua(4))
document.write("1 (betul)<br/>");
else
document.write("0 (salah)<br/>");
document.write("Apakah 128 merupakan hasil dari pangkat 2? <br/>")
if (cekPangkatDua(128))
document.write("1 (betul)<br/>");
else
document.write("0 (salah)<br/>");
document.write("Apakah 5 merupakan hasil dari pangkat 2? <br/>")
if (cekPangkatDua(5))
document.write("1 (betul)<br/>");
else
document.write("0 (salah)<br/>");
</script>
**Temukan angka terbesar kedua dalam sebuah array dari n angka. Contoh : Untuk n = 5 dan inputVector = [0, 12, 17, 3, 1], outputnya adalah 12.
https://jsfiddle.net/rescenic/bzmn4v8f/
<script>
// Mengecek elemen terbesar kedua
// Fungsi untuk mencetak elemen terbesar kedua
function print2largest(inputVector, inputVector_size) {
let n;
let largest = second = -2454635434;
// Minimal 2 elemen
if (inputVector_size < 2) {
document.write(" Invalid Input ");
return;
}
// Mencari elemen terbesar
for (n = 0;n<inputVector_size;n++){
if (inputVector[n]>largest){
largest = inputVector[n];
}
}
// Mencari elemen terbesar kedua
for (n = 0 ;n<inputVector_size;n++){
if (inputVector[n]>second && inputVector[n]<largest){
second = inputVector[n];
}
}
if (second == -2454635434){
document.write("Tidak ada elemen terbesar kedua<br>");
}
else{
document.write("Elemen terbesar kedua adalah " + second);
return;
}
}
// Cek Nilai Terbesar Kedua
let inputVector= [ 0, 12, 17, 3, 1 ];
let n = inputVector.length;
print2largest(inputVector, n);
</script>
**Temukan angka yang berulang dalam array. Contoh: Untuk n = 12 dan arr = [9, 31, 38, 5, 62, 44, 38, 17, 19, 38, 50, 74], outputnya adalah 38.
https://jsfiddle.net/rescenic/jsvbo4dc/
<script>
//Cek elemen yang berulang
function mostFrequent(arr, n)
{
// Sortir array
arr.sort();
// Cari frekuensi maks menggunakan linear traversal
let max_count = 1, res = arr[0];
let curr_count = 1;
for (let i = 1; i < n; i++)
{
if (arr[i] == arr[i - 1])
curr_count++;
else
{
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[i - 1];
}
curr_count = 1;
}
}
// Cek jika elemen terakhir itu paling sering
if (curr_count > max_count)
{
max_count = curr_count;
res = arr[n - 1];
}
return res;
}
// Tes fungsi cek elemen berulang
let arr = [9, 31, 38, 5, 62, 44, 38, 17, 19, 38, 50, 74];
let n = arr.length;
document.write(mostFrequent(arr,n));
</script>
**Lihat gambar berikut untuk pertanyaan, gunakan bahasa pemrograman typescript / php untuk menyelesaikannya
https://3v4l.org/HQvkK
<html>
<head>
<title><?php echo "Sekolah class";?></title>
</head>
<body>
<?php
class Sekolah {
public $kelas;
}
class Kelas extends Sekolah {
public $id_kelas;
public $guru;
public $siswa;
function __construct(){
$this->id_kelas = rand (1,6); //Kelas 1 s.d. Kelas 6
//echo $this->id_kelas . " adalah nomor id kelas. <br />";
}
}
class Guru extends Kelas {
public $mata_pelajaran;
}
class Siswa extends Kelas {
protected $id_siswa;
protected $nama_siswa;
protected $umur_siswa;
function __construct($nama_siswa){
$this->nama_siswa = $nama_siswa;
$this->id_siswa = rand (1, 20); //rentang id siswa
$this->umur_siswa = rand (7, 12); //rentang umur siswa
//echo $this->id_siswa . " adalah nomor id siswa. <br />";
}
function getnama(){
return $this->nama_siswa;
}
function getid(){
return $this->id_siswa;
}
function getumur(){
return $this->umur_siswa;
}
}
class Mata_Pelajaran {
public $mata_pelajaran;
}
class Grade {
public $grade;
}
$kelas_1 = new Kelas();
$kelas_2 = new Kelas();
$siswa_1 = new Siswa("Ridwan");
$siswa_2 = new Siswa("Maulana");
$siswa_3 = new Siswa("Hakim");
$siswa_4 = new Siswa("Alice");
$siswa_5 = new Siswa("Bob");
$siswa_6 = new Siswa("Tandal");
echo "<br /> Siswa yang bernama " . $siswa_1->getnama() . " memiliki no. id " .$siswa_1->getid() . " dan berusia " . $siswa_1->getumur() . " tahun. Siswa tersebut masuk ke dalam kelas " . $kelas_1->id_kelas . ". <br/><br/>";
echo "<br /> Siswa yang bernama " . $siswa_2->getnama() . " memiliki no. id " .$siswa_2->getid() . " dan berusia " . $siswa_2->getumur() . " tahun. Siswa tersebut masuk ke dalam kelas " . $kelas_1->id_kelas . ". <br/><br/>";
echo "<br /> Siswa yang bernama " . $siswa_3->getnama() . " memiliki no. id " .$siswa_3->getid() . " dan berusia " . $siswa_3->getumur() . " tahun. Siswa tersebut masuk ke dalam kelas " . $kelas_1->id_kelas . ". <br/><br/>";
echo "<br /> Siswa yang bernama " . $siswa_4->getnama() . " memiliki no. id " .$siswa_4->getid() . " dan berusia " . $siswa_4->getumur() . " tahun. Siswa tersebut masuk ke dalam kelas " . $kelas_2->id_kelas . ". <br/><br/>";
echo "<br /> Siswa yang bernama " . $siswa_5->getnama() . " memiliki no. id " .$siswa_5->getid() . " dan berusia " . $siswa_5->getumur() . " tahun. Siswa tersebut masuk ke dalam kelas " . $kelas_2->id_kelas . ". <br/><br/>";
echo "<br /> Siswa yang bernama " . $siswa_6->getnama() . " memiliki no. id " .$siswa_6->getid() . " dan berusia " . $siswa_6->getumur() . " tahun. Siswa tersebut masuk ke dalam kelas " . $kelas_2->id_kelas . ". <br/><br/>";
echo "1. Mengurutkan siswa berdasarkan nilai rata-rata mereka: <br/>";
$grade = array("Ridwan"=>"5", "Maulana"=>"2.5", "Hakim"=>"4.5", "Alice"=>"1.5", "Bob"=>"2", "Tandai"=>"3.5");
asort($grade);
foreach($grade as $x => $x_value) {
echo $x . " - " . $x_value;
echo "<br>";
}
echo "<br/> 2. Mata pelajaran yang diurutkan berdasarkan rata-rata nilai yang diberikan kepada siswa: <br/>";
$mata_pelajaran = array("Bahasa Inggris"=>"2.95", "Sejarah"=>"1.5", "Fisika"=>"2.5", "Matematika"=>"3", "Bahasa Jerman"=>"4", "Olahraga"=>"5");
arsort($mata_pelajaran);
foreach($mata_pelajaran as $x => $x_value) {
echo $x . " - " . $x_value;
echo "<br>";
}
echo "<br/> 3. Kelas yang diurutkan dengan siswa terbaik: <br/>";
$bestclass = array("4. A"=>"2.5", "1. C"=>"3", "2. B"=>"2");
asort($bestclass);
foreach($bestclass as $x => $x_value) {
echo $x . " - " . $x_value;
echo "<br>";
}
?>
</body>
</html>