Programming Day 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Getting input from the user mqreoaola f,i mßYS,lhd úiska ,ndÿka

mßYS,lhdf.ka wdodk ,nd.ekSu wdodkhlg wod, iEu m%;sodkhlgu fmr


th ,enqKq wdldrh mßYS,lhd oekqïj;a l,
values for a variable can be assigned from the hq;=h
inside of the code, or taken as an input from
the user when the program is running var
úp,Hla i|yd w.h fla;h we;=,;ska wdfoaY a , b : integer ;
l, yels w;r fla;h l%shd;aul jk w;r;=r
mßYS,lhdf.ka b,a,d .ekSuo l, yelsh begin
write ( ‘ input length ’ ) ;
blank variables should be created before read ( a ) ;
taking user input
mßYS,lhdf.ka wdodk ,nd.ekSug fmr write ( ‘ input width ’ ) ;
úp,H idod ;sìh hq;=h read ( b ) ;
write ( ‘ area is : ’ , a*b );
end.
read ( VariableName ) ;

Getting multiple user input


wdodk lsysmhla ,nd.ekSu
var
a , b : integer ; same read command can be used to input
values to a set of variables at once
begin úp,Hka lsym s hlg tljr wdodk ,nd .ekSu
a := 5 ; i|yd tlu read úOdkh Ndú;d l, yel
read ( b ) ;
end.
read ( var1 , var2 , var3 ) ;

As a good practice, before every input we


must inform the user that the program is
expecting an input using a write command
mqreoaola jYfhka" iEu wdodkhlgu fmr
mß.Klh wdodkhla n,dfmdfrd;a;= jk nj
write fla;hla fhdod mßYS,lhd oekqj;a l,
hq;=h'

var
marks : integer ;
begin
write ( ‘ input marks ’ ) ;
read ( marks ) ;
end.

As a good practice, before every output which


is related with a user input, we must inform
the user how the output was generated
Arrays ArrayName: Array [0..N] of DataType;
wdrdjka

 a data structure which contain multiple


storage locations of the same data type ArrayName wdrdfõ ku
 defined under the same symbolic name N (size) m%udKh
(identifier) DataType o;a; j¾.h
 where each data is separated from one Can be changed as preferred
another using an index wNsu; mßÈ fjkia l, yel

 o;a; tlsfklska fjka lr .ekSu i|yd


o¾Yl wxlhla o" C. naming arrays
 ish¨ o;a; u;l ;nd .ekSu i|yd tlu wdrdjka kdulrKh
ixfla;d;aul kduh o Ndú;d lrñka
 tlu j¾.fha rdYshla u;l ;nd.ekSu same rules for naming variables are also
i|yd Ndú;d lrk o;a; jHqyhla applied for naming arrays
wrdjka kdulrKh i|ydo úp,H
kdulrKhg fhdod.;a kS;s tA wdldrfhkau
In Pascal, sector which was used to declare Ndú;d fõ
variables itself is also used to define arrays
meial,a NdYdfõ§ úp,H iE§u i|yd Ndú;d
l, l,dmhu wdrdjka iE§u i|ydo Ndú;d D. assigning values to an array element
lrhs wdrdjl wjhjhla i|yd w.hka wdfoaYh

assignment operator is used


wdfoaY lsÍfï fufyhjkh Ndú;d lrhs

Each element is addressed as


iEu wjhjhlau y÷kajkq ,nkafka
E. displaying an array element
wdrdjl wjhjhla fmkaùu
identifier[index]
var

A. data types of arrays k : array [ 0 .. 4 ] of integer ;


wdrdjkaf.a o;a; j¾. begin
k [ 1 ] := 7 ;
Data types of arrays are as same as the data
types of variables. It alerts the computer write ( k [ 1 ] ) ;
about the type of data that we are going to readln ;
store inside the array end.
wdrdjl o;a; j¾. úp,Hl o;a; j¾. j,g
iudk jk w;r tA u.ska wdrdjla ;=, .nvd
lsÍug n,dfmdfrd;a;= jk o;a; j¾.h .ek F. Reusing the same array element
mß.Klh oekqïj;a lsÍula isÿlrhs wdrdjl tlu wjhjhla kej; kej;
B. declaring a variable Ndú;h
úp,Hla iE§u

var
k : array [ 0 .. 4 ] of integer ;
begin
k [ 0 ] := 7 ;
k [ 1 ] := 5 ;
k [ 3 ] := 9 ;
k [ 2 ] := k [ 0 ] + k [ 1 ] ;
k [ 4 ] := k [ 3 ] * 2 ;
write ( k [ 2 ] , k [ 4 ] ) ;
readln;
end.

G. Assigning and displaying values for arrays


using loops
Æmhla Ndú;fhka wdrdjlg w.hka
we;=,;a lsÍu iy wdrdjl we;s w.hka
lsheùu

Var
k : array [ 0 .. 9 ] of integer ;
i : integer ;
begin
for i := 0 to 9 do
begin
write ( ‘ input no: ’, i + 1 ) ;
read ( k[ i ] ) ;
I := I + 1 ;
end;

for i := 0 to 9 do
begin
write ( k [ i ] ) ;
I := I + 1 ;
end;
end.

You might also like