Week 8, 9 & 10

8th, 9th and 10th Week(1st May 2013)

Hi and long time no see! For week 8 and 9 (and 10, but we have a week off now), we’ve learnt about “Array”. Well, I tried to write about Arrays for week 8 but I felt that it is too little for me to post so I just combined the reflections for week 8, 9 and 10 into 1 entry focusing on ‘Array’. In this entry, I will describe the fundamentals of “Array” as best as I can as a beginner, then I will include some codes of programs I’ve made as examples of an ‘Array’ function.

First of all, what is an ‘array’? Simply put, an array is a group of data of the same type arranged in a systematic order or sequence. This group of data is often identified by identified by 1 name; a subscript or an index. For example; No[0], No[1], No[2] … , the varying values in  between  the square brackets is like an  address to where the actual value (keyed in by the user) is stored.

However, even ‘arrays’ need to be declared using a format like No[5]; where ‘No’ is the name of the array and the value in between the square brackets is the sample size of your array. Your array will have many sizes and you can fix the size by inserting a value. You may also leave it blank but it still yields the same result. (To be honest, I’ve never tried leaving it blank to see if it’s true but many books, tutorials and such say so…)

After you declare an array, you’d of course want to use it. But first, I’ll explain (as best as I can) about this funny thing an array does. So, based on the first example stated above; No[0], No[1], No[2]; well, it won’t come up in the code exactly like that but I just wanted to show how the value of the data keyed in by the user is stored. These data are called elements if you refer them individually in the place where they are stored. [0], [1], [2] and so on (depends on the programmer’s desired sample size) are the address to where the elements are stored. To make it simple, [0] is a box labelled as ‘0’ where the computer store the first element (actual value is set by user) , then, [1] is the box to store the second element and so on with the subsequent boxes (addresses).

That’s basically what an ‘array’ is. They (I don’t know who specifically, experienced programmers who wrote the relevant books, tutorials and such) say ‘array’ is one of the most sophisticated method for a programmers to create their programmes systematically. Therefore, we will now see examples of programmes I’ve made applying ‘arrays’ (all based on tutorial questions given in class).

#include <cstdlib>

#include <iostream>

#define N 2

/*

 

Nama: Nur Arina Bt Faizal      Program : Aturcara Untuk Memproses Mata Liga Sepakbola

No Matrik: UK25837             Amali : Minggu 8

 

Bina satu aturcara C++ yang berstruktur yang memproses

mata dalam permainan 30 pasukan dalam Liga Sepakbola Liga UMT. Data input terdiri dari:

nama pasukan, bilangan bermain,menang,kalah dan seri.

Contoh data:

Nama pasukan

Bilangan main

Bilangan menang

Bilangan kalah

Bilangan seri

Mata

 

FST

10 5 3 2 17

FPE

9 8 1 0 ?

…FMSM

10 3 7 0

 

Formula untuk mengira mata ialah

Mata = Bilangan_Menang*3 + Bilangan_Seri*1

Tindakan anda:

Aturcara anda menggunakan 6 tatasusunan 1-dim untuk menyimpan data

Aturcara anda akan mengira mata yang diperolehi oleh setiap pasukan.

Pamerkan satu jadual hasil dari pemprosesan aturcara anda.

Tarikh akhir hantaran: Minggu#9

 

Algoritma Umum;

Input: Inputkan data yang berikut;

a)Nama pasukan

b)Bilangan main

c)Bilangan menang

d)Bilangan kalah

e)Bilangan seri

Proses: Nilai mata dikira;

Mata = Bilangan_Menang*3 + Bilangan_Seri*1

 

Output: Pamerkan nilai mata untuk setiap pasukan; Mata

*/

 

 

using namespace std;

void BacaData(); // Pengisytiharaan fungsi

void KiraMata();

void PamerMata();

 

 

string namapas[N];

int bilmain[N];

int menang[N];

int kalah[N];

int seri[N];

int mata[N];

 

int i;

 

 

int main(int argc, char *argv[])

{

BacaData();   //Panggil fungsi 1

KiraMata();   //Panggil fungsi 2

PamerMata();  //Panggil fungsi 3

system(“PAUSE”);

return EXIT_SUCCESS;

}

 

void BacaData() // Pengenalan kepada aturcara

{

string nama,uk;

cout << “\n      ************ Amali Minggu 8 ************” << endl;

cout << ”    ******** Aturcara untuk Memproses Mata Liga Sepakbola ********” << endl;

cout << ”  ******* Nama : Nur Arina Bt Faizal Abdullah *******” << endl;

cout << “************** Matrik No : UK25837 *********************” << endl << endl << endl;

 

for (i=0; i<N; i++)

{

cout << “\nNama Pasukan:”;

cin >> namapas[i];

cout << “\nBilangan Bermain:”;

cin >> bilmain[i];

cout << “\nMenang:”;

cin >> menang[i];

cout << “\nKalah:”;

cin >> kalah[i];

cout << “\nSeri:”;

cin >> seri[i];

}

}

 

void KiraMata()

{

for (i=0; i<N; i++)

{

mata[i] = menang[i]*3 + seri[i]*1;

}

}

 

void PamerMata()

{

cout << “Nama Pasukan ” ;

cout << “Bilangan Bermain ” ;

cout << “Menang “;

cout << “Kalah “;

cout << ”    Seri “;

cout << ”    Mata ” << endl;

for (i=0; i<N; i++)

{

cout << namapas[i] << “\t\t”;

cout << bilmain[i] << “\t\t”;

cout << menang[i]<< “\t”;

cout << kalah[i]<< “\t”;

cout << seri[i]<< “\t”;

cout << mata[i] << endl;

}

}

Second Example;

 

#include <cstdlib>

#include <iostream>

/*

 

Nama: Nur Arina Bt Faizal   Program : Program To Calculate Parking Fees

No Matrik: UK25837          Tutorial Week 9

 

You have been given the following algorithm as a guide to write the program.

 

Problem analysis:

data input -> N time durations

data output -> N charges, average charge, highest charge, lowest charge, total charge

 

Algorithmic steps:

1.  Initial input process:

Read input data into array

 

2. Calculation process for all charges:

First hour charge (B1) = RM20.00

Next hours charge  (B2) = (time-1) X 10.00

Charge = B1 + B2

 

3. Display results:

Display time durations, all charges, highest charge, lowest charge & total chage.

End of Algorithmic steps

 

You are given the following C++ skeleton program to complete.

 

int N;  // number of data

// declarations of 2 arrays

int time[100];  // parking durations

float charges[100];  // parking charges

 

main()

{

ReadData();  // read data into time array

CalculateCharges();  // calculate charges & save in charges array

CalculateAverageCharge();  // calculate average charge

CalculateHighestCharge();  // calculate highest charge

CalculateLowestCharge();  // calculate lowest charge

CalculateTotalCharge();   // calculate total chages

DisplayCharges();   // display results

}

 

Write a full C++ program to implement the above program.

 

*/

 

 

using namespace std;

// read data into time array

void     CalculateCharges();  // calculate charges & save in charges array

void     CalculateAverageCharge();  // calculate average charge

void     CalculateHighestCharge();  // calculate highest charge

// calculate lowest charge

// display results

 

int N;  // number of data

// declarations of 2 arrays

int t[3];  // parking durations

float charges[3];  // parking charges

 

main()

 

{

cout << “\n      ************ Tutorial Week 9 ************” << endl;

cout << ”    ******** Program to Calculate Parking Fees ********” << endl;

cout << ”  ******* Nama : Nur Arina Bt Faizal Abdullah *******” << endl;

cout << “************** Matrik No : UK25837 *********************” << endl << endl << endl;

// read data into time array

CalculateCharges();  // calculate charges & save in charges array

CalculateAverageCharge();  // calculate average charge

CalculateHighestCharge();  // calculate highest charge

// calculate lowest charge

system(“PAUSE”);

return EXIT_SUCCESS;

}

 

 

void CalculateCharges()

{

float charges[N], time[N], B1, B2;

for ( N = 0; N < 3; N++)

{

cout << “\nTime: ” << endl;;

cin  >> time[N];

B1 = 20.00;

B2 = (time[N]-1) * 10.00;

charges[N] = B1 + B2;

cout << “\nTotal Time of Parking: ” << time[N] << ” hour(s).” << endl;

cout << “\nCharge for the 1st hour: RM ” << B1 << endl;

cout << “\nCharge for the next consecutive hour(s): RM ” << B2 << endl;

cout << “\nTotal charge: RM ” << charges[N] << endl << endl << endl;

 

 

}

}

 

void CalculateAverageCharge()

{

float Ave, Total, charges[N], time[N], B1, B2;

for ( N = 0; N < 3; N++)

{

Total = Total + charges[N];

}

cout << “\nTotal overall charge for 100 parkings: RM ” << Total << endl;

 

Ave = Total / 3;

cout << “\nAverage charge for 100 parkings: RM ” << Ave << endl << endl;

 

}

void CalculateHighestCharge()

{

float Low, ParkNo, High, charges[N], time[N], B1, B2;

for ( N = 0; N < 3; N++)

{

High = 0;

if ( charges[N] > High)

High = charges[N];

}

cout << “\nThe highest charge is: RM ” << High << endl;

 

for ( N = 0; N < 3; N++)

{

Low = 0;

if (charges[N] < Low)

Low = charges[N];

ParkNo = N;

}

cout << “\nThe lowest charge is RM ” << Low << ” at parking number ” << N;

 

}

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