Вы находитесь на странице: 1из 5

PRAKTIKUM VI

ARRAY DAN FUNGSI

TUJUAN

● Memahami konsep array


● Memahami manfaat array
● Dapat menggunakan array pada proses pengurutan
● Dapat menggunakan array pada proses string
● Dapat menggunakan array pada proses matrix

TEORI PENDAHULUAN

TUGAS DAN PERCOBAAN

Program 1.

#include <iostream>
# include <conio.h>

using namespace std;

void tampilkan_array(int X[],int N);


void bubble_sort(int X[10],int N);

int main()
{
const int r=5; // size of array A
int a[r],b[c],i;
// Enter Details of the first sorted array
cout<<" Enter the first sorted array ==> "<<endl;
for(i=0;i<r;i++)
{
cout<<" Element "<<i+1<<":";
cin>>a[i];
b[i]=a[i];
}

tampilkan_array(a,5);
bubble_sort(a,5);
tampilkan_array(a,5);
return 0;
}

void tampilkan_array(int X[],int N)


{
cout<<endl; cout<<endl;
for (int i=0;i<N;i++) cout<<X[i]<<",";
}

void bubble_sort(int X[10],int N)


{
int i,j,temp;
for (i=0; i<N; i++)
{
for (j=0; j<N-1-i;j++)
{
if (X[j+1]<X[j]) /* Compare two neighboring elements */
{
temp = X[j]; /* Swap X[j] and X[j+1] */
X[j] = X[j+1];
X[j+1]=temp;
}
}
tampilkan_array(X,N);getch();
}
}

Program 2.
Ulangi program nomor 1 dengan menggunakan fungsi selection sort

Program 3.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

using namespace std;

int main()
{
char string[40];
int i,j=0,flag=0;
cout<<" Enter string";
gets(string);
j=strlen(string)-1;
for (i=0;i<strlen(string)-1;i++)
{
if (string[i] != string[j])
{
flag++;
}
j--;
}
if (flag==0)
{
cout<<" \n |Entered string is palindrome|";
}
else
cout<<" Entered string is not a palindrome";
return 0;
}

Program 4
#include <iostream>
# include<string.h>
# include<stdio.h>
# include<conio.h>

using namespace std;

int main()
{
char name[5][25];
int i;
cout<<"Enter the names with a maximum of 24 characters";
for(int i=0;i<5;i++)
gets(name[i]);
cout<<" \n The names stored are";
for (i=0;i<5;i++)
puts(name[i]);
getch();
return 0;
}

Program 5.
Buat program untuk menghitung jumlah huruf vokal dari input sebuah string dengan maximum panjang
string 250 karakter.

Program 6
// Header Files
# include<iostream.h>

using namespace std;

void display(int formal_2darray[3][3]);

int main()
{
int actual_2darray[3][3];
int i,j;
cout<<" Enter the elements of the 2 dimensional array:";
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cin>>actual_2darray[i][j];
}
}
//Passing 2d array to function
display(actual_2darray);
return 0;
}
/******************************
Name: display
Purpose: To display the 2d array in Matrix form
**************************************/
void display(int formal_2darray[3][3])
{
int i,j;
cout<<endl<<" Displaying the 2d array in Matrix Form... \n"<<endl;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
cout<<"\t"<<formal_2darray[i][j]<<" ";
}
cout<<endl;
}
}

Program 7.
# include <iostream.h>
# include <conio.h>
# include <iomanip.h>

using namespace std;

void swap(int e, int f);

int main()
{
int a,b;
//int trans[5][5];
cout<<"Enter the size of the 2-dimensional array A(Rows(<5) and Columns(<5))";
cin>>a>>b;
swap(a,b);
cout<<"Thank you for using the program ";
getch();
return 0;
}
/********************************
Name: swap
Parameters: int e, int f
Purpose: Transpose the matrix
********************************/
void swap( int e, int f)
{
int arr1[5][5],arr2[5][5];
int i,j,p,q;
// Transposing the array
cout<<" Enter array A";
for (p=0;p<e;p++)
for (q=0;q<f;q++)
cin>>arr1[p][q];
for (i=0;i<e;i++)
for (j=0;j<f;j++)
arr2[j][i]=arr1[i][j];
cout<<" Displaying Elements of A"<<endl;
for (i=0;i<e;i++)
{
for (j=0;j<f;j++)
cout<<setw(8)<<arr1[i][j];
cout<<endl;
}

cout<<" Transposed array A as follows \n\n";


for (j=0;j<f;j++)
{
for (i=0;i<e;i++)
cout<<setw(8)<<arr2[j][i];
cout<<endl;
}
}

Program 8.
Buat program untuk menghitung jumlah elemen diagonal kiri dan diagonal kanan untuk matrix 5 x 5

KESIMPULAN

Вам также может понравиться