Tuesday, April 26, 2011

c++: jawapan untuk mid term soalan E

hoho.. hari tu masa mid term x dpt jawab.. but akhirnya den cuba sampai berjaya..sekadar suka2.. hoho..
i like2..syukur2 alhamdulillah..kalau anda nak tahu jawab c++ ni kalau dapat buat coding nya satu kampung ni aku nk bg tahu.. hahahaha
 ini soalan midterm nya...
"you have a shop that sells five types of fruits, Write a program that asks your cashier to enter the product number that was sold today. If your cashier entered the wrong product code, your program must display the warning "Invalid product code!". Your program will terminate only when your cashier enter the value -1. when your program terminate, it should calculate the total number of each products sold. You must use switch and loop in your program. 
assume that product codes are:
apple:01, orange:02,mangoes:03,strawberries:04,grapes:05
then your program should appear as below:


ini lah input yg kena keluar...
tips untuk jawap soalan sebegini.. first kena lah baca soalan pastu highligt mana output iaitu "cout<< " duluan
cz itu lebih memudahkan kerja anda.. x pun cari apa variable yg nak kena declare... :)
lagi satu kena faham nak guna loop apa tau2.. :)

nah ini jawapanya yang panas dari komputer ku ini ... ilmu share2 kalau den paham hihi.. x faham.. den mmg x suka berkata apa2...

#include<iostream>
using namespace std;
int main()
{
  int product1=0,product2=0,product3=0,product4=0,product5=0;
    int product;// declare variable
    cout<<"Apple:01, Orange:02, Mangoes:03, Strawberries:04, Grapes:05";
    cout<<endl<<endl;

    cout<<"Please enter product code (-1 to end input): ";
    cin>> product;

    while(product != -1)
   {
//jadi apabila kita key in dia -1 satu maka prosess switch x akan masuk... 
switch ( product ) 
{//(product= pangilan bg no 1 till 5) wakilkan
 case 1: // bagi product 1
 product1++;// seperti concept loop for i++ dimana akan berulang.. fahamkan concept switch tau
               //product1 mestilah di declare kerana ia adalah variable
 break;
 case 2:
 product2++;
 break;
 case 3:
 product3++;
 break;
 case 4:
 product4++;
 break;
 case 5:
 product5++;
 break;
 default:
 cout << "Invalid product code: " << product <<'\n';

 break;
 }

cout<<"Please enter product code (-1 to end input): ";
    cin>> product;

}

 cout<< product1<<  "apples sold today\n" ;
  cout<< product2<<  "oranges sold today\n" ;
   cout<< product3<<  "mangoes sold today\n" ;
    cout<< product4<<  "packs of strawberries sold today\n" ;
     cout<< product5<<  "red grapes sold today\n" ;


    cin.ignore();
    cin.get();
    system("cls");
    return 0;
}

---------------------sekian itulah coding nya-----------------
jika ada sebarang komen membina2 berilah yer.. sukacitanya saya menerima

withlove from kazumi d minggu exam.. esok paper c++(27/4/2011)
doakn saya dapat keputusan yg cemerlang


2 comments:

  1. yeap, thats the correct method. Pahamkan loop sbb loop mmg byk n famous dlm C++ ni :) .. array pon cenggitu

    ReplyDelete