• No se han encontrado resultados

Translation recognition task

// WAP to count odd & even no’s from an array.WAP to count odd & even no’s from an array.

void main() void main() {

{ iinnt t aa[[1100]], , ii, , ccee;;

for (i=1; i<=10; i++) for (i=1; i<=10; i++) {

{

 printf(”\n Enter Element:”);

 printf(”\n Enter Element:”);

cin>>a[i];

cin>>a[i];

if(a[i]%2==0) if(a[i]%2==0)

ce++

ce++

} }

 printf(“\nEven No’s=%d”,ce);

 printf(“\nEven No’s=%d”,ce);

 printf(“\nOdd No’s=%d”,10-ce);

 printf(“\nOdd No’s=%d”,10-ce);

} }

Addresses and Pointers Addresses and Pointers

Every byte in the computer memory has an address. Addresses are basically numbers that start from 0 and Every byte in the computer memory has an address. Addresses are basically numbers that start from 0 and end at highest address which will vary

end at highest address which will vary accordinaccording to g to the amount of memory available in computer. E.g. Thethe amount of memory available in computer. E.g. The highest address for 64 KB of memory is

highest address for 64 KB of memory is 65535 (i.e. 64* 1024=65536 bytes). 65535 (i.e. 64* 1024=65536 bytes). & operator returns the memory& operator returns the memory address of a variable

address of a variable

Pointers are memory variables that store the address of another variable instead of value. Pointers are Pointers are memory variables that store the address of another variable instead of value. Pointers are declared as

declared as

int *p;

int *p;

The asterisk (

The asterisk (**) also called indirection operator.) also called indirection operator.

Advantages of Pointer:

Advantages of Pointer:

1.

1. UsinUsing poig pointernters we cs we can cran create eate dynadynamic mic arraarrayy 2.

2. UsinUsing pointg pointers it is poers it is possibssible to pasle to pass comps complete arlete array to furay to functinctionon 3.

3. Helpful Helpful in cin creating reating data data structurstructures likes like line linked liked list, st, stack, stack, queuequeue 4.

4. PoiPointernters reducs reduce the lee the length & cngth & compomplexilexity of prty of prograogram.m.

5.

5. Passing Passing argumentarguments to funs to function whction when functen function needs ion needs to modito modify origify original argunal arguments.ments.

6.

6. PointerPointers increas increase the se the executiexecution speed on speed of progof program aram and are nd are more emore efficientfficient

Disadvantage

Disadvantages s of Pointer:of Pointer:

1.

1. UniUninitinitializalized poined pointerters causs cause the sye the system tstem to craso crash.h.

2.

2. DiDiffifficucult lt to to undunderserstantandd For Example

For Example void main( ) void main( ) {

{ int int i=3, i=3, *x; *x; float float j=1.5, j=1.5, *y;*y; Output:Output:

p

prriinnttff((““\\nn%%dd\\tt%%ff””, , ii, , jj)); ; 33 55 p

prriinnttff((““\\nn%%dd\\tt%%dd””, , &&ii, , &&jj));; 22000000 22000022 x

x==&&ii;; yy==&&jj;; 22000000 22000022 p

prriinnttff((““\\nn%%dd\\tt%%dd””, , xx, , yy));; 33 55   printf(“\n%d\t%f”,

  printf(“\n%d\t%f”, *x, *x, *y); *y); 3030

*x=30;

*x=30;

 printf(“\n%d”, i);

 printf(“\n%d”, i);

} }

Operation on Pointers Operation on Pointers

Following operation can be performed on a pointer  Following operation can be performed on a pointer 

1)

1) AddAdditiition on of of a na numbumber er to to a a poipointenterr int a[5]={11, 12, 13, 14, 15},*p;

int a[5]={11, 12, 13, 14, 15},*p;

 p

 p=&=&a[a[0]0];; ////ThThe ade addrdresess of s of ththe fie firsrst et elelemement ont of a if a is sts storored ied in p in p i.e.e. 2. 2000000  p

 p=p=p+2+2;; ////p p nonow w cocontntaiains ns ththe e adaddrdresess s of of ththirird d elelememenent t of of a a i.i.e. e. 20200404 aa[[00]] aa[[11]] aa[[22] ] aa[[33]] aa[[44]]

2

200000 0 2200002 2 2200004 4 22000066 22000088 2) Subtraction of a number from pointer

2) Subtraction of a number from pointer int a[5]={11, 12, 13, 14, 15},*p;

int a[5]={11, 12, 13, 14, 15},*p;

 p

 p=&=&a[a[3]3];; ////ThThe ade addrdresess of s of ththe foe foururth th elelememenent of t of a ia is sts storored ied in p in p i.e.e. 2. 2000066 p

p=p=p-1-1;; ////p np noow cw cononttaainins ts the he aadddrdreess ss of of tthhiird rd eleleemmenent ot of a f a ii..ee. 2. 2000404

Pointers and array:

Pointers and array: There is a close association between pointers and arrays. The array name itself is aThere is a close association between pointers and arrays. The array name itself is a  pointer, which will store the base address of the array. For example

 pointer, which will store the base address of the array. For example 1

111 1122 1133 1144 1155

void main( ) void main( ) {

{

int a[5]={ 31, 54, 66, 77, 16};

int a[5]={ 31, 54, 66, 77, 16};

for( int i=0;i<5; i++) for( int i=0;i<5; i++)

 printf(“\n%d”, *(a+j));

 printf(“\n%d”, *(a+j));

} }

Here expression

Here expression *(a+j) has exactly *(a+j) has exactly the same the same effect as effect as a[j].a[j].

Structure Structure

A structure is a collection of related fields of different types. Here fields are called structure member or  A structure is a collection of related fields of different types. Here fields are called structure member or  structure element. A structure represents an entity for example book. In C we can also use function in structure element. A structure represents an entity for example book. In C we can also use function in structure. But structures are mainly used to collect data.

structure. But structures are mainly used to collect data.

struct book  struct book  {

{ int int bno, bno, edition;edition;

char title[20];

char title[20];

float price;

float price;

};

};

void main( ) void main( ) {

{ strstruct uct boobook b1k b1={ ={ 101101,3,3, “, “DODOS”S”, 23, 230.0.00 }00 };;

struct book b2, b3;

struct book b2, b3;

  b2.bno=102; b2.edition=2;

  b2.bno=102; b2.edition=2;

strcpy(b2.title,”Java”);

strcpy(b2.title,”Java”);

 b2.price=140.00;

 b2.price=140.00;

 printf(”\n Enter book no.”); scanf(“%d”, &b3.bno);

 printf(”\n Enter book no.”); scanf(“%d”, &b3.bno);

 printf(”\n Enter Edition.”); scanf(“%d”, &b3.edition);

 printf(”\n Enter Edition.”); scanf(“%d”, &b3.edition);

 printf(”\n Enter Title”); scanf(“%s”, b3.title);

 printf(”\n Enter Title”); scanf(“%s”, b3.title);

 printf(”\n%d\t%d\t%s\t%f”, b1.bno, b1.edition, b1.title, b1.price);

 printf(”\n%d\t%d\t%s\t%f”, b1.bno, b1.edition, b1.title, b1.price);

 printf(”\n%d\t%d\t%s\t%f”, b2.bno, b2.edition, b2.title, b2.price);

 printf(”\n%d\t%d\t%s\t%f”, b2.bno, b2.edition, b2.title, b2.price);

 printf(”\n%d\t%d\t%s\t%f”, b3.bno, b3.edition, b3.title, b3.price);

 printf(”\n%d\t%d\t%s\t%f”, b3.bno, b3.edition, b3.title, b3.price);

} }

Features:

Features:

1.

1. StructuStructure specifre specifier and dier and definitiefinition can alon can also be coso be combined imbined into a sinto a single stangle statementtement struct

struct {

{ int int bno, bno, edition;edition;

char title[20];

char title[20];

float price;

float price;

} b1;

} b1;

2.

2. StructuStructure memre members can bbers can be initie initialized at alized at the timthe time of cre of creation of eation of structurstructure variabe variables.les.

struct book

struct book b1={ 103, b1={ 103, 3, “C”, 3, “C”, 120.00 };120.00 };

3.

3. One One structure variable structure variable can can be be assigned to assigned to another.another.

struct

struct book book b2= b2= b1;b1;

Union Union

A union is a collection of related fields of different types sharing the same memory area. Here fields are A union is a collection of related fields of different types sharing the same memory area. Here fields are called union member or union element. Union declaration has more than one variable declaration but only called union member or union element. Union declaration has more than one variable declaration but only one can be used at a time. For example

one can be used at a time. For example union abc

union abc {

{ cchhaar r aa;;

int b;

int b;

float c;

float c;

double d;

double d;

}v;

}v;

The storage of memory locations to these members are represented as below The storage of memory locations to these members are represented as below 1

1000011 11000088

G

G 2222 22..33 224422..3333333333336677

aa

 b  b