0% menganggap dokumen ini bermanfaat (0 suara)
219 tayangan15 halaman

Contoh Program Array 1 Dimensi

Contoh program Array 1 Dimensi dalam tiga bahasa pemrograman yaitu C#, C++, dan Java. Program tersebut mendemonstrasikan pendeklarasian array 1 dimensi, pengisian data ke dalam array, dan menampilkan isi array. Program tersebut memberikan contoh dasar penggunaan array 1 dimensi dalam berbagai bahasa pemrograman.

Diunggah oleh

bapao
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai DOCX, PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
219 tayangan15 halaman

Contoh Program Array 1 Dimensi

Contoh program Array 1 Dimensi dalam tiga bahasa pemrograman yaitu C#, C++, dan Java. Program tersebut mendemonstrasikan pendeklarasian array 1 dimensi, pengisian data ke dalam array, dan menampilkan isi array. Program tersebut memberikan contoh dasar penggunaan array 1 dimensi dalam berbagai bahasa pemrograman.

Diunggah oleh

bapao
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai DOCX, PDF, TXT atau baca online di Scribd

Contoh Program Array 1 DIMENSI

ARRAY 1 DIMENSI
Array adalah sekelompok data sejenis yang disimpan ke dalam variabel dengan nama
yang sama, dengan memberi indeks pada variabel untuk membedakan antara yang
satu dengan yang lain.
Definisi Array 1 dimensi :
Array 1 Dimensi , array adalah hal yang paling penting dalam setiap bahasa
pemrograman. Menurut definisi, array adalah alokasi memori statis. Ini mengalokasikan
memori untuk tipe data yang sama secara berurutan. Ini berisi beberapa nilai jenis yang
sama. Hal ini juga menyimpan nilai-nilai dalam memori pada ukuran tetap.
Array juga mempunyai definisi lain yaitu struktur data yang statis yang mempunyai 1
nama tetapi memiliki banyak tempat. Setiap tempat harus dibedakan, untuk
membedakannya dibutuhkan penunjuk, pentunjuk dapat berupa karakter(char) atau
integer. Sekali disimpan dalam penunjuk yang sama(berbentuk indeks), maka isinya
tidak akan hilang kecuali indeksnya diisi oleh nilai yang lain.
Cara pendeklarasian Array setiap bahasa Program berbeda tapi semuanya memiliki
karakter yang sama . . .
deklarasi di csharp dan java:
tipedata [] nama variabel = new nama variabel [jumlahElement]
kalo di C++
tipedata namaVariabel [jumlahElemen];

INI ADALAH CONTOH PROGRAMNYA YANG MENGGUNAKAN C#


1
2
3
4
5
6
7
8
9
10
11
12
13

namespace Array1Dimensi
{
class Program
{
public byte banyak; //property
public static void Main(string[] args)
{
Arr arre = new Arr(); //deklarasi Class Arr
[Link](); //panggil method Masuk dr class Arr
[Link]();
[Link]("Press any key to continue . . . ");
[Link](true);

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

}
}
class Arr
{
public void Masuk()
{
Program pro = new Program(); //deklarasi Class Program
string [] nama; //deklarasi Array

[Link]("masukkan banyak elemen array = ");


[Link] = [Link]([Link]()); //input mengunakan property Class p
[Link]();
nama = new string[[Link]]; //inisialisasi array
//mengisi Array
for (byte i = 0; i<[Link]; i++)
{
[Link]("Masukkan nama mahasiswa ke {0} = ",i+1);
nama[i] = [Link]();
}
[Link]();
//menampilkan Array
for (byte i = 0; i<[Link]; i++)
{
[Link]("Nama maha siswa ke {0} adalah = {1}",i+1,nama[i]);
}
}
}
}

SEKARANG KITA LIAT HASILNYA SEKARANG BAGAIMANA YAA

KITA COBA BUAT PROGRAM INI DIJADIKAN C++ YA..


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

#include <cstdlib>
#include <iostream>
using namespace std;
class Arr
{
private : int banyak; //property
public : void Masuk()
{
cout<<"masukkan banyak elemen array = ";
cin>>banyak;
cout<<endl;
string nama[banyak]; //deklarasi dan inisialisasi array
//mengisi array
for (int i = 0; i<banyak; i++)
{
cout<<"Masukkan nama mahasiswa ke "<<i+1<<" = ";
cin>>nama[i];
}
cout<<endl;
//menampilkan isi array
for (int i = 0; i<banyak; i++)
{
cout<<"Nama mahasiswa ke "<<i+1<<" adalah "<<nama[i]<<endl;
}
}
};
int main(int argc, char *argv[])
{
Arr arre;
//deklarasi Class Arr
[Link]();
//panggil Method Masuk dari Class Arr

32
33
34
35
36
37
38
39
40

cout<<endl;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}

NAH, INI DIA HASILNYA HEHEHEE

NGGAK LENGKAP RASANYA KALO NGGAK PAKEK BAHASA


PROGRAM JAVA YA,KITA BUAT SEKARANG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

package javaapplication2;
import [Link];
public class Main
{
public byte banyak;

//property

public static void main(String[] args)


{
Arr arre = new Arr(); //deklarasi Class Arr
[Link](); //panggil method Masuk dr class Arr
}
}
class Arr
{
public void Masuk()
{
Scanner baca = new Scanner([Link]);
String [] nama; //deklarasi Array
Main pro = new Main(); //deklarasi Class Program

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

[Link]("masukkan banyak elemen array = ");


[Link] = [Link](); //input mengunakan property Class program
nama = new String[[Link]]; //inisialisasi Array
[Link]();
//mengisi Array
for (byte i = 0; i<[Link]; i++)
{
int s = i+1;
[Link]("masukkan nama maha siswa ke "+s+" = ");
nama[i] = [Link]();
}
[Link]();
//menampilkan Array
for (byte i = 0; i<[Link]; i++)
{
int s = i+1;
[Link]("nama maha siswa ke "+s+" adalah "+nama[i]);
}
}
}

MAKA,DAPAT DILIHAT HASILNYA AKAN SEPERTI INI

NAH, SEKARANG SAYA AKAN MENAMBAHKAN CONTOH YANG KE-2


TENTANG ARRAY 1 DIMENSI KE DALAM 3 BAHSA PEMOGRAMAN
YAITU: C# ,C++, DAN JAVA

INI ADALAH CONTOH PROGRAM YANG SAYA APLIKASIKAN KE


DALAM BAHASA C#, KITA COBA SIMAK BARENG-BARENG YAA
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

using System;
namespace Arr_1_dimensi
{
class Program
{
private static byte banyak()
{
byte data;
[Link]("Banyak Elemen Array = ");
data=[Link]([Link]());
return data;
}
private static byte [] arr(byte a)
{
byte[] data=new byte[a];
for (int i = 0; i < a; i++) {
[Link]("Nilai Data Elemen ke [{0}] = ", i);
data[i]=[Link]([Link]());
}
return data;
}
private static void tampil(byte []a, byte b)
{
for (int i = 0; i < b; i++) {
[Link]("Data ke-{0}= {1}",i,a[i]);
}
}
public static void Main(string[] args)
{
byte data;
data=banyak();
byte [] arraydata=new byte[data];
arraydata=arr(data);
tampil(arraydata,data);
[Link]("Press any key to continue . . . ");
[Link](true);
}
}
}

SEKARANG BIASA DI LIHAT BUKAN, BEGINILAH HASILNYA ^_^

WKTUNYA SEKARANAG KITA COBA MENGGUNAKAN BAHASA


PEMOGRAMAN C++ PERHATIKAN Y
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

#include <cstdlib>
#include <iostream>
using namespace std;
int banyak()
{
int data;
cout<<"Banyak Elemen Array = ";
cin>>data;
return data;
}
int* arr(int a)
{
int* data=new int[a];
for(int i=0;i<a;i++)
{
cout<<"Nilai Data Elemen ke {"<<i<<"} = ";
cin>>data[i];
}
return data;
}
void tampil(int a[],int b)
{
for (int i=0;i<b;i++)
{
cout<<"Data ke-{"<<i<<"} = ";
cout<<a[i];
cout<<endl;
}
}
int main(int argc, char *argv[])
{
int data;
data=banyak();
int* arraydata=new int[data];
arraydata=arr(data);

31
32
33
34
35
36
37
38
39
40

tampil(arraydata,data);
system("PAUSE");
return EXIT_SUCCESS;
}

MAKA HASILNYA AKAN SEPERTI INI.

YANG TERAKHIR INI ADALAH CONTOH PROGRAM YANG TELAH DI


APLIKASIKAN KE DALAN BAHASA PEMOGRAMAN JAVA. DI SIMAK
DULU YUKKS. :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

package javaapplication2;

import [Link]
import [Link];
public class Main {
public static Scanner Input=new Scanner([Link]);
static byte banyak()
{
byte data;
[Link]("Banyak Elemen Array = ");
data=[Link]();
return data;
}
static byte [] arr(byte a)
{
byte []data=new byte[a];
for (int i = 0; i < a; i++)
{
[Link]("Nilai Data Elemen ke {"+ i +"} = ");

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

data[i]=[Link]();
}
return data;
}
static void tampil(byte []a,byte b)
{
for (int i = 0; i < b; i++)
{
[Link]("Data ke-{"+i+"} = " +a[i]);
}
}
public static void main(String[] args) {
byte data;
data=banyak();
byte[]nilai=new byte[data];
nilai=arr(data);
tampil(nilai, data);
}
}

HASILNYA SEPERTI INI.

contoh program BlueJ sederhana


1).Persegi Panjang
import [Link];

public class persegi_panjang{


public static void main(String [] args){
int panjang,lebar,luas,keliling;
Scanner S = new Scanner ([Link]);
[Link]("masukan panjang=");
panjang=[Link]();
[Link]("masukan lebar=");
lebar=[Link]();
luas=(panjang*lebar);
keliling=2*(panjang+lebar);
[Link]("luas persegi panjang adalah="+luas);
[Link]("keliling persegi panjang adalah="+keliling);
}
}
Out put
masukan panjang=54
masukan lebar=96
luas persegi panjang adalah=5184
keliling persegi panjang adalah=300
2)Bujur Sangkar
import [Link];
public class Bujur_Sangkar {
public static void main(String []args){
int sisi,luas,keliling;
Scanner O=new Scanner ([Link]);
[Link]("masukan sisi=");
sisi=[Link]();
luas=(sisi*sisi);
[Link]("luas dari Bujur Sangkar adalah="+luas);
keliling=(4*sisi);
[Link]("luas dari Bujur Sangkar adalah="+keliling);
}
}
Out put
masukan sisi=78
luas dari Bujur Sangkar adalah=6084
luas dari Bujur Sangkar adalah=312
3)Segitiga
import [Link];
public class Segitiga{
public static void main(String[]args){
int alas,tinggi,luas;

Scanner F=new Scanner([Link]);


[Link]("input tinggi=");
tinggi=[Link]();
[Link]("input alas=");
alas=[Link]();
luas=(alas*tinggi)/2;
[Link]("luas segitiga="+luas);
}
}
Out put
input tinggi=589
input alas=366
luas segitiga=107787
4)Lingkaran
import [Link];
public class lingkaran {
public static void main(String [] args){
double phi=3.14;
double r,luas,keliling;
Scanner I=new Scanner ([Link]);
[Link]("jari-jari lingkaran=");
r=[Link]();
luas=phi*r*r;
keliling=2*phi*r;
[Link]("luas lingkaran="+luas);
[Link]("keliling="+keliling);
}
}
Out put
jari-jari lingkaran=777
luas lingkaran=1895709.06
keliling=4879.56
5).Karakter
class cahar{
public static void main(String[]arags){
int three=3;
char one='1';
char four=(char)(three+one);
[Link]("nilai variabel three="+three);
[Link]("nilai variabel one="+one);
[Link]("nilai variabel four="+four);
}

}
Out put
nilai variabel three=3
nilai variabel one=1
nilai variabel four=4
6).Logika
class logika{
public static void main(String []args){
boolean A=true;
boolean B=false;
boolean C= A & B;
boolean D= A | B ;
[Link]("logika A="+A);
[Link]("logika B="+B);
[Link]("logika C="+C);
[Link]("logika D="+D);
}
}
Out put
logika A=true
logika B=false
logika C=false
logika D=true
7).Class Operator Penugasan
class operator_penugasan{
public static void main(String []args){
int var = 1;
int a,b,c;
a=b=c=99;
[Link]("nilai variabel adalah="+var);
[Link]("nilai A adalah="+a);
[Link]("nilai B adalah="+b);
[Link]("nilai C adalah="+c);
}
}
Out put
nilai variabel adalah=1
nilai A adalah=99
nilai B adalah=99
nilai C adalah=99
8). Matematika

class matematika{
public static void main(String[]args){
int x=10;
int y=2;
char z='a';
int a=z*10;
int b=x+y;
int c=x-y;
int d=x/y;
int e=x % y;
[Link]("hasil A="+a);
[Link]("hasil B="+b);
[Link]("hasil C="+c);
[Link]("hasil D="+d);
[Link]("hasil E="+e);
}
}
Out put
hasil A=970
hasil B=12
hasil C=8
hasil D=5
hasil E=0
9).penaikan
class penaikan{
public static void main(String [] args){
int x=10;
int y,z;
[Link]("nilai x="+x);
[Link]("nilai y="+ ++x);
[Link]("nilai z= "+x++);
}
}
Out put
nilai x=10
nilai y=11
nilai z= 11
10).operator logika
class operatorLogika{
public static void main(String[]args){
boolean A=true;
boolean B=false;

boolean C=A & B;


boolean D=A ^ B;
[Link]("hasil A="+A);
[Link]("hasil B="+B);
[Link]("hasil C="+C);
[Link]("hasil D="+D);
Out put
hasil A=true
hasil B=false
hasil C=false
hasil D=true
11).class penurnan
class penurunan{
public static void main (String []args){
int x=15;
int y,z;
[Link]("nilai x="+x);
[Link]("nilai y="+ --x);
[Link]("nilai z="+x--);
}
}
Out put
nilai x=15
nilai y=14
nilai z=14
12).class relasional
class relasional{
public static void main(String []args){
int x;
if ('x'>10);
{
x=0;
}
[Link]("hasil x="+x);
}
}
Out put
hasil x=0
13).class majemuk
class majemuk{
public static void main(String[]args){

int a=5;
int b=2;
int c=3;
a+=5;
b*=4;
c+=a*b;
c%=b*a;
[Link]("hasil A="+a);
[Link]("hasil B="+b);
[Link]("hasil C="+c);
}
}
Out put
hasil A=10
hasil B=8
hasil C=3

Common questions

Didukung oleh AI

In C#, a one-dimensional array is declared using the syntax 'tipedata[] namaVariabel = new tipedata[jumlahElement]', which explicitly indicates the data type array and memory allocation. C++ simplifies this with 'tipedata namaVariabel[jumlahElemen]', assuming direct memory allocation for the specified elements during declaration. Java combines elements from both with 'tipo de dato[] nombreVariable = new tipo de dato[tamaño]', requiring explicit mention of the data type for array elements and allocation size post-declaration. The core similarity is their array index mechanism but differ in syntax specifics and the integration of array handling features, such as memory management models and convenience functions provided by each language .

Static methods play a crucial role in handling array operations in Java by encapsulating functionality that doesn't rely on instance variables. This structuring allows methods such as array initialization, insertion, and iteration to be executed within a class context without creating an instance. It enables convenient access to the methods by using the class name directly, promoting reusable code structures and ensuring that the operations are consistent and not dependent on the state of specific object instances. Static methods aid in modular programming, making the code easier to read, test, and maintain .

Type safety in strongly typed languages like C# and Java is crucial in guaranteeing that operations on array elements respect data type constraints. This reduces runtime errors by ensuring that each element inserted into the array is of a compatible data type, preventing illegal assignments. This safety feature streamlines debugging processes and enhances code stability and reliability. For instance, in both C# and Java, trying to assign an incompatible data type to an array element will lead to a compile-time error, protecting against runtime failures and unexpected behavior, thus preserving consistent program execution .

Loops in C++ are fundamental for performing repeated operations like inserting array elements and displaying array contents. A 'for' loop is typically used for such operations due to its concise syntax and clear iteration control, which is essential for accessing array elements by their indices. For insertion, the loop runs for the total number of elements, accepting input and storing it at the respective index. Similarly, for display, the loop retrieves and prints each value at the corresponding index. This use of loops optimizes operations by automating repetitive tasks, ensuring efficient data handling and reducing coding redundancy .

Input handling for array initialization shows syntactical differences across C#, C++, and Java due to the unique programming constructs of each language. In C#, Console.ReadLine() is used to capture input and Convert.ToByte() is employed to convert the string input to the appropriate byte data type. In C++, cin is utilized for input, where data is directly stored in the target variable type. Java uses the Scanner class with methods like nextByte() to capture and convert input into the required data type. These differences highlight how each language provides distinct mechanisms while maintaining the core logic of reading and storing input in arrays .

In geometry-related program calculations, particularly for rectangles and squares, arrays can be used to store various dimensions, such as lengths and widths, collected from user inputs. This is especially useful when calculating areas and perimeters of multiple shapes, as the array can maintain a list of values to iterate over and compute respective results efficiently. Input dimensions are stored in arrays, and loops are used to access these values for calculations. For example, the dimensions of a rectangle could be stored in an integer array [length, width], which simplifies the calculation of area (length * width) and perimeter (2*(length + width)). This approach centralizes data input and calculation logic, minimizing repeated code .

Conceptually, C++ treats one-dimensional arrays more as direct memory manipulation with pointers and static memory allocation. This approach provides high control but demands explicit memory management to prevent issues like memory leaks and buffer overflows. Conversely, Java abstracts much of this complexity through dynamic memory management and garbage collection, promoting safety and ease of use at the cost of lower control over memory resources. Java's objects and reference-based approach reduce direct pointer manipulation risks, offering a more secure environment for array operations, which is ideal for rapid application development .

A one-dimensional array is defined as a static memory allocation structure that stores multiple values of the same type in a sequential manner. This memory allocation implies that once the size of the array is set, it cannot be altered, which ensures fixed memory usage and predictable access times. In programming languages such as C#, C++, and Java, the syntax may differ, but the underlying concept remains the same, utilizing a contiguous block of memory that is accessed using an index. For example, in C#, an array is declared as 'tipedata[] namaVariabel = new tipedata[jumlahElement]', whereas in C++, it's 'tipedata namaVariabel[jumlahElemen]'. This implies that once data is stored at an index within the array, it remains until it is explicitly replaced, ensuring data persistence within the program's lifecycle .

Array indices significantly enhance data storage and retrieval efficiency in one-dimensional arrays by providing direct access to any element using its index position. This minimizes data retrieval time to constant O(1) time complexity, which is ideal for scenarios requiring fast access speeds. Data storage is orderly and contiguous, leading to predictable access patterns that optimize performance. However, this comes at the cost of inflexibility in memory usage, as the array size is fixed, and inefficient for insertion and deletion operations except at the end. Therefore, while indices permit efficient navigation within static bounds, they also establish limits on dynamic data handling .

Arrays are pivotal in iterative computations for scientific and mathematical tasks due to their ability to store large datasets that need to be processed repetitively. They allow for efficient manipulation and retrieval of numerical data, which is essential in large-scale simulations or mathematical modelings, such as matrix operations or signal processing. The contiguous memory storage and index accessibility minimize computational overhead, enabling swift traversals and modifications. Arrays support structured data handling for loops, allowing for systematic application of functions over datasets, ensuring both performance and versatility in solving complex problems .

Anda mungkin juga menyukai