Monday, March 28, 2011

EXERCISE (IF...ELSE & SWITCH...CASE)


2. CALCULATE (IF...ELSE)

CALCULATE:
a) RECTANGULAR
b) TRIANGLE
c) CIRCLE
d) FAHRENHEIT
e) CELCIUS

Your Selection:

Your Selection is _________ and you are calculate _________. The answer is _____________
 ANSWER :

#include <iostream.h>
main()
{
      //declare
      float b,h,r,w,area;
      float c,f,cel,fah;
      float pi=3.14;
      int selection;
      //out
      cout<<"Please choose you choice"<<endl;
      cout<<"1.Rectangular";
      cout<<"\n2.Triangle";
      cout<<"\n3.Circle";
      cout<<"\n4.Fahrenheit";
      cout<<"\n5.Celcius"<<endl;
      cout<<"Your Selection is:"<<endl;
      cin>>selection;
      switch (selection)
      {
         case 1: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the width"<<endl;
                     cin>>w;
                     //process
                     area = w*h;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Rectangular"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 2: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the base"<<endl;
                     cin>>b;
                     //process
                     area = 0.5*b*h;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Triangle"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 3: cout<<"Please input the radius"<<endl;
                     cin>>r;
                     //process
                     area = pi*r*r;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Circle"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 4: cout<<"Please insert the celcius"<<endl;
                     cin>>c;
                     //process
                     fah= 9/5*(c+32);
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Fahrenheit"<<endl;
                     cout<<"The answer is:"<<fah;
                     break;
         case 5: cout<<"Please insert the fahrenheit"<<endl;
                     cin>>f;
                     //process
                     cel = 5/9*(f+32);
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Celcius"<<endl;
                     cout<<"The answer is:"<<cel;
                     break;
         default :cout<<"Invalid Selection";
          return 0;
             }
}


HOMEWORK 2

#include <iostream.h>
main ()

{
//variable
float sub1,sub2,sub3,sub4,sub5,cpa;
//input1
cout<<"insert subject1:";
cin>>sub1;
//input2
cout<<"insert subject2:";
cin>>sub2;
//input3
cout<<"insert subject3:";
cin>>sub3;
//input4
cout<<"insert subject4:";
cin>>sub4;
//input5
cout<<"insert subject5:";
cin>>sub5;
//process
cpa=((sub1*3)+(sub2*3)+(sub3*2)+(sub4*2)+(sub5*4))/5;
//output
cout<<"subject 1:"<<sub1<<endl;
cout<<"subject 2:"<<sub2<<endl;
cout<<"subject 3:"<<sub3<<endl;
cout<<"subject 4:"<<sub4<<endl;
cout<<"subject 5:"<<sub5<<endl;
cout<<"CPA is:"<<cpa<<endl;
return 0;
}

HOMEWORK 1

#include<iostream.h>
main()

{
//variable
float as1,as2,as3,as4,as5,mid,final,total;
//input1
cout<<"assignment 1:";
cin>>as1;
//input2
cout<<"assignment 2:";
cin>>as2;
//input3
cout<<"assignment 3:";
cin>>as3;
//input4
cout<<"assignment 4:";
cin>>as4;
//input5
cout<<"assignment 5:";
cin>>as5;
//input6
cout<<"midterm:";
cin>>mid;
//input7
cout<<"final:";
cin>>final;
//formula
total = as1+as2+as3+as4+as5+mid+final;
//output
cout<<"assignment 1 is:"<<as1<<endl;
cout<<"assignment 2 is:"<<as2<<endl;
cout<<"assignment 3 is:"<<as3<<endl;
cout<<"assignment 4 is:"<<as4<<endl;
cout<<"assignment 5 is:"<<as5<<endl;
cout<<"mid score is:"<<mid<<endl;
cout<<"final score is:"<<final<<endl;
cout<<"total is:"<<total;
return 0;
}


Friday, March 25, 2011

Sifir

#include <iostream.h>
main ()
{
     int i,j;
     for (i=1;i<=12;i++)
     {
         cout<<"\n";
         for (j=1;j<=12;j++)
         {cout<<" "<<i*j;}
         }
           return 0;
         }

TIN MINUMAN

#include <iostream.h>
main()
{
      //declare
      int selection;
      float money,balance,price,addm,add;
      //out
      cout<<"\n******************";
      cout<<"\nMesin Tin Minuman";
      cout<<"\n------------------";
      cout<<"\n1.PEPSI";
      cout<<"\n2.COCA COLA";
      cout<<"\n3.F&N MIRINDA";
      cout<<"\n4.F&N ORANGE";
      cout<<"\n5.100 PLUS";
      cout<<"\n******************";
      //input
      cout<<"\nPlease Choose Your Drink=";
      cin>>selection;
  
      cout<<"\nPlease Enter MONEY: ";
      cin>>money;
  
      switch (selection)
      {
             case 1: cout<<" Your drink is Pepsi";
                     price = 2.00;
                     break;
             case 2: cout<<" Your drink is Coca Cola";
                     price = 2.00;
                     break;
             case 3: cout<<" Your drink is F&N Mirinda";
                     price = 1.80;
                     break;
             case 4: cout<<" Your drink is F&N Orange";
                     price = 1.80;
                     break;
             case 5: cout<<" Your drink is 100 Plus";
                     price = 2.50;
                     break;
             default: cout<<"invalid selection";
             }
  
      if (money>price)
      {
                      balance = money - price;
      }else if (money<price)
                            {
                                add = price - money;
                                cout<<"Please add (RM" <<add<< ")= ";
                                cin>>addm;
                                balance = money + addm;
                                }
                                cout<<"Your balance is ";
                                return 0;
}



Sunday, February 27, 2011

Assignment 1

1.       Write correct C++ syntax & for each statement below:
a)    Initializing variable Pi with the value 3.14 (constant)

//variable Pi
Float Pi = 3.14

b)      Declare a variable named Parameter with double data type (declare variable)

//variable Parameter
Double Parameter = 9.212156867

c)       Give instruction that allowed user to input data (input)

Cout<<”Please Input Your Data Here”;
Cin >>data;

d)      Input number of computer using variable (input assign to variable value)

//variable noc=number of computer
Int noc;

2.    Solve the question below. Show the working.
a)    5 * 2 % 3 + 25 / 5

(5*2 % 3) + (25 /5)
1 + 5 = 6

b)    a = 5, b = 6
!((a < 3) && (a == 3) || (b > 9))
NOT((5 < 3) && (5 == 3) || (6 > 9))
NOT((0) && (0) || (0))
= 1

3.    Identify syntax errors in the following program.

include<iostream.h>
main()
{
float allowance = 300.00, salary
, TSalary;
cout<<"Input Salary = ";
cin>>salary;
TSalary = salary + Allowance;
cout<<"Salary is= "<< T
Salary;
}




4.    Write a program that will calculate the monthly salary for an employee that where Saturday and Sunday are considered as non-working days.
#include <iostream.h>
main()
{
      //declare
      float basic, payment, total ;
      //intput
      cout<<"Enter basic=";
      cin>>basic;
      cout<<"Enter payment=";
      cin>>payment;
      //process
      total= (payment*22)+basic;
      //output
      cout<<"Your salary="<< total;
      return 0;
 }

5.    Write a program that calculates the average of 5 numbers that can be input by user.

#include <iostream.h>
main()
{
//declare
int num1, num2, num3, num4, num5, average;
//input
cout<<"Enter 5 numbers=";
cin>>num1;
cin>>num2;
cin>>num3;
cin>>num4;
cin>>num5;
//process
average= (num1+num2+num3+num4+num5)/5;
//output
cout<<"Total average="<<average;
return 0;
}


6.    Write a program that will calculate the area of rectangular, triangle and circle.

Rectangular

#include <iostream.h>
Main()
{
//declare
Float h, w, area;
//input
Cout<<”Enter your height=”;
Cin>>h;
Cout<<”Enter your width=”;
Cin>>w;
//process
Area= h*w;
//output
Cout<<”Total of area=”;
Return 0;
}

Triangle

#include <iostream.h>
main()
{
      //declare
      float b,h, area;
      //input
      cout<<"Enter base=";
      cin>>b;
      cout<<"Enter height=";
      cin>>h;
      //process
      area= b*h*0.5;
      //output
      cout<<"Total area="<< area;
      return 0;
      }

Circle

#include <iostream.h>
main()
{
      //declare
      float r,pi=3.14, area;
      //input
      cout<<"Enter radius=";
      cin>> r;
      //process
      area= r*r*pi ;
      //output
      cout<<"Total area="<< area;
      return 0;
      }







Friday, February 25, 2011

Weekly Reflection 3

Tue Feb 08, 2011

-Breakfast 9am-10am.
-Miss suruh Download TURBO C++ 32bit di forum.
-Tp ada problem sikit time download tu.. so class start lambat sikit because of that.
-Miss aku dah kelam kabot, die start class dlu..dalam masa yg sama still men-download (>,<)
-1st miss ajar macam mana nk Register, manage, and settingkan  blogspot.
-Da settle bagi tutorials pasal blog, miss bagi latihan Coding C++ :

1. Calculate 3 numbers with using the formula below :-
calculate = (num1*num2)-num3+(num2/num1)
ANSWER:


#include<iostream.h>
main()
{
//declare variable
float num1,num2,num3,calculate;
//input1
cout<<"Enter Num1";
cin>>num1;
//input2
cout<<"Enter Num2";
cin>>num2;
//input3
cout<<"Enter Num3";
cin>>num3;
//formula
calculate=(num1*num2)-num3+(num2/num1);
//output
cout<<"Answer is="<<calculate;
return 0;
}



*Mesti korang pelikkan mana pergi Weekly Reflection 2 aku ? tetibe je skip Weekly Reflection 3.. haha sebabnye minggu ke-2 aku tak datang class.. Emergency Case !! ( BANGUN LAMBAT)

Programming Exercise 1

Tue Feb 01, 2011

 Multiply Two Integers


#include<iostream.h>
main()
{

//Declare variable
int multiply,num1,num2;

//Input num1
cout<<"Enter num1 =";
cin>>num1;

//Input num2
cout<<"Enter Num 2 =";
cin>>num2;

//Formula
multiply=num1*num2;

//Output
cout<<"mutiply= "<<mutiply;
return 0;
}

by: Echelon Tech Group

Weekly Reflection 1

Tue Feb 01, 2011

-1st class , dah dtg lambat.
-dapat rehat 15 minit utk breakfast
-masok je dalam class miss suruh cari group
-aku dapat group dgn ( Mai, Akma, Nash, Bob), 5 org termasok aku.
-then, Introduction chapter 1
-belajar tentang algorithm , pseudo code , flowchart
-algorithm > menggunakan ayat manusia (means tak perlu coding)
-pseudo code > more to coding
-buat weekly reflection : by group
-buat assignment tentang perbezaan antara procedural programming , structured programming and object oriented programming
-buat forum
-kena buat blogspot

Thursday, February 10, 2011

FOP : Chapter 2

Chapter 2 : C++ Basics

The C++ language consist of two elements : Semantics and Syntax 

  • Semantics is a vocabulary of commands that humans can understand and that can be converted into machine language. Table below shows a partial list of vocabulary use in C++ program programming that are also referred to as keywords or reserved word.
  • Syntax is a language structures (or grammar) that allow humans to combine these C++ commands into a program that actually does something.
Example : 

Cout<<”Hello World.”;       (Correct C++ statement) =Correct Syntax
c Output “Hello World”.    
(Incorrect C++ statement) = Wrong Syntax

This are the keywords/reserved word :





asm
do
inline
short
typeid
auto
double
int
signed
typename
bool
dynamic_cast
log
sizeof
union
break
else
mutable
static
unsigned
case
enum
namespace
static_cast
using
catch
explicit
new
struct
virtual
char
extern
operator
switch
void
class
false
private
template
volatile
const
float
protected
this
wchar_t
const_cast
for
public
throw
while
continue
friend
register
true

default
goto
reinterpret_cast
try

delete
if
return
typedef



A simple example C++ program :
#include <iostream>
using namespace std;
 
int main()
{
        cout << "Welcome to the wonderful world of C++!!!\n";
 
        return 0;
}

Here is an example of running the program:
Welcome to the wonderful world of C++!!!

  • A C++ application program is formed by many components that are comments, the include directive and header filesfunctionsdeclarationsreserved wordsconsole input/outputescape sequences and statements.




Components
Summary
Example
Comments
Comments are non-executing statements that you add to a program for purpose of documentation.
// One line statements
/* Multiple
   line
   statements */
The preprocessor directive and header file
The preprocessor directive enables the program to use certain functions contained in the external file.
#include <iostream.h>
#include <math.h>


Functions
Functions are the basic building block in C++.
Main()
{
    // Statements
}
Declarations
All variables used in a C++ program must be declared before their use. Variables are simply reference to memory locations.
int x, y, sum;
Reserved words
Reserved words used in the C++ language have special meaning and they cannot be used for any other purpose.
Include, int, main, return
Escape sequences
An escape sequence always begins with the backslash (/) and is followed by one letter or special character.
\n, \t, \\, \”, \’
Console input & output
Data are entered into the computer using cin (console input) and the redirection operator     >>. The function cout and the redirection operator   << sends the output to the system   console.
cout<”Enter a number: “;
cin>>num;
C++ statements
Executable statements in C++ must be followed by a semicolon.
Int x, y, sum;
cout<”Enter a number: “;
cin>>x;
sum = x + y;