Sunteți pe pagina 1din 10

Computer Programming

Assignment No.01
Instructions:
Deadline to submit 1
st
assignment is 17
th
October 2010, 11:59 pm.
Submission Date with 50% Marks penalty is 18
th
October 2010, 11: 59 pm.
Your code should be commente generous!" if any.
nswer of e!ery "uestion must be brief.
ssignment will be accepted only in soft form and through email.
Make a MS word file containing answers ha!ing name as ssign# your $ame
and %eg.no e.g
#Assign$01%&aha%09'C('109)
Sub&ect of the email should be as
#Assign$01%&aha%09'C('109)
'(mail it on the ishtia".ahmad)uetta*ila.edu.pk
Cop" assignments, an assignments emai!e on group *i!! get +'%, mar+s.
-age 1 of 10
O,-(C./0(1
.o test the knowledge of the following topics/ co!ered so far0
the acti!ity of programming
the architecture of computers
using your compiler
recogni1ing synta* and logic errors
writing algorithms in plain 'nglish
integer and floating(point numbers
writing arithmetic e*pressions in 233
appreciating the importance of comments and good code layout
defining and initiali1ing !ariables and constants
recogni1ing the limitations of the int and double types
reading user input and displaying program output
changing the !alues of !ariables through assignment
writing simple programs that read numbers and te*t/ process the input and display
the results
,ecoming 2ami!iar *ith "our computer
ll modern computers offer their users an interface to their physical/ electrical/ and
digital systems. Your computer4s operating system pro!ides fundamental ser!ices from
behind the scenes. large part of a computer4s work is simply storing and retrie!ing data
so that it can be a!ailable to use.
.he operating system takes care of a lot of the filing work i.e. storing/ mo!ing and
remembering where things are. You interact with the operating system !ia a Graphic
User Interface (GUI)/ for e*ample a windowing system using a pointing de!ice like a
mouse/ or a Command Line Interface/ using only a keyboard and a single window.
3. 1: 4hat are some e5amp!es o2 tas+s "ou *ou! te!! the computer to o 6ia the
operating s"stem inter2ace7 8!ist on!" 2our9
,perations like browsing the web and editing te*t in a word processor ( most of the
things in!ol!e your operating system e*ecuting some program. .he operating system
itself is a program which is running all the time. 5t e*ecutes your instructions and/ in turn/
it can run other programs.
5t is useful to think of a program as a se"uence of instructions. 6oth e*ecutable
instructions and digital data can be represented as files/ for e*ample/ on your hard dri!e.
6oth are se"uences of symbols/ &ust like the letters that make up the words in this
sentence. Your &ob as a programmer is of course to pro!ide the instructions that operate
on the data.
-age 7 of 10
You ha!e already got some e*perience with the operating system to find a file. You will
locate math.h/ an include file used by 233. You ha!e learned in lab0 how to find a file on
your computer as well as the location of the include files.
3. 2: 4hat i "ou o to 2in math.h7 89rite steps to locate math.h:
.he program that you will be running to write computer programs is your te*t editor/
which is often part of an integrated compiler en!ironment. You ha!e already worked in
computer lab for total ; hours 8< labs: and you ha!e learned how to start the editor.
Compi!ing an running programs 2rom *ithin "our e6e!opment en6ironment
=re"uently in these labs you ha!e compiled !arious programs. 6elow is a copy of a 233
program that displays a drawing. 2opy and paste it into your compiler4s editor/ and from
there sa!e it as art.cpp.
>> -rogram to Display an 4art4 drawing
#include ?iostream.h@
#include ?conio.h@
!oid main8:
A
clrscr8:0
cout??B C C B0
cout??B C C C C B0
cout??B C C C C B0
cout??B C C C C B0
cout??B C C C C B0
cout??B C C C C B0
cout??B C C B0
getch8:0
D
,nce you ha!e typed in 8or/ in this case/ pasted in: a program/ you need to compile it to
create an e*ecutable file. =ind out the steps for your computer system/ then go ahead and
compile art.cpp to an e*ecutable file.
3. :: ;escribe *hat "ou i.
=inally/ execute the program. ,nce again/ the steps depend on your computer system.
3. <: ;escribe *hat "ou i to e5ecute the program.
3. 5: ;escribe *hat "ou i to e5ecute the program.
4riting simp!e programs
-age < of 10
Your initial 233 program contained entirely in one file and there were some elements
that they all had because of re"uirements of the 233 language. 'ssentially/ these were
1: Markers for the compiler ( to know where your program begins and ends E and
7: =iles that your program also needs in order to operate.
9hen you built a program/ your compiler may had looked for code of the formF
#include ?iostream.h@
#include ?conio.h@
!oid main8:
A
>C
your work goes here
C>

getch8:0
D
;etecting s"nta5 an !ogica! errors
.here are numerous opportunities for error in any program/ many times in places that
seem too simple to re"uire close attention.
3. =: 4hat o "ou thin+ the 2o!!o*ing program is suppose to o7
#include iostream.h@
#include ?conio.h@
!oid main8:
A
double radius G 110 >> centimeters
double pi G <.1H 0
double sphereI!olume G 8H><:CpiC8radius C radius C radius:0
double surfaceIarea G 5 C pi C radius0
cout ?? BJolume G B ?? sphereI!olume0
cout ?? Brea G B ?? surfaceIarea
getch8:0
D
3. =: 4i!! it *or+ as it is7
-age H of 10
.ry compiling the program.
3. 7: 4hat *ere the resu!ts7 8>se cop" an paste to *rite a cop" o2 "our compi!er?s
error messages9.
3. 8: &i5 the s"nta5 errors. @i6e a cop" o2 "our program?s output.
3. 9: .he program has t*o !ogica! errors. &i5 them both an *rite the correcte
program.
A!gorithms
n algorithm is a recipe to carry out a task. .o be programmable/ such a recipe must be
Knambiguous
'*ecutable
.erminating
9hile an algorithm will e!entually be e*pressed in a particular programming language/ it
is fre"uently helpful to describe its operation in natural language/ before undertaking the
task of coding it up. =or e*ample/ suppose that you would like to calculate your future
2L-/ and set your goal that you will impro!e your 2L- H% after e!ery semester. 5f
you ha!e got <.01 2L- after end of 1
st
year/ your 2L- after third semester would beF
<.1< 8<.01 3 8<.01 C .0H:: and the semester after <.7; 8<.1< 3 <.1< C .05::/ the semester
after<.<M 8<.7; 3 8<.7; 3 0.05::/ etc.
3 10: ;escribe an a!gorithm to o this computation 2or a C@PA o2 2.8 a2ter an"
number o2 semesters 8bet*een 2 to 89. Aour a!gorithm shou! be so etai!e that
an"one can carr" out the steps an arri6e at the correct ans*er.
Number ."pes
$umbers are essential to computing. 5n 233 there are two fundamental numeric typesF
integers and floating point numbers. Integers ha!e no decimal part/ whereas floating
point numbers do. Variables of either type are represented by user specified names. Space
in the computer4s memory is associated with the name so that the !ariable acts like a
container for the numeric data.
Declaring a !ariable reser!es space in memory and associates a specified name with it.
9hen a !ariable is declared/ it contains whate!er !alue is lefto!er from the pre!ious use
of the memory space. Initializing a !ariable sets it to a specific !alue. 5f no specific !alue
-age 5 of 10
is known/ it4s a good idea to initiali1e new !ariable to 1ero in order to a!oid ha!ing it
contain a random !alue.
."pe Name 6a!ue
int !ariable0 >> Jariable declared but not initiali1ed
int !ariable G 00 >> Jariable declared and initiali1ed to 1ero
double !ariable G !alue0 >C Jariable declared and initiali1ed to another !alueC>
3. 11: Are the 2o!!o*ing integers an 2!oating point numbers proper!" ec!are
anBor initia!iCe7 /2 not, supp!" a correction.
int <0
double0
int G 1M0
float myten G10.7<0
doubleIsum G7.70
int thatI!alue 7170
9hen declaring a !ariable/ take a moment to consider by what name it will be known.
!alid name is made up of characters from the setF
62D'=LN5OPQM$,-R%S.KJ9SY+
upper case letters
abcdefghi&klmnop"rstu!w*y1
lower case letters
017<H5;TUM
digits
I the underscore character
.he only limitation is that a !ariable name cannot begin with a digit. ,f course/ the name
must be uni"ue. $either the compiler nor you would be able to distinguish between
!ariables with the same name. =or some simple cases using single letters like i/ &/ k is
enough/ but as programs grow larger this becomes less informati!e. Qike the name for a
product or ser!ice/ a !ariable name should do two thingsF
1: 6e uni"ue
7: '*press the purpose
5n most cases/ this will happen simply if the name selected is descripti!e enough to tell a
human reader how a !ariable is being used/ for e*ample countImessages/ userIpreference/
orcustomerIname. .his will also help you when you are trying to maintain the program later
on/ since a descripti!e name will assist you in remembering e*actly what a !ariable is
used for.
3. 11: Comp!ete the 2o!!o*ing tab!e o2 poor 6ariab!e names, better 6ariab!e names
an escriptions.
Poor 0ariab!e Name 0ariab!e Dename ;escription
int roll number
Serial number
-age ; of 10
double %sales0
stress in a material
Constants
Qike !ariables/ any numeric constants used by your program should also ha!e
informati!e names/ for e*ampleF const int =QSNI-,5$.I--'% G H510 or const int
6,5Q5$LI-,5$.I9.'% G 717. Ksing named constants transform statements such as
int y G H51 ( * 3 717 into the more intelligible
int y G =QSNI-,5$.I--'% ( * 3 6,5Q5$LI-,5$.I9.'%.
3: 12: @i6e e2initions 2or each o2 the constant escriptions !iste be!o*E
const
definition
Description
$umber of days in a week
$umber of weeks in a year
Minimum wage per hour
Comments
'!en carefully named !ariables are rarely sufficient to fully con!ey the operation of a
program. .hey do not/ for e*ample/ pro!ide an e*planation of a future feature that could
be implemented because of how something is done now/ or lea!e a detailed description of
how a !ariable is computed. You can howe!er place any additional information necessary
in a comment. 2omments are plain te*t for humans to read. .hey are separated from the
machine readable code needed by the compiler by either ofF
>C say whate!er you want in here/ between the slash(asterisk boundaries
the compiler will ignore it
. . .
C>
or
>> say what e!er you want from the double slashes to the end of the line . . .
>> the compiler will ignore it
>> . . .
=rom both design and documentation perspecti!es/ it is a good idea to get in the habit of
commenting your code as early as possible.
-age T of 10
3. 1: : Pro6ie a brie2 escription in terms o2 comments o2 *hat the program
mi5ture.cpp oes.
>>mi*ture.cpp
#include ?iostream.h@
#include?conio.h@
!oid main8:
>C your work goes here((use >> or >C as appropriate C>
A
clrscr8:0
cout ?? B%atio of 2ementV B0
int rIcement0
cin @@ rIcement0
cout ?? BWn %atio of 9aterV B0
int rIwater0
cin @@ rIwater0
cout ?? BBWn %atio of sandV B0
int rIsand0
cin @@ rIsand0
cout ?? B.otal 9eight of Mi*tureV B0
float wImi*ture0
cin @@ wImi*ture0
cout??X 9eight of water is F X0
float wIwaterGwImi*tureC8rIwater>8rIwater3rIcement3rIsand::0
cout??wIwater0
cout??X Wn 9eight of 2ement is F X0
float wIcementGwImi*tureC8rIcement>8rIwater3rIcement3rIsand::0
cout??wIcement0
cout??X Wn 9eight of sand is F X0
float wIsandGwImi*tureC8rIsand>8rIwater3rIcement3rIsand::0
cout??wIsand0
getch8:0
D
/nput an Output
233 defines two standard streams that can be used for te*t mode input and output/
respecti!ely cin and cout. ,perators @@ and ?? are used to direct input and output to and
from the stream.
5n mi*ture.cpp/ for e*ample/ cin @@ rIwater0 was used to get user input. .he e*pression
cout??X Wn 9eight of 2ement is F X0
displays the message Y9eight of 2ement is F X
5t was followed by cout??wIcement0
/which places the contents of the !ariable total into the cout stream.
-age U of 10
5tems in the input stream are separated by hite space ( spaces/ tabs and newlines. =or
e*ample/ two numbers may be input as
< H
or
<
H
5f there is an error/ the stream goes into a failed state rather than plodding ahead with bad
data. =or e*ample/ if the user has typed
l0 8small letter l instead of 18one::
with a small letter l instead of 18one:
10
then the stream fails when trying to read a number.
3. 1< : ;o the 2o!!o*ing !ines o2 input ata *or+ proper!"7 4h" or *h" not7
Kser 5nputF 7H 75 7;
int first/ second/ third0
cin @@ first @@ second @@ third0

Kser 5nputF 7H.H 75.5 7;
double fourth/ fifth/ si*th0
cin @@ fourth @@ fifth @@ si*th0

Kser 5nputF 7H.H 75.5 7;.;
int se!enth/ eighth/ ninth0
cin @@ se!enth @@ eighth @@ ninth0

Kser 5nputF 7<.H 7H.5
double tenth/ ele!enth/ twelfth0
cin @@ tenth @@ ele!enth @@ twelfth0

-age M of 10
Assignment
.here are se!eral primary ways to modify the contents of a !ariable in 233.
cin @@ !ariable0 the 4@@4 operator places the contents of the input stream into the
!ariable
!ariable G <0 the 4G4 operator/ called the assignment operator/ takes whate!er
combination of !alues are on its right side and puts them into a !ariable on its left
side.
During compilation/ !alues on both sides of the @@ and G operators are checked to insure
that the data recei!ed matches the data type of the !ariable to which it will be assigned.
3: 15. Are the right an !e2t han sies o2 the assignment operators o2 compatib!e
t"pe in the 2o!!o*ing statements7 4rite "es or no an gi6e reason.
int dGH0
double a0
int c G c C 8; 3 a:0
double " G B<B0
Arithmetic
3. 1= : Deca!!ing *hat "ou ha6e !earne about integers an 2!oating point 6a!ues,
*hat 6a!ue is assigne to F b" each o2 the 2o!!o*ing7
int + G M 3 70
int + G M ( 70
int + G 5 C 70
int + G U > H0
int + G M > H0
double + G M > H0
int + G 777 > <000
double + G 777.0 > <00.00
int + G 777 > <00.000
3. 17: .rans!ate the 2o!!o*ing a!gebraic e5pressions into CGG :
y G * 3 <>H ( 7
y G *
7
3 ;* ( 7
*
y G (((
1(*
-age 10 of 10

S-ar putea să vă placă și