Study

Manipulasi Array

Sedikit mengulang materi Algoritma dan Pemrograman tentang aritmatika array..lumayan bisa buat nambah postingan blog.. 🙂

Manipulasi disini maksudnya kita akan mencoba menambah data , mencari data, mencari data minimum, dan data maksimum

So, let’s check it out..

Menambah data

Untuk menambah data, yang kita perlu deklarasikan terlebih dahulu adalah seberapa banyak data dalam array. misalnya ada 10.  maka sourcenya akan seperti ini..

#include <iostream>
using namespace std;

int main()
{
	// definisikan terlebih dulu maksimum data dalam array
        const int max = 10;
	int number[max];

	// hitung jumlah semua data
       int sum = 0;

	cout << "Please type 10 integers.\n";

	for( int i = 0; i < max; i++ )
	{
		cout << "Number " << i + 1 << ": ";
		cin >> number[i];
		sum += number[i];
	}

	cout << "\n\nThe sum of these numbers is " << Sum << "\n\n";

	return 0;
}

Pencarian Data

Berbeda dengan penambahan data, untuk memudahkan isi daripada array sudah dideklarasikan terlebih dahulu..

#include <iostream>
using namespace std;

int main()
{
	// Declare the members of the array
	int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
	int find;
	int i, m = 8;

	cout << "Enter a number to search: ";
	cin >> find;

	for (i = 0; (i < m) && (Numbers[i] != Find); ++i)
		continue;

	// Find whether the number typed is a member of the array
	if (i == m)
		cout << find << " is not in the list" << endl;
	else
		cout << find << " is the " << i + 1
		        << "th element in the list" << endl;
	return 0;
}

Nilai Maksimum dan Minimum

Untuk mencari nilai maksimum dan minimum, logikanya adalah kita akan membandingkan data dalam array 1 per 1 hingga ditemukan nilai terbesar/terkecil.

// Example of finding the minimum member of an array
#include <iostream>
using namespace std;

int main()
{
	// The members of the array
	int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89};
	int minimum = numbers[0];
	int a = 8;

	// Compare the members
	for (int i = 1; i < a; ++i) {
		if (numbers[i] < minimum)
			minimum = numbers[i];
	}

	// Announce the result
	cout << "The lowest member value of the array is "
	       << minimum << "." << endl;

	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s