BLOGGER TEMPLATES AND TWITTER BACKGROUNDS »

Sunday, March 27, 2011

ASSIGNMENT 2







EXERCISE BEFORE MIDTERM

1) Change the if...else to swicth case ... statement
#include<iostream.h>
main( )
{
int nomA,nomB,selesction,value;
cout<<"Enter 2 numbers :";
cin>>nomA>>nomB;
cout<<"1.addition";
cout<<"2.substraction";
cout<<"3.multiply";
cout<<"4.divison";
cout<<"enter selection";
cin>>selection;
{
if(seletion==1)
{value=nomA+nomB;}
else if (selection==2)
{value=nomA-nomB;}
else if (selection==3)
{value=nomA*nomB;}
else if (selection==4)
{value=nomA/nomB;}
else{
cout<<"invalid selection";}
cout<<"the value is: " <<value
}
return0;
}

ANSWER:


#include<iostream.h>
main( )
{
int nomA,nomB,selesction,value;
cout<<"Enter 2 numbers :";

cin>>nomA>>nomB;
cout<<"1.addition";
cout<<"2.substraction";
cout<<"3.multiply";
cout<<"4.divison";
cout<<"enter selection";
cin>>selection;
{
//swicth ...case statement
(swictth selection)
{case1:nomA+nomB;break;
case2:nomA-nomB;break;
case3:nomA*nomB;break;
case4:nomA/nomB;break;
default:cout<<"invalid selection";
cout<<"The value is: " <<value;
}
return0;
}


2)Change for loop below to while loop

#include<iostream.h>
main( )
{
int i,power2;
for(i:1;i<9;i++)
{
power2=i*i;
cout<<" " <<i<<"power2"<<power2;
}
}

ANSWER:


#include<iostream.h>
main( )
{
int i,power2;
//While
 i=1;
while(i<=9)
{
power2=i*i;
cout<<"   "<<i<<"power2"<<power2;
i++;
}
}

3)Write a program that input 2 numbers . compare the numbers and display only the biggest number . If the number are same display "the number are same"

#include<iostream.h>
{
main()
int num1,num2
cout<<"enter an integer";
cin>>num;
//if (num2>num1)
{cout<<"num2 is bigger than num1;}
ele if (num2==num1)
cout<<"num2 is same as num 1";
else
{cout<<"invalid ";}
return0;
}

4.write a program using for loop hat receive prices of 3 products, calculate the price that must be paid after 16% discount is given to every customer

#include<iostream.h>
main( )
{
int x , price , total , discount;
for (x=1,x<4,x++)
{
cout <<"price ["<<x<<"]";
cin >>price;
total= total+price

}cout<<total=<<total;
dicount=total *0.01;
cout<<"discount"
return0;
}

5. what is the output for following program

a) intx=20
for (int i=5,i<=x,i++)
{
cout<< i;
}

answer: i =5,10,15,20

b) int mul=1
do
{
mul=mul*2;
cout<<"the mul is :" <<mul;
mul++;
while (mul10)

answer : mul = 1 ,3, 7

FOP CHAPTER 6

TOPIC : LOOP&WHILE


LOOP
#include<iostream.h>
main( )
{
//declare variable
//get input
cout<<"enter integer";
cin>>x;
//loop for
for(y=0:y<xy:y++)
{
cout<<"     "x;
}

WHILE

#include<iostream.h>
main( )
int x,y;
cout<<"enter an integer";
cin>>x;
//while(y<x)
cout<<"   "<<x;
y++;
}
}

ASSINGMENT 1

Instruction : write a program

   
**********************************
          MESIN TIN MINUMAN
-----------------------------------------------
          JENIS                     RM
-----------------------------------------------
1.  PEPSI                          1.80
2. COCA COLA               1.90
3. FS ORANGE                1.50
4. FS MIRINDA               1.60
5. 100 PLUS                     2.00
*********************************















EXECISE 2 MAR 2011

1) a - initializing variable pie with the value 3.14
    answer : float pie = 3.14
    b- declare a variable and perimeter with double data type
    answer : Double perimeter ;
    c- give instruction that allowed user to input data
    answer : cout << " enter your data " ;
                   cin >> data ;
    d- input number of computer using variable
    answer : cout << " enter computer number " ;
    cin >> number ;


2) change else statement to swicth..case

#include <iostream.h>
main ( )
{
int selection , quantity ;
float price ;
cout << "1.pen = rm 0.50" ;
cout<< "2.pencil = rm 0.30";
cout<< "3.ruler = rm0.20";
cout<< "4.eraser=rm0.10';
cin>>selection;
cin>>quantity;
if (selection==1)
{price = quantity*0.20}
else if (selection ==2)
{price = quantity*0.30}
else if (selection ==3)
{price = quantity*0.20}
else if (selection ==4)
{price = quantity*0.10}
else
{cout << "invalid seletion" ;}
cout<< "the price is :      " << price ;
return 0;
}

answer :

#include <iostream.h>
main ( )
{
int selection , quantity ;
float price ;
cout << "1.pen = rm 0.50" ;
cout<< "2.pencil = rm 0.30";
cout<< "3.ruler = rm0.20";
cout<< "4.eraser=rm0.10';
cin>>selection;
cin>>quantity;
swicth (selection)
{
case 1 : price=quantity*0.50;break;
case 2 : price=quantity*0.30;break;
case 3 : price=quantity*0.20;break;
case 4 : price=quantity*0.10;break;
default : cout << "invalid selection";
}
cout << the price is :      " << price;
return 0;
}

FOP CHAPTER 5

STUDY DATE : 2 MAR 2011
TOPIC : IF ELSE ... SWICTH CASE......

EXAMPLE :

AGE   =    GENERATION
1-6      =  kids
7-12    =  child
13-21  =  teenagers 
22-30  =  adult
> 31    = old

if ..... else 

#include <iostream.h>
main ( )
{
declare variable
int age ;
// input
cout << " enter your AGE : " ;
cin >> age ;
//if else statement
if (age > = 1 II age <=6)
{cout << " you are kids ! " ;
else if ( age >=7 II age < = 12 )
{cout << '" you are child ! " ;
else if ( age >=13 II age <=21 )
{cout << " you are teenagers ! " ;
else if ( age <=22 II age <=30 )
{cout << " you are adult ! " ;
else if ( age > 30 )
{cout << " you are old ! " ;
else
{cout << "invalid selection" , ;
return 0 ;
}

swicth...case

#include < iostream.h>
main ( )
//declre variable
int age ;
//input
cout << " enter your age : " ;
cin >> age;
// swicth ... case statement
{ case ('age'>=1 II 'age' <= 6) : cout << " you are kids ! " ; break ;
case ( 'age'>=7 II 'age' <=12) : cout <<  " you are child ! " ; break ;
case ('age'>= 13 II 'age' <= 21) : cout << " you are teengers ! " ; break ;
case ( 'age' >= 22 II 'age' <=30) : cout << "you are adult ! " ; break ;
case ( 'age >30 ) : cout << " you are old ! " ; break ;
default : cout << " invalid selection " ;
}
return 0 ;
}

FOP CHAPTER 4

OPERATORS AND EXPRESSION

1) Arithmetic operators
++ (plus)
-- (minus)
+ (addition)
- (substraction)
* (multiplication)
/ (floating point division / int division)
% (modulus)

2) Increment and decrement in C++

++ (pre-increment) ++m
++ (post-increment) --n
-- (pre-decrement) --n
-- (post-decrement) n--

EXAMPLE 1:

1) a=2 , m=2 , p=2 , x=2 , b=3 , n=3 , q=3 , y=3



question :
  (++a) + (--b) * 4> (++m) * ( -- n) + 4
=(2+1) + (3-1) * 4> (2) * (3) + 4
=               3+2* 4> 2*3+4
=                3+8   > 6+4
=                 11    > 10
=                    true/1

3) Reltional operators:-

< less than
> greater than
== equal too
<= less than or equal too
>= greater than or equal too
!= not equal too

4) logical operators

( 0/FALSE = 1/TRUE ) && (AND) {tips if got 0 will be 0/false}
II (OR) { TIPS IF GOT 1 WILL BE 1/TRUE

 ! (NOT)
 ! (&&) NOT AND
 ! (II) (NOT



EXAMPLE :

a=2 , m=2 , p=2 , x=2 , b=3 , n=3 , q=3 , y=3

++ a +  --b * 4 > ++m * --n +4 && p++ + q -- * 4 < x++ * y-- + 4
(2+1)+(3-1)*4 > (2+1)*(3-1)+4 &&       2+3*4      < 2*3+4
   3   + 2 * 4    >   3    * 2    +4 &&       2 + 12      <  6+4
         11         >         10          &&        14          <   10
                  1/true                    &&              0/false
                                     0/false

Saturday, March 5, 2011

EXECISE 23 FEB 2011

1) SOLVE THE QUESTION BELOW

a) 5 * 2 % 3 + 25 / 5
    10     % 3 + 5
    1              + 5
    6 (answer)

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


2 ) Identify the syntax errors in the following program

# include < iostream.h>
mai ( )
{
float allowance = 300.00 salary ;
// input
cout  << " input salary = '' ;
cin > salary ;
salary = salary + allowance ;
// output
cout << '' salary is = '' << salary;
}

answer :
# include < iostream.h >
main ( )
{
float allowance = 300.00 salary ;
// input
cout  << " input salary = '' ;
cin >> salary ;
salary = salary + allowance ;
// output

cout << '' salary is = '' << salary ;
return 0 ;

}

3. write a program that calculate the monthly salary for an employee that where saturday & sunday are considered as non working days

# include < iostream.h >
main ( )
// declare variable
float no working days ;
float salary per day ;
float monthly salary ;
// input 1
cout << '' enter no working days '' ;
cin >> '' no working days '' ;
// input 2
cout << '' enter salary per day '' ;
cin >> '' salary per day '' ;
// formula
'' monthly salary = working days * salary per day '' ;
// output
cout << '' monthly salary << monthly salary '' ;
return 0 ;
}

4) write a program that calculates the average of 5 numbers that can be input by user

# include < iostream.h >
main  ( )
float no 1
float no 2
float no 3
float no 4
float no 5
float total
float average
// input
cout << '' num 1 '' ;
cin >> '' num 1 '' ;
cout << '' num 2 '' ;
cin >> '' num 2 '' ;
cout << '' num 3 '' ;
cin  >> num 3 '' ;
cout << '' num 4 '' ;
cin >> '' num 4 '' ;
cout << '' num 5 '' ;
cin >> '' num 5 '' ;
// formula
total = num1 + num2 + num3 + num4 + num5
// output 1
cout << '' total / 5 '' ;
//output 2
cout << '' average '' << average ;
return 0 ;
}

5 ) write  program that will calculate the area of rectangular , triangle , circle


# include < iostream.h >
main ( )
{
declare variable
float height ;
float width ;
float area_rectangle ;
float radius ;
float area_circle ;
float height ;
float area_triangle ;
// input 1
cout << '' enter height '' ;
cin >> '' height '' ;
// input 2
cout << '' enter width '' ;
cin >> '' width '' ;
// input 3
cout << '' enter radius '' ;
cin >> '' radius '' ;
// formula 1
area_rectangle = height*width ;
//output 1
cout << '' area_rectangle = '' << area_rectangle ;
// formula 2
area_circle = 3.14*radius*radius ;
//output2
cout << '' area_circle = '' << area circle ;
// formula3
area_triangle = 0.5*height*height ;
//output3
cout << '' area_triangle = '' area_triangle ;
retrun o ;
}

MATA DAH NGANTOK , SAMBUNG ESOK LA JWBNYERR