Saturday, December 31, 2011

Tutorial Assignment 1

Assalamualaikum.
Macam yg dh dijanjikan, ni tutorial pertama aku leh bg.
Program ni berdasarkan assignment 1 CSC138 kelas En. Jiwa sesi November 2011
Aku dah modify sikit soalan sbb xnk bg korang copy paste jwpn bg kt sir.
Ini full coding dgn soalan modified.
(aku guna site mediafire sbb tu yg aku tau, kalau ade site lg sng pakai btau ah)
Jom, tgk coding fragment by fragment.
Oh, aku tulis, compile n run program ni pakai Turbo C++

#include <iostream.h>
#include <iomanip.h>

const int CITYNUM = 2;
const int WEEKNUM = 4;

So, #include <iostream.h> ni benda biasa lah kot, tak payah nk explain kn?

#include <iomanip.h> ni untuk kita pakai function2 untuk manipulate(kawal) input atau output. Input Output MANIPulator. Dalam program ni kita cuma pakai setw dgn setprecision. I'll explain more as we get to them.
Kalau korang pakai compiler selain dari Turbo, korang kena buang '.h' tu jadi #include <iostream>, kalau tak error. Pastu kena tambah satu statement lg lepas dh habes #include, 'using namespace std;'

const int CITYNUM = 2;
const int WEEKNUM = 4;
Ini pun korg tahu kot, declare constant. Dua2 constant ni aku pakai sbg saiz 2D array. Aku sj pakai constant supaya senang nnt kalau aku nk ubah saiz 2D array aku just ubah kt sini, tak payah nk tukar satu2 kt dlm coding.

void main()
{
 char cityName[CITYNUM][15];
 int victim[CITYNUM][WEEKNUM];
 float expenditure[CITYNUM][WEEKNUM];

 cout.setf (ios::fixed);
 cout << setprecision(2);

 cout << "Flood Victim Application" << endl;
void main() ini pun benda wajib tahu, sape taktau lg pegi part 1 blaja balik :P . Computer mmg akan cari function 'main()' ni sbg entry point untuk apa2 program yg kita tulis. boleh jugak pakai 'int main()', tapi nanti kena tambah return statement sebelum end. Lg satu, kena tambah 2 parameter apa entah (dh lupa) untuk compiler2 baru.

char cityName[CITYNUM][15];
Aku declare 2D array of char, untuk store nama2 bandar. Sebenarnya ini array of string. Tapi dah string tu sendiri ialah array of char (char name[20]), so bila nk simpan byk2 nama, jadi lah 2D array of char. Kalau rajin explore, boleh je cari kt website cplusplus cara nk pakai datatype string,  tak payah pakai char.

int victim[CITYNUM][WEEKNUM];
Ini 2D array utk store number of victim.
float expenditure[CITYNUM][WEEKNUM];
Ini 2D array utk store expenditure.
Nampak tak aku pakai constants kt sini? Contoh la nanti kalau aku nk ubah program ni, nak kira untuk 10 weeks pulak instead of 4 weeks, aku just pegi ubah kt declaration constant td, tak payah ubah semua kt sini.

cout.setf (ios::fixed);
cout << setprecision(2);
Ok, yang ni rasanya korang dh penah buat, tp aku explain je lah. Ni untuk format-kan nombor perpuluhan yang korang nk display. yang cout.setf tu copy lah bulat2 satu line tu, aku pun xpaham sgt nk explain camne, tapi kira line ni untuk set kan jenis format nombor floating yg kita nk display lah. selain dari ios::fixed tu ade lg parameter lain boleh pakai, korg explore sendiri nanti share2 dgn aku lak. Tapi yang kita pakai fixed tu format dia supaya nombor pepuluhan yg keluar ikut bilangan dalam setprecision tu dan nombor2 yang lebih takkan dibundarkan. Contoh la kalau kita buat :
cout << 12.3456 << endl;
cout << 12.1 << endl;
output :
12.34
12.10
Nombor tak bundar jadi 12.35 .
Then, kalau korang nak ubah titik perpuluhan, korang ubah la nombor kt dalam setprecision tu. Aku guna 2 sebab nak display nilai duit kan.

 //loop for row
 for(int x=0;x<CITYNUM;x++)
 {
  cout << "*** City " << (x+1) << " ***" << endl;
  //input city name
  cout << "City Name : ";
  cin.get(cityName[x],15);

So, ini loop pertama, disebabkan kena pakai 2D array, mau takmau kena lah buat nested loops. Loop ni untuk array berubah dari satu row ke row seterusnya.
Line 2: sekali lagi aku pakai constant sbg condition for loop.
Line 4: cout biasa, nk display input untuk city yang ke berapa.
Line 6: cara nk request input pakai cin.get (<variable name char> , <size variable>). Size variable tu ikut lah size datatype char yg kita declare kt atas td.

  //loop for column
  for(int y=0;y<WEEKNUM;y++)
  {
   //input number of victims weekly
   cout << "Week " << (y+1) << " : ";
   cin >> victim[x][y];

Ini loop kedua, loop yg belah dalam. Loop ni untuk ubah dari column ke column lah.
Line 5: cout biasa untuk display input week ke berapa.
Line 6: request input untuk number of victims.

   //calculate expenditure
   if(victim[x][y] < 50)
    expenditure[x][y] = (float)(victim[x][y]*70);

   else if((victim[x][y]>50) && (victim[x][y]<=100))
    expenditure[x][y] = (float)(victim[x][y]*68);

   else if((victim[x][y]>100) && (victim[x][y]<=200))
    expenditure[x][y] = (float)(victim[x][y]*66);

   else if((victim[x][y]>200) && (victim[x][y]<=100))
    expenditure[x][y] = (float)(victim[x][y]*64);

   else
    expenditure[x][y] = (float)(victim[x][y]*60);
  }

Ini semua if statements lah, untuk calculate expenditure, ikut soalan.
Tgk line 3,6,9,12,15 tu ada (float) ini casting untuk tukar integer dari variable victim tu jadi float datatype.
Lepas tu end loop untuk column.

  //flush input stream
  cin.ignore(80,'\n');
  cout << endl;
 }

Line 2: cin.ignore ni sebenarnya untuk flush input stream. Kita kena flush input stream di antara satu input statement (cin) dan sebelum pakai cin.get . Macam dalam coding ni, kita baru buat input number of victim, tp selepas pusingan loop yang pertama, kita nk buat input city name, pakai cin.get sbb tu kena flush.

A bit hard to explain but here we go:
Setiap kali kita buat input, let say, cin>>num1; , masa run, kita kena type input, contoh : 2, so kita tekanlah nombor '2' kt keyboard and then tekan 'Enter', baru input tu masuk dalam console. Nombor '2' tu akan masuk dalam variable 'num1', tapi, value 'Enter' (compiler baca 'Enter' sbg '\n') tu stay kt dalam console. Kalau kita just pakai cin>> takdak masalah, boleh run mcm biasa. Tapi, bila pakai cin.get atau cin.getline, compiler akan baca value 'Enter' tu sbg satu input. So, kalau kita tak flush value 'Enter' tu, variable yg kita pakai dlm cin.get tu akan store value 'Enter' atau '\n' tu. Sbb tu kalau korang tak flush, masa run tiba2 program skip satu input. (kalau explanation ni tak faham, btau aku, nanti aku buat graphical punya).

 //display victim report
 cout << endl;
 cout << "Weekly Victim Report" << endl;
 cout << setw(15) << "City";

 for(x=0;x<WEEKNUM;x++)
 {
  cout << setw(9) << "Week " << setw(1) << (x+1);
 }
 cout << endl;

Part ni yang lain2 tu biasa je lah kot. kecuali Line 4 dgn Line 8 tu, setw. Benda ni kalau korang taknak pakai pun takpe, setakat nk bagi chantek output je. Rasanya korang pakai '\t' jee senang untuk buat display macam table tu kan. Tapi aku pakai setw sbb aku rasa cam cool je, haha.

setw ni rasanya untuk set width utk satu output stream yg seterusnya. Satu je. Contoh :
cout << setw(10) << "Hello" << "World";
Statement ni akan 'reserve' 10 character space untuk "Hello", tapi tak buat apa2 untuk "World".
Output dia camni :
_ _ _ _ _ HelloWorld
Underline tu blank space, satu underline untuk satu character space (macam tekan space bar sekali).
Yang ni pulak :  cout << setw(10) << "Hello" << setw(8) << "World";
Output : _ _ _ _ _Hello_ _ _World
Korang try lah macam2 pakai setw ni, sampai paham macam mana nk manipulate. Jgn risau, tak masuk exam kot. Kot~~~
Line 4: aku set width utk field 'City' 15 character space, sbb saiz city name aku 15 (tgk declaration)
Line 8: aku set width untuk "Week " (ade satu space situ) 9 space, untuk nombor week tu 1 space. Total spacing untuk setiap 'Week' = 10 spacing.

 //loop for row
 for(x=0;x<CITYNUM;x++)
 {
  cout << setw(15) << cityName[x];

  //loop for columns
  for(int y=0;y<WEEKNUM;y++)
  {
   cout << setw(10) << victim[x][y];
  }
  cout << endl;
 }

So, ini untuk display 'table' korang, of course kena display row by row, kena access balik 2D array yg dh input sebelum ni, so, kena lahh pakai nested loop lagi.
Line 3: aku set width untuk display cityName 15 spacing, sama saiz dgn 'City' (part sebelum ni).
Line 8: set width 10 spacing untuk display number of victim, sama saiz dgn total spacing untuk 'Week' (part sebelum ni).
Kiranya pakai setw ni aku dah susun supaya output jadi camni (contoh input city name = Kajang, week 1 victim = 100):
_ _ _ _ _ _ _ _ _ _ _ City _ _ _ _ _ Week 1 (sampai Week 4)
_ _ _ _ _ _ _ _ _ Kajang  _ _ _ _ _ _ _ 100 (sampai week 4)

huruf 'y' kt 'City' akan selari dgn huruf 'g' kt 'Kajang' tu. Nombor '1' kt 'Week 1' akan selari dgn nombor '0' kt '100' tu.

 //display expenditure report
 cout << endl;
 cout << "Weekly Expenditure Report" << endl;
 cout << setw(15) << "City";
 for(x=0;x<WEEKNUM;x++)
 {
  cout << setw(9) << "Week " << setw(1) << (x+1);
 }
 cout << endl;

 //loop for row
 for(x=0;x<CITYNUM;x++)
 {
  cout << setw(15) << cityName[x];

  //loop for columns
  for(int y=0;y<WEEKNUM;y++)
  {
   cout << setw(10) << expenditure[x][y];
  }
  cout << endl;
 }
}

Part ni sama je dgn part2 atas yang display victim report tu. Cuma tukar variable dgn tajuk report je kot. Boleh je nk adjust2 setw kt sini, sbb expenditure tu akan keluar byk digit, so 10 spacing tu mungkin tak cukup.

Takat ni je lah aku buat utk korang eh. Yang extra 5 marks untuk function tu pandai2 sendiri lah yee, hahaha. Function korang kena paham betul2, kalau boleh genggam betul2 sbb function ni kira core untuk programming lah. Korang akan pakai banyak function sampai part 5. Mungkin part 3 atau part 4 nanti baru korang betul2 boleh faham function ni camne. Anyway, kalau ada apa2 tak paham dlm post ni, comment je. Insyaallah aku reply secepat mungkin, n kalau perlu aku edit lah entri ni. Kalau nk request coding untuk  extra 5 marks tu pun boleh, kalau ade request, then aku buat entri baru. So, jgn lah malu2 nak tanya walau sebodoh mana pun korang rasa soalan korang tu, insyaallah kalau aku mampu aku jawab. Kalau aku tak mampu, kita sama2 google cari.

Full Coding:
#include <iostream.h>
#include <iomanip.h>

const int CITYNUM = 2;
const int WEEKNUM = 4;

void main()
{
 char cityName[CITYNUM][15];
 int victim[CITYNUM][WEEKNUM];
 float expenditure[CITYNUM][WEEKNUM];

 cout.setf (ios::fixed);
 cout << setprecision(2);

 cout << "Flood Victim Application" << endl;

 //loop for row
 for(int x=0;x<CITYNUM;x++)
 {
  cout << "*** City " << (x+1) << " ***" << endl;
  //input city name
  cout << "City Name : ";
  cin.get(cityName[x],15);

  //loop for column
  for(int y=0;y<WEEKNUM;y++)
  {
   //input number of victims weekly
   cout << "Week " << (y+1) << " : ";
   cin >> victim[x][y];

   //calculate expenditure
   if(victim[x][y] < 50)
    expenditure[x][y] = (float)(victim[x][y]*70);

   else if((victim[x][y]>50) && (victim[x][y]<=100))
    expenditure[x][y] = (float)(victim[x][y]*68);

   else if((victim[x][y]>100) && (victim[x][y]<=200))
    expenditure[x][y] = (float)(victim[x][y]*66);

   else if((victim[x][y]>200) && (victim[x][y]<=100))
    expenditure[x][y] = (float)(victim[x][y]*64);

   else
    expenditure[x][y] = (float)(victim[x][y]*60);
  }

  //flush input stream
  cin.ignore(80,'\n');
  cout << endl;
 }

 //display victim report
 cout << endl;
 cout << "Weekly Victim Report" << endl;
 cout << setw(15) << "City";

 for(x=0;x<WEEKNUM;x++)
 {
  cout << setw(9) << "Week " << setw(1) << (x+1);
 }
 cout << endl;


 //loop for row
 for(x=0;x<CITYNUM;x++)
 {
  cout << setw(15) << cityName[x];

  //loop for columns
  for(int y=0;y<WEEKNUM;y++)
  {
   cout << setw(10) << victim[x][y];
  }
  cout << endl;
 }

 //display expenditure report
 cout << endl;
 cout << "Weekly Expenditure Report" << endl;
 cout << setw(15) << "City";
 for(x=0;x<WEEKNUM;x++)
 {
  cout << setw(9) << "Week " << setw(1) << (x+1);
 }
 cout << endl;

 //loop for row
 for(x=0;x<CITYNUM;x++)
 {
  cout << setw(15) << cityName[x];

  //loop for columns
  for(int y=0;y<WEEKNUM;y++)
  {
   cout << setw(10) << expenditure[x][y];
  }
  cout << endl;
 }
}

Program Screenshot:


Terima kasih kerana membaca.
Assalamualaikum.

No comments:

Post a Comment