0% found this document useful (0 votes)
47 views8 pages

Nama: Dimas Hendrico NIM: 21/475218/PA/20551 Tugas 5 1. Program Rata-Rata Data Dalam Array

The document contains 5 C++ programs written by Dimas Hendrico (ID 21/475218/PA/20551) for Assignment 5. The programs include: 1) A program to calculate the average of integers in an array 2) A program to convert a decimal number to binary 3) A program to find the intersection of elements between two arrays 4) A program to multiply two matrices 5) A program to find the smallest missing positive integer

Uploaded by

Dimas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Nama: Dimas Hendrico NIM: 21/475218/PA/20551 Tugas 5 1. Program Rata-Rata Data Dalam Array

The document contains 5 C++ programs written by Dimas Hendrico (ID 21/475218/PA/20551) for Assignment 5. The programs include: 1) A program to calculate the average of integers in an array 2) A program to convert a decimal number to binary 3) A program to find the intersection of elements between two arrays 4) A program to multiply two matrices 5) A program to find the smallest missing positive integer

Uploaded by

Dimas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Nama : Dimas Hendrico

NIM : 21/475218/PA/20551

Tugas 5

1. Program rata-rata data dalam array

#include <iostream>

using namespace std;

int main(){
int n,nArray[10000];
float sumtemp;
sumtemp = 0;
cout << "How many integers? : ";
cin >> n;
cout << "Write all the integers : ";
for(int i=0;i<n;i++){
cin >> nArray[i];
sumtemp += nArray[i];
}
cout << "The average is : " << sumtemp/n;
return 0;
}
Berikut outputnya
2. Program konversi bilangan desimal ke biner

#include <iostream>

using namespace std;

int main(){
int bArray[10], a, c, vartm;
cout << "Insert a number : ";
cin >> a;
vartm = a;
for(c=0;a>0;c++){
bArray[c] = a%2;
a = a/2;
}

cout << "The result of number " << vartm <<" in binary is :\n";
for(c=c-1;c>=0;c--){
cout << bArray[c];
}
return 0;
}
Berikut outputnya
3. Program untuk menentukan interseksi atau irisan pada dua array

#include <iostream>

using namespace std;

int main()
{
int z, a, b, aArray[555], bArray[555];
cout << "Insert length of Array : ";
cin >> z;
cout << "Enter element of Array a" << endl;
for (a = 1; a <= z; a++)
{
cout << "Enter element a" << a << " : ";
cin >> aArray[a];
}
cout << endl;
cout << "Enter element of Array b" << endl;
for (b = 1; b <= z; b++)
{
cout << "Enter element b" << b << " : ";
cin >> bArray[b];
}

cout << "Intersection :\n";


for (a = 1; a <= z; a++)
{
for (b = 1; b <= z; b++)
{
if (aArray[a] == bArray[b])
{
cout << aArray[a] << endl;
}
}
}
return 0;
}
Berikut outputnya
No 4-5 di halaman setelah ini
4. Program perkalian dua buah matriks

#include <iostream>

using namespace std;

int main()
{
int A[4][4],B[4][4],m1,m2,m3,m4,a,b;
cout << "Enter elements for matrix A : "<<endl;
for(a=1;a<=2;a++){
for(b=1;b<=2;b++){
cout << "Enter element of a" <<a<<b<< " : ";
cin>> A[a][b];
}
}
cout << endl;
cout << "Enter elements for matrix B : "<<endl;
for(a=1;a<=2;a++){
for(b=1;b<=2;b++){
cout << "Enter element of b"<<a<<b<<" : ";
cin>> B[a][b];
}
}
cout << endl;
cout << "Matriks A : \n"<<A[1][1] <<"\t"<< A[1][2] << endl <
< A[2][1] <<"\t"<<A[2][2]<<endl<<endl;
cout << "Matriks B : \n"<<B[1][1] <<"\t"<< B[1][2] << endl <
< B[2][1] <<"\t"<<B[2][2]<<endl<<endl;

m1 = A[1][1]*B[1][1] + A[1][2]*B[2][1];
m2 = A[1][1]*B[1][2] + A[1][2]*B[2][2];
m3 = A[2][1]*B[1][1] + A[2][2]*B[2][1];
m4 = A[2][1]*B[1][2] + A[2][2]*B[2][2];

cout <<"Multiplication between matrix A and B :\n"<<m1 <<"\t


"<<m2<<"\n"<<m3 <<"\t"<<m4<<endl;

return 0;
}
Berikut outputnya
5. Program untuk mencari bilangan bulat positif terkecil yang hilang

#include <iostream>

#include <algorithm>

using namespace std;

int main()
{
int a, b, aArray[100], bArray[100], tm, c, n, m, uu;
cout << "How many integers? : ";
cin >> a;
cout << "Write the integers separated by space : ";
for (b = 0; b < a; b++)
{
cin >> aArray[b];
}

for (int i = 0; i < a; i++)


{
if (aArray[i] > 0 && aArray[i] < 100)
{
bArray[i] = aArray[i];
}
}

sort(bArray, bArray+a);

int o = 1;
for (int y = 0; y < a; y++)
{
while (o == bArray[y] && bArray[y] > 0)
{
tm = o + 1;
o++;
}
}

cout << "The smallest missing positive integer is : ";


if(tm==2){
for(int z=0;z<a;z++){
if(bArray[z]==1){
uu = 1;
}
else{
uu = 2;
}
}
cout << uu;
}
else{
cout << tm;
}

return 0;
}
Berikut outputnya

You might also like