Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch statement For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one.

Similar presentations


Presentation on theme: "Switch statement For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one."— Presentation transcript:

1 switch statement For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one of the several possible cases break is used to jump out when match happens.

2 char idChar; int aNumber, bNumber, cNumber; swtich( idChar) { case ‘A’: aNumber=aNumber+1; break; case ‘B’: bNumber=bNumber+1; break; case ‘C’: cNumber=cNumber+1; default: cout<<“error”; }

3 If we don’t use break it executes all commands after the selected choice. break is used to skip over a part of a program. If we use break in loop it cause to jump out of the loop. continue is used in loop and cause a program to skip the rest of the loop.

4 #include const int Arsize=20; // define a constant integer int spaces=0; int main() { char line[Arsiz]; cout<<“enter a line of text”<<endl; cin.get(line,Arsize); for(int i=0; line[i]!=‘ \0’;i++) { cout<<line[i]; if (line[i]==‘.’) break; if ( line[i] != ‘ ‘) continue; spaces++; }

5 cout<<“\n”<<spaces<<endl; return 0; }

6 What is the output of the following code int i,j; int counter=0; int total=0; int n; cin>>n; for(i=0; i<n ; i++) { if( counter % 2 ) continue; for( j=0; j< n-i; j++) { total+= counter – j; if ( ! (j % 2) ) continue; counter ++; }

7 cout<<total;

8 Writing to a Text File We need to declare the header file, which is fstream The fstream header file defines an ofstrem class for handling the output. ofstream outFile; outFile.open(“sample.txt”); char name[20]; cin>> name; outFile<<name;

9 cout<<“enter a file name “<<endl; cin>>name; ofstream fout; fout.open(name); if (! fout) // if ( !fout.is_open()) { cout<<“ file does not exist”<<endl; exit(1); } // if file exist open it otherwise it creates the file // we check whether it was successful or not double number; cin>>number; fout<<number; fout.close();

10 Reading from Input File We need to declare the header file, which is fstream The fstream header file defines an ifstrem class for handling the input file

11 #include #include //file I/O #include // for exit() const int size=60; int main() { char filename[size]; ifstream infile; cout<<“enter a file name”; cin.getline(filename,size); infile.open(filename); if( !infile.is_open()) { cout<<“not open”; exit(1); }

12 double value; double sum=0.0; int count=0; infile>>value; while ( infile.good()) // while good input { ++count; sum+=value; infile>>value; } if (infile.eof()) cout<<“end of file”<<endl; else if( infile.fail()) cout<<“data mismatch \n”;

13 else cout<<“with no reason terminate \n”; if (count==0) cout<<“ no data \n”; else { cout<<count<<endl; cout<<“sum = “<<sum; cout <<“Average= “<<sum/count<<endl; } infile.close(); return 0; }

14 Function Definition Syntax typeName functionName(parameterList) { statements; return value; }

15 #include int simple(int val1, int val2); // int main() { int a,b,sum; cout<<“enter two numbers”<<endl; cin>>a>>b; sum=simple(a,b); cout<<“the sum is = “<<sum; } int simple ( int val1, int val2) { int total; total=val1+val2; return total; }

16 int simple( int a,b); // not acceptable int simple( int, char); We can send array to function as a parameter.

17 #include const int ArSize=8; int sum_arr(int arr[], int n); int main() { int numbers[ArSize]={1,3,4,8,9,-1,2,0}; int sum=sum_arr(numbers,ArSize); cout<<“sum of the numbers is “<<sum; } int sum_arr(int arr[ ],int n) { int total=0; for(int i=0;i<ArSize;i++) total+= arr[i]; return total; }

18 We can write int sum_arr(int *arr, int n);

19 Function and Pointers double (*pf) (int ); // pf points to a function that return double; double *pf(int); // pf is a function that return a pointer to a // double

20 double pam( int ); double (*pf) (int); pf = pam; double ned(double); int ted (int); double (*pf)(int) pf= ned; /// ? pf= ted; /// ?

21 double pam(int); double (*pf) (int); pf=pam; double x=pam(4); double y=(*pf) (5); // call pam; We can write double y= pf(5);

22 #include double betsy(int); double pam(in); void estimate (int lines, double (*pf)(int)); void main() { int code; cout<<“how many line of code do you need?\n”; cin>>code; cout<<“ here is betsy estimate\n”; estimate(code, betsy); cout<<“ here is pam estimate\n”; estimate(code, pam); }

23 double betsy( int lns) { return 0.05 * lns; } double pam(int lns) { return 0.03*lns+0.0004*lns*lns; } void estimate(int lns, double (*pf)(int)) { cout<<lns<<“lines will take “; cout<<(*pf)(lns)<<“hours(s) \n”; }

24 Recursive function In C++ a function can call itself. Note that main() can’t call itself. Recursive function are used in backtracking. Also are used in Artificial Intelligence.

25 Example for void function void recurs( argumentList) { statemts1; if (test) recurs (arguments); statements2; }

26 More Example #include long factorial ( int n); int void main() { int number; long fac_num; cout<<“enter a number less than 11”; cin>>n; fac_num=factorial(n); cout<<“factorial of “<<n<<“ is “<<fac_num; }

27 long factorial ( int n) { if (n <= 1) return 1; else return (n*factorial(n-1)); }

28 Write a program to read some numbers and print out them in reverse order. Input zero to quit. We are not allowed to use array.

29 void print_reverse(int n ) { cout<<“enter a number : zero for quit”<<endl; cin>>n; if( n ==0) return; print_reverse(n); cout<<n; }

30 Write a program to print out all the string of size n of 0, 1. ( n is an input);

31 include void all_string(int n, int max_size, int arr[ ]) { if(n==max_size) { for( int i=0; i<max_size; i++) cout<<arr[i]; cout<<endl; return; } arr[n]=0; all_string(n+1,max_size; arr); arr[n]=1; all_string(n+1,max_size;arr); }

32 int void main(); { int size; cout<<“enter an positive number”; cin>>size; if( size < 1) { cout<<“error”; exit(1); } int array=new int[size]; all_string(0,size,array); }

33 Write a program to print out power set of set {1,2,…,n} ( n is an input). Write a Maze program. Write a program to find all m-subset of an n-set.

34 Inline function When the time execution of a function is short it’s better to declare it as an inline function #include inline double square(double x) { return x*x; }

35 int main() { double a,b; double c=13.0; a= square(5.0); b=square(4.5+7.5); cout<<a<< “ “<<b; return 0; }

36 Call by Value & Call by Reference void swap ( int a, int b) { int temp; temp=a; a=b; b=temp; } int main() { int num1=4, num2=9; swap(num1,num2); cout<<num1<< “ “ <<num2; }

37 void swap1(int &a, int &b) { int temp; temp=a; a=b; b=temp; } void swap2(int *a, int *b) { int temp; temp=*a; *a=*b; *b=temp; }

38 int main() { int num1=4, int num2=9; swap1(num1,num2); cout<<num1<<“ “<<num2; swap2( &num1, &num2); cout<<num1<<“ “<<num2; }

39 When Using Reference Argument To allow you to alter a data in the calling function To speed up the program by passing a reference instead of an entire data object

40 Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more than one function with the same name but they must have different signatures.

41 void print ( char * str, int length); void print( double d, int length); void print( long l, int d); void print (long v, int length);


Download ppt "Switch statement For selecting one choice from a list of choices switch statement evaluates an expression to determine a value Match the value with one."

Similar presentations


Ads by Google