Upload
galatu-basha
View
232
Download
0
Embed Size (px)
Citation preview
8/10/2019 Cpp-collected Lab Manual
1/31
Adama University
Lab-2
What is the out put of the following programs?
Example 1#include#includeint main ( )
{ clrscr();int x;int y;x= 10=10;y = x>0;cout
8/10/2019 Cpp-collected Lab Manual
2/31
Example5
#include#includeint main ( )
{ clrscr();int x=10;int y=;cout
8/10/2019 Cpp-collected Lab Manual
3/31
return 0;
Exercises
.1 rite ex/ressions or the olloin"
o test i a num%er n is e$en. o test i a character c is a di"it.
o test i a character c is a letter.
o do the test n is odd and /ositi$e or n is e$en and ne"ati$e.
o "i$e the a%solute $alue o a num%er n.. dd extra %racets to the olloin" ex/ressions to ex/licitly sho the order inhich the o/erators are e$aluated
(n = / * ? || n == 0)( (/ < ? 4 n < / 4 ? ' n * ? 2 n 1 ? * n)
.& hat ill %e the $alue o each o the olloin" $aria%les ater its initiali6ation4dou%le d = ' int(&.1@);lon" = &.1@ * &;char c=AaA& ;char c=AdAAcA*BaA ;. rite a /ro"ram hich in/uts a /ositi$e inte"er n and out/uts raised to the /oer o
n. 9int chec the li%rary unctions in the header ile.
.5 rite a /ro"ram hich in/uts three num%ers and out/uts the messa"e sorted i thenum%ers are in ascendin" order, and out/uts not sorted otherise.
.@ rite a /ro"ram that acce/t an inte"er rom the user and /rint the last di"it o the enteredinte"er.
.- rite a /ro"ram that acce/ts three num%ers rom the user and return the a$era"e o thethree num%ers.
.C rite a /ro"ram that acce/ts the tem/erature in D0 orm and "enerate the E0 e?ui$alent. 9int E0 =(D0F &) 52G
&
8/10/2019 Cpp-collected Lab Manual
4/31
Adama University
Department of CS!
Cpp-Lab-" and lab-# Exercise
hat is out /ut4Example: 1
#include void main ()
{int x;
cout > x; if (x > ) cout
8/10/2019 Cpp-collected Lab Manual
5/31
if (ch >& 6 44 ch
8/10/2019 Cpp-collected Lab Manual
6/31
brea*;case =cout
8/10/2019 Cpp-collected Lab Manual
7/31
cout
8/10/2019 Cpp-collected Lab Manual
8/31
i;H!!;,
,
Example: 1#include
void main () { int x9 ans1er; for (x & ; x
8/10/2019 Cpp-collected Lab Manual
9/31
Exercises
1. rite a /ro"ram hich in/uts a /ersonAs hei"ht (in centimetres) and ei"ht (inilo"rams) and out/uts one o the messa"es underweight, normal, or overweight,
usin" the criteria
Jnderei"ht ei"ht < hei"ht2.5Kormal hei"ht2.5
8/10/2019 Cpp-collected Lab Manual
10/31
y =&;case 1 x =;Leault
y = &;x = 1;
C. rite a /ro"ram that creates the olloin" num%er /attern.
1 & 5 @ - C G1 & 5 @ - C1 & 5 @ -1 & 5 @1 & 51 & 1 &
1 1G. rite a /ro"ram that acce/ts student mar out o 100, and return the corres/ondin"
letter "rade %ased on the olloin" conditioni mar is "reater than or e?ual to G0 Ai mar is "reater than or e?ual to C0 and less than G0 $i mar is "reater than or e?ual to@0 and less than C0 Ci mar is "reater than or e?ual to 0 and less than @0 Di mar is less than 0 %or other cases &'
&($ rite the /ro"ram usin" %oth s)itc*and else-if.
10
8/10/2019 Cpp-collected Lab Manual
11/31
Adama University
Department of CS!Cpp-Lab- + Exercise
Functions
Example: 1
#include#include$oid dd ( );$oid su% ( );$oid main (){ char ch; cout ch; sitch(ch) {
case OO 22 or addition dd ( ) ; %rea;
case OHO 22 % or su%traction su% ( ); %rea;
$oid dd(){ int a,%,c;cout a>>%;c = a %;cout
8/10/2019 Cpp-collected Lab Manual
12/31
$oid main (){ char o/;
int o/1,o/;couto/1>>o/;
cout
8/10/2019 Cpp-collected Lab Manual
13/31
$oid add (int, int, int);$oid main ( ){
add (,&);add (,&,);
"etch( );$oid add(int a, int %){ int sum; sum= a%; cout
8/10/2019 Cpp-collected Lab Manual
14/31
nat ( );nat (5);nat (1,15);"etch();
$oid nat (int a, int %){or (int =a;
8/10/2019 Cpp-collected Lab Manual
15/31
Adama University
Department of CS!Cpp-Lab-6,7 Exercises
Example !*e follo)in. pro.ram stores / nat0ral n0mbers in one array and displays t*em 1)it*
eno0.* space bet)een t*e n0mbers )*en displayed(
#include #include #include $oid main ( ) { int nP10Q;
or(int i = 0;i
8/10/2019 Cpp-collected Lab Manual
16/31
{22 e no that e need a constant num%er o elementsconst int max = 10;int num%erPmaxQ;
22 e ill calculate their sumint sum = 0;
cout
8/10/2019 Cpp-collected Lab Manual
17/31
return 0;
Example #
22 :xam/le o indin" the minimum mem%er o an array#include
int main(){
22 he mem%ers o the arrayint num%ersPQ = {C, 5, &@, , 5, @0, -5, CG;int minimum = num%ersP0Q;int a = C;
22 Eom/are the mem%ers
or (int i = 1; i < a; i) {i (num%ersPiQ < minimum)minimum = num%ersPiQ;
22 nnounce the resultcout
8/10/2019 Cpp-collected Lab Manual
18/31
Example 6
#include $oid Lis/layherray(dou%le m%rPQ, int count);
int main()
{ dou%le distancePQ = {.1, -0.5, [email protected], @C.-C, @.C, @C.0, &@.55, @&.1;
22 Urocessin" 5 mem%ers o the arraycout
8/10/2019 Cpp-collected Lab Manual
19/31
Example 4
he olloin" /ro"ram stores sales o commodities in 5 stations or & months. Wou can alsodis/lay hat you stored or checu/ /ur/ose.
#include#include#include$oid main ( ){
loat salesP5QP&Q;or(int s=0;s
8/10/2019 Cpp-collected Lab Manual
20/31
Examples /
#include#include
$oid main ( ){char str P0Q;cout
8/10/2019 Cpp-collected Lab Manual
21/31
cin>>name; cout$oid main ( ){ const int /=&0; char ch1P50Q,chP100Q=+I am /roud +; cout
8/10/2019 Cpp-collected Lab Manual
22/31
cin>>ch; i(strcm/(ch1,ch) = = 0)
cout=0;i**)
cout
8/10/2019 Cpp-collected Lab Manual
23/31
Str0ct0re
Example(
#include
#includestruct student{
char name P0Q;char idRnumP11Q;int a"e ;
;Zoid main ( )
{clrscr( );student stud1;
cout stud1. name;coutstud1. idRnum;cout
8/10/2019 Cpp-collected Lab Manual
24/31
cout
8/10/2019 Cpp-collected Lab Manual
25/31
#include#include
struct /erson
{ strin" name; int a"e; 22o course, mem%er $aria%les must %e /resent
$oid /rint (){
cout
8/10/2019 Cpp-collected Lab Manual
26/31
char title P@0Q; int year;
user PKQ;
$oid /rintmo$ie (mo$iesRt mo$ie);
int main (){ or (int n=0; n#include
struct mo$iesRt {char titleP50Q; int year;;
int main (){ strin" mystr;
mo$iesRt amo$ie; mo$iesRt ' /mo$ie; /mo$ie = amo$ie;
@
8/10/2019 Cpp-collected Lab Manual
27/31
cout
8/10/2019 Cpp-collected Lab Manual
28/31
coutem/1.home. Uost;coutem/1.home.distance;
cout
8/10/2019 Cpp-collected Lab Manual
29/31
istream myRiile(+test.txt+); hile (myRiile) {
myRiile."etline(line,V_); cout$oid main ( ){ char strPQ=+this strin" ill %e ritten to a ile character %y character+; ostream oRile(+test.txt+);
or(int T=0;T
8/10/2019 Cpp-collected Lab Manual
30/31
cin>>/ers.name;cout
8/10/2019 Cpp-collected Lab Manual
31/31
int /os;myRrec.see"(0,iosend);/os=(myRrec.tell"()2si6eo(/ers));cout