Array: It Is a Derived Data Type s1

Total Page:16

File Type:pdf, Size:1020Kb

Array: It Is a Derived Data Type s1

Function- It groups a number of program statements into a single unit and gives it a name. This unit can be invoked from other parts of the program. Functions are the main building blocks of C++ programs. By separating and coding parts of your program in separate module.

FUNCTION Use: Making the program modular and compact(reduction in program size) and avoid ambiguity.

FUNCTION Prototype: It is a declaration that tells the program about type of the return value of the function and the number and type of each arguments. It is also known as FUNCTION declaration. FUNCTION Prototype are required for those functions which are intended to be called be fore they are defined. It helps compiler in static type checking whether the FUNCTION call is proper or not.

FUNCTION call: A function can be called from another function by using its name. The FUNCTION name must be followed by a set of actual parameters, enclosed in parenthesis separated by commas.

FUNCTION Definition: A FUNCTION declaration plus the body of the function(statements contained in it) make up the definition of the FUNCTION

Formal parameters/variable: It is a list of variables that will accept different types of values being supplied to the function by the calling program. They are declared after the FUNCTION name and inside parenthesis i.e. the parameters that appear in FUNCTION definition.

Actual variables/parameter: The variables associated with function name during FUNCTION call are called actual variables i..e parameters that appear in FUNCTION call.

Pass By Value: When an argument (local variable) is passed by value, a copy of the variable’s value is assigned to the receiving functions parameter. i.e copy of actual are made in formal and if formal parameters are changed the changes will not be reflected back in actual parameters.

Pass By Reference: When you pass an argument by reference or by address, the variables address is assigned to the receiving function’s parameter, if the receiving function changes the variable, it is also changed in the calling function i.e changes made in formal parameters are reflected back in actual parameters(Arrays are always passed by reference).

Use of return- 1)It is a jump statement 2) It transfers the control back to the calling function 3) If you want to return a value from a function to its calling function, put the return value after return statement.4) There can be as many return statements as you want but at a time one will be executed 5) return can return only one value.

Scope Rules of Variables: The scope of a variable means how the different parts of the program can access that variable. There are four types of variables:

1. LOCAL Variable: The variables which are declared inside any function are called local variable. Its scope is then limited to the function itself. The variable is said to be accessible or visible within the function only and has a local scope. E.g. void main(){ int b; // b is local variable for main()} Thus variables that are declared inside an opening and terminating curly brace are termed as local variables.

2. GLOBAL Variable: The variables which are declared globally (before main() or outside of any function in C+ +) are called global variable. The usual place of declaration is in the beginning of the program file i.e. before all function definitions so that it is visible to all functions and exists till the program terminated. e.g. #include Note: In a situation where local and global has the same variable name then local always int i;// i is global hide the global variable. In case you wan to use global then specify ::global variable name Thus global variables are the variables that are known throughout the program and may be sued by any part of the program. They hold their value throughout the execution of the program. Two Dimensional Arrays 1. sum of rows a[3][2] 2. sum of columns a[2][3] 3. sum of left diagonals a[3][3] for(row=0;row<3;row++) for(col=0;col<3;col++) sum=0; { sum=0; {sum=0; for(row=0;row<3;row++) for(col=0;col<2;col++) for(row=0;row<2;row++) sum+=a[row][row] sum+=a[row][col]; sum+=a[row][col]; cout<<”sum is “<col) for(col=0;col<3;col++) {c[row][col]=0; cout<col) for(col=0;col<3;col++) {c[row][col]=0; cout<

Input-Output Functions C supports 3 main categories of I/O functions: stream, console(it comprises of both keyboard-std. Input device and VDU-std. Output device) and low level. We have already used 2 C++ iostream functions cout and cin. Include Function Type Description char ch; ch=getchar(); fflush(stdin); getchar( ) Stream Read a character from stream char ch=’A’; putchar(ch); putchar() Stream write a character to the stream getc( ) Stream Read a character from stream putc( ) Stream write a character to the stream gets() Stream Read a string from stream puts( ) Stream write a string to the stream cgets( ) console Read a string from system console getche( ) Console Read a character from system console putch( ) Console write a character on system console cputs Console write a string on system console

By default all stream I/Os are buffered. Buffered I/O: Whenever we read from the keyboard, a block of data is stored in a buffer called input buffer. The input stream, therefore, reads from the buffer with the help of a pointer until the input buffer is empty. As soon as the buffer becomes empty, the next block of data is transferred into the buffer from I/O device. Similarly, data written by an output stream pointer is not directly written onto the device, but into an output buffer. As soon as the output buffer becomes full, a block of data is written on to the I/O device making the output buffer empty. Input Buffer Block of data Data Stream I/O Devices Pointers Output Buffer Block of data Data Recursion- In C++ a function can invoke itself, this is called recursion. A function is said to be recursive if there exists a statement in its body for the function call itself. We must take care that there exists a reachable termination condition inside the function so that function may not be invoked endlessly. E g. int fact(int num){if(num= =0) return 1; else return (num * fact(num-1));} Default Arguments- C++ allows a function to assign a parameter the default value in case no argument for that parameter is specified in the function call. E.g. void pattern(char M, int B=2){for(int I=0;I It initializes the random number generator with a random number. random(int x)- It returns a random number between 0 and (x-1). 1. Traversing a linear array: 3. Binary Search: 4. Bubble Sort: Highest element for(i=0;iA[j+1]) for(i=0;iNUM) using break;} //searched end=mid-1; }//end of while A[j+1]=temp;//third if(i= =N) if(beg>end)cout<<”searchunsuccessful”; variable cout<<”search unsuccessful”; }//end of if condition }//end of inner for }//end of outer for

5. Selection Sort(in ascending 6. Insertion Sort: (in ascending order) 7. Inserting an element NUM at order) Kth position Note: the size of the for(i=0;i=0) k--; {small=A[j]; phs=j; } { A[j+1]=A[j]; for(i=N-1;i>=k;i--) }//end of inner for j--; A[i+1]=A[i]; //shifting temp=A[i];//swap A[i] with } A[k]=NUM; //at kth position A[phs] A[j+1]=temp; insert A[i]=A[phs]; } N++; //no. of elements have to A[phs]=temp; be //increased by one } //end of outer for 8. Inserting in a Sorted Array 9. Delete Kth Element 11. Merge (One follows another): (ascending order) –inserts NUM k--; Array A[M] +B[N]=C[M+N] after finding proper position POS for(i=k;iNUM) 10. Delete(value) OR in Sorted Array C[M+I]=B[i]; {POS=i; flag=1; break; } flag=0; } for(i=0;i=POS ; i--) if(flag= =0) cout<<”element not found”; A[M] + B[N]=C[M+N] A[i+1]=A[i]; else{ for(i=POS;i

Recommended publications