<p> 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. </p><p>FUNCTION Use: Making the program modular and compact(reduction in program size) and avoid ambiguity. </p><p>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. </p><p>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.</p><p>FUNCTION Definition: A FUNCTION declaration plus the body of the function(statements contained in it) make up the definition of the FUNCTION </p><p>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. </p><p>Actual variables/parameter: The variables associated with function name during FUNCTION call are called actual variables i..e parameters that appear in FUNCTION call. </p><p>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. </p><p>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). </p><p>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.</p><p>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:</p><p>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.</p><p>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 <iostream.h> 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 “<<sum; } cout<<”column sum is”<<sum;} 4. sum of right diagonals a[3][3] 5. Transpose of a matrix a[3][2] 6. Display the lower half a[3][3] sum=0; stored in b[2][3] for(row=0;row<3;row++) for(row=0,col=2;row<3;row++,col--) for(row=0;row<3;row++) for(col=0;col<3;col++) sum+=a[row][col] for(col=0;col<2;col++) if(row<col) b[col][row]=a[row][col]; cout<<a[row][col]; 7. Display the Upper Half a[3][3] 8. ADD/Subtract matrix 9. Multiplication a[2][3]*b[3] a[2][3]+/-b[2][3]=c[2][3] [4]=c[2][4] for(row=0;row<3;row++) for(row=0;row<2;row++) for(col=0;col<3;col++) for(row=0;row<2;row++) for(col=0;col<4;col++) if(row>col) for(col=0;col<3;col++) {c[row][col]=0; cout<<a[row][col]; c[row][col]=a[row][col]+/- b[row] for(k=0;k<3;k++) [col] c[row][col]+=a[row][k]*b[k] [col] } One Dimensional Arrays 1. Reversing an array 2.Exchanging first half with second half 3. Interchanging alternate locations (size j=N-1; (array size is even) of the array will be even) for(i=0;i<N/2;i++) for(i=0,j=N/2;i<N/2;i++,j++) for(i=0;i<N;i=i+2) { temp=a[i]; { temp=a[i]; { temp= a[i]; //swapping the alternate a[i]=a[j]; a[i]=a[j]; a[i]=a[i+1]; //locations using third a[j]=temp; j--; } a[j]=temp; } a[i+1]=temp;} //variable ------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 “<<sum; } cout<<”column sum is”<<sum;} 4. sum of right diagonals a[3][3] 5. Transpose of a matrix a[3][2] 6. Display the lower half a[3][3] sum=0; stored in b[2][3] for(row=0;row<3;row++) for(row=0,col=2;row<3;row++,col--) for(row=0;row<3;row++) for(col=0;col<3;col++) sum+=a[row][col] for(col=0;col<2;col++) if(row<col) b[col][row]=a[row][col]; cout<<a[row][col]; 7. Display the Upper Half a[3][3] 8. ADD/Subtract matrix 9. Multiplication a[2][3]*b[3] a[2][3]+/-b[2][3]=c[2][3] [4]=c[2][4] for(row=0;row<3;row++) for(row=0;row<2;row++) for(col=0;col<3;col++) for(row=0;row<2;row++) for(col=0;col<4;col++) if(row>col) for(col=0;col<3;col++) {c[row][col]=0; cout<<a[row][col]; c[row][col]=a[row][col]+/- b[row] for(k=0;k<3;k++) [col] c[row][col]+=a[row][k]*b[k] [col] } One Dimensional Arrays 1. Reversing an array 2.Exchanging first half with second half 3. Interchanging alternate locations (size j=N-1; (array size is even) of the array will be even) for(i=0;i<N/2;i++) for(i=0,j=N/2;i<N/2;i++,j++) for(i=0;i<N;i=i+2) { temp=a[i]; { temp=a[i]; { temp= a[i]; //swapping the alternate a[i]=a[j]; a[i]=a[j]; a[i]=a[i+1]; //locations using third a[j]=temp; j--; } a[j]=temp; } a[i+1]=temp;} //variable</p><p>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 <stdio.h> 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</p><p>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<B;I++)cout<<M;} void main(){ pattern(‘*’); pattern(‘#’,4);} Constants Arguments- A C++ function may have constant argument(s). These arguments are treated as constants. These values cannot be modified by the function. WE have to use the keyword const in the function prototype. E.g. void max(const float x, const float y, const float z);//const informs the compiler that the arguments having const should not be modified by the function max. Data Storage Types: It determine about the storage of a variable. There are 4 storage class specifiers. Storage Place Default Scope Lifetime Initial value Auto Memory Garbage value Local to the function Till the parent function is running Register( it provides a fast CPU Garbage value Local to the function Till the parent function is access than those in memory Registers( limited running number) Static Memory 0 (zero) Local to the function Entire program execution Extern Memory 0(zero) File scope Entire program execution Static: There are static local and static global variable. A static global variable is available for the entire file in which it is declared. A static local variable is initialized only once when the function is called first time. It holds (retains) its value even when the control leaves the function and the value last time around will be used when the control enters next time in the function. It can be accessed only with the function it is declared but has the lifetime of a global variable. Extern: When a program is divided into 2 or more files, then a global variable should not be declared separately for each file. It is preceded by the keyword extern, which tells the compiler that the variable has been declared somewhere else and no separate memory is allocated to it. randomize()- <stdlib.h> It initializes the random number generator with a random number. random(int x)- <stdlib.h> 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;i<n;i++) beg=0,end=N-1,mid; settles down.(for ascending order) Apply process to A[i] while(beg<=end) for(i=0;i<=n-2;i++)//for passes {mid=(beg+end)/2;//mid should be int { for(j=0;j<=n-2-i;j++) 2. Linear Search: if(A[mid]= =NUM) { if(A[j]>A[j+1]) for(i=0;i<n;i++) {POS=mid; break; } {temp=A[j];//swap A[j] if(A[i]= =NUM) else if(A[mid]<NUM) with { POS=i ;//position of element to beg=mid+1; A[j]=A[j+1]; // A[j+1] be else if(A[mid]>NUM) 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</p><p>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<n-1;i++) for(i=1;i<n;i++) array has to be more than elements { small=A[i]; phs=i; { temp=A[i]; taken for(j=i+1;j<n;j++) j=i-1; { if(A[j]<small; while(temp<A[j] && j>=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;i<N-1;i++) for(i=0;i<M;i++) flag=0; A[i]=A[i+1]; C[i]=A[i]; for(i=0;i<n;i++) N--; for(i=0;i<N;i++) { if(A[i]>NUM) 10. Delete(value) OR in Sorted Array C[M+I]=B[i]; {POS=i; flag=1; break; } flag=0; } for(i=0;i<N;i++) 12. Merge Two sorted Arrays if(flag= =0) POS=N;//insert at last if(A[i]= =NUM){POS=i;flag=1;break;} both in Asc. Order for(i=N-1 ; 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<N-1;i++) A[POS]=NUM; A[i]=A[i+1]; }//end of else N++; N--; 12. i=0, j=0,k=0; STRINGS 2. Convert First, Middle and while((i<M) &&(j<N)) 1. Count the no. of words last Name { if(A[i]<B[j]) char s[80]; char s[80]; {C[k]=A[i]; i++;k++;} gets(s); gets(s); else int i=0,wc=0; int i=0; { C[k]=B[j];j++,k++;} while(s[i]!=’\0’) while(s[i]!=’\0’) } { while(s[i]= =’ ‘ && s[i]!=’\0’)i++; { while(s[i]= =’ ‘ && s[i]! if(i<M) if(s[i]!=’\0’) =’\0’)i++; { for(p=i;p<M; p++) {while(s[i]!=’ ‘ && s[i]!=’\0’)i++; s[i]=toupper(s[i]); { C[k]=A[p];k++; } } wc++; i++; else if(j<N) } while(s[i]!=’ ‘ && s[i]!=’\0’) { for(p=j;p<N; p++) } { s[i]=tolower(s[i]); { C[k]=B[p];k++; } } i++; } } One Dimensional Array: ( A is array, N is no. of elements, NUM to be searched, POS is the position in the array)</p>
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-