HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C++

ARRAY

ARRAY

We discussed briefly about an array in ‘C’. Like, in ‘C++’ also, Initialization of single dimensional array is int I[5]={ 2,4,6,8,10 }; Initialization of two-dimensional character array is char arr[][8] = {“KISHORE”, “NAVEENA”, “VINOTHA”}; (OR) char arr[3][8] = { { ‘K’, ’I’, ’S’, ’H’, ’O’, ’R’, ’E’, ’\0’ }, { ‘V’, ’I’, ’N’, ’O’, ’T’, ’H’, ’A’, ’\0’ }, { ‘N’, ’A’, ’V’, ’E’, ’E’, ’N’, ’A’, ’\0’ } }; // EX-6: PGM ILLUSTRATES TWO-DIMENSIONAL CHARACTER ARRAY #include #include void main() { char arr[][8] = { {'V', 'I', 'N', 'O', 'T', 'H', 'A','\0'}, {'N', 'A', 'V', 'E', 'E', 'N', 'A','\0'} }; clrscr(); cout<<"\nFirst String is : "< #include void main() { int month, nod; int days[12]={ 31,28,31,30,31,30,31,31,30,31,30,31 }; clrscr(); lbl: cout<<"\n Enter the month (1 to 12): "; cin>>month; if(month>12) { cout<<"\nINVALID MONTH"; goto lbl; } cout<<"\n Enter the day(1 to 31): "; cin>>nod; if(nod>31) { cout<<"\nINVALID DATE"; return; } for(int j=0;j
POINTERS: At the time of execution itself, the memory allocates the required space. This can only be achieved with the help of a special type of variable, a pointer variable, also called a pointer. A pointer is a variable that stores the memory address of another variable. Advantages: • Pointers allow direct access to individual bytes in the memory. Thus, data in the memory is accessed faster than through an ordinary variable. This speeds up the execution of programs. • Pointers allow the program to allocate memory dynamically, only when required, with the help of the new operator. They also allow the program to free the memory when it is no longer required. This is done with the help of the delete operator. You will learn about these operators later. Uses: • In database program, where the no. of records to be stored is unknown at the time of designing the database. • In word-processing programs (the no. of words stored by the program in memory is unknown at the time of writing the program. This is also known as the compile-time of a program. Run-time implies that the program is executing). • Mostly used in any software like an operating system, which interacts directly with the hardware. // EX-8: PGM ILLUSTRATES THE USE OF POINTERS #include #include void main() { int a,b; int *pt; a=50; b=80; clrscr(); pt=&a; cout<<*pt< #include int n[5]= { 5,10,15,20,25 }; void main() { int *ptr; ptr=n; clrscr(); cout<<*(ptr+2)<