Array 1 Dimensi

· OOP1
Penulis

– Program C#

</pre>
using System;
namespace Function_Array
{
class Program
{
private static int[] InputArray(byte panjang)
{
int[] elemen= new int[panjang];

for (int i=0;i<panjang ;i++ ) {
Console.Write(“Masukkan array ke {0} : “,i)
elemen[i]=Convert.ToInt32(Console.ReadLine());
}
return elemen;
}
private static void TampilkanArray(int[] elemenArray)
{
for (int i=0;i<elemenArray.Length ;i++ ) {
Console.WriteLine(elemenArray[i]);
}
}
public static void Main(string[] args)
{
int[] nilai=new int[5];
nilai=InputArray(5);
Console.WriteLine(“Menampilkan nilai array”);
TampilkanArray(nilai);
Console.Write(“Press any key to continue . . . “);
Console.ReadKey(true);

}

}

}

Hasilnya;

Image

-Program C++

#include <iostream>

using namespace std;

class Program

{
public: void Input(int array[5],int panjang)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;
for (int i=0;i<panjang ;i++ )
{
cout<<“Masukkan array ke “<<(i)<<“: “;
cin >>array[i];
}
}
public : void Tampilkan(int array[5], int panjang)
{
for (int i=0;i<panjang ;i++ )
{
cout<<“Elemen ke- “<<(i)<<” : “;
cout<<array[i]<<endl;
}
}
};
int main()
{
Program hasil;
int array[5];
int a;
cout<<“Masukkan jumlah Array = “;
cin>>a;
hasil.Input(array,a);
cout<<endl;
cout<<“Tampilkan Array”<<endl;
hasil.Tampilkan(array,a);
return 0;
}</pre>

Hasilnya;

Image

-Program Java

package javaapplication14;

import java.util.Scanner;
class baru
{
public void Array()
{
Scanner masuk = new Scanner(System.in);
byte [] nilaiArray = new byte[5];

//menginputkan array

for (byte i=0;i<5;i++)
{
System.out.print(“Isi pada index ke{“+i+”} = “);
nilaiArray[i] = masuk.nextByte();
}
//menampilkan array
for (byte i=0;i<nilaiArray.length;i++)
{
System.out.println(“Index ke{“+i+”}=”+nilaiArray[i]);
}
}
public class Main {
public static void main(String[] args){

baru program = new baru();
program.Array();

}
}

Hasilnya:

Image

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: