E-book How to Solve
Assalamualaikam wr. wb.Untuk postingan kali ini, saya akan memberikan contoh program sederhana menentukan nilai huruf menggunakan ARRAY yang merupakan dari bagian tugas E-book How to solve (Kasus 4.2).
Oke langsung saja ke Algoritma dan kodingan dibawah ini :
Algoritma :
Deklarasi banyak,i,x
array int nilai [20]
deklarasi string nama
inputkan banyak mahasiswa
for (int i=1;i<=banyak;i++){
input nama;
input nilai mahasiswa i
if (nilai[20]>=80 || nilai[20]<=100)
ceta nilai = A
else if (nilai[20]>=60 || nilai[20]<80)
cetak nilai = B
else if (nilai[20]>=40 || nilai[20]<60)
cetak nilai = C
else if (nilai[20]>=20 || nilai[20]<40)
cetak nilai = D
else if (nilai[20]>=0 || nilai[20]<20)
cetak nilai = E
end for
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int banyak, i,x;
int nilai[20];
string nama;
cout<<"Masukan Banyak Mahasiswa :"; cin>>banyak;
for (int i=1;i<=banyak;i++){
cout<<"nama Mahasiswa :";cin>>nama;
cout<<"Masukan nilai :";cin>>nilai[20];
if (nilai[20]>=80 || nilai[20]<=100){
cout<<"\n Nilai "<<nilai[20]<<"Adalah A";
}
else if (nilai[20]>=60 || nilai[20]<80){
cout<<"\n Nilai "<<nilai[20]<<"Adalah B";
}
else if (nilai[20]>=40 || nilai[20]<60){
cout<<"\n Nilai "<<nilai[20]<<"Adalah C";
}
else if (nilai[20]>=20 || nilai[20]<40){
cout<<"\n Nilai "<<nilai[20]<<"Adalah D";
}
else if (nilai[20]>=0 || nilai[20]<20){
cout<<"\n Nilai "<<nilai[20]<<"Adalah E";
}
cout<<"\n========================================================================\n";
}
return 0;
}
Berikut ini hasil dari compile kodingan diatas :
Sedangkan Kasus 4.5,berikut kodingan dibawah ini :
4.5.1
#include <iostream>
#include <stdio.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
int n, *arr, onee = 0, twoo, threee, total = 0, maxx = -1, temp_maxx;
cin >> n;
arr = new int[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
total += arr[i];
}
// O(n^2) is the following
for (int i = 1; i < n - 1; i++) {
onee += arr[i - 1];
twoo = 0;
for (int j = i + 1; j < n; j++) {
twoo += arr[j - 1];
threee = total - twoo - onee;
temp_maxx = max(max(onee, twoo), threee);
if ((temp_maxx < maxx) || (maxx == -1))
maxx = temp_maxx;
}
}
cout << maxx;
return 0;
}
4.5.2
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//Bucket Sort
void bucket_sort (int arr[], int n)
{
//Here range is [1,100]
int m = 101;
//Create m empty buckets
int buckets[m];
//Intialize all buckets to 0
for (int i = 0; i < m; ++i)
buckets[i] = 0;
//Increment the number of times each element is present in the input
//array. Insert them in the buckets
for (int i = 0; i < n; ++i)
++buckets[arr[i]];
//Sort using insertion sort and concatenate
for (int i = 0, j = 0; j < m; ++j)
for (int k = buckets[j]; k > 0; --k)
arr[i++] = j;
}
//Driver function to test above function
int main()
{
int input_ar[] = {10, 24, 22, 62, 1, 50, 100, 75, 2, 3};
int n = sizeof (input_ar) / sizeof (input_ar[0]);
bucket_sort (input_ar, n);
cout << "Sorted Array : " << endl;
for (int i = 0; i < n; ++i)
cout << input_ar[i] << " ";
return 0;
}
Demikian postingan singkat dari saya semoga anda tidak pusing,terima kasih.
Wassalamualaikum wr. wb.
Untuk lanjutan dari E-book how to solve dengan kasus lain bisa kelink dibawah ini :
Fahri Ardianto 4.3 dan 4.4 Klik disini
Marzota Dwi Rahman 4.1 dan 4.6 Klik disini
0 komentar: