Date post: | 13-Mar-2016 |
Category: |
Documents |
Upload: | callum-rocha |
View: | 78 times |
Download: | 1 times |
switch statementFor 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.
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”; }
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.
#include<iostream.h>
const int Arsize=20; // define a constant integerint 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++; }
cout<<“\n”<<spaces<<endl; return 0;
}
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 ++; } }
cout<<total;
Writing to a Text FileWe 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;
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();
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
#include<iostream.h>#include<fstream> //file I/O#include<cstdlib> // 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); }
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”;
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;}
Function Definition
Syntax typeName functionName(parameterList) { statements; return value; }
#include<iostream.h> 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;}
int simple( int a,b); // not acceptable int simple( int , char);
We can send array to function as a parameter.
#include<iostream.h> 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;}
We can write
int sum_arr(int *arr, int n);
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
double pam( int ); double (*pf) (int); pf = pam;
double ned(double); int ted (int); double (*pf)(int) pf= ned; /// ? pf= ted; /// ?
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);
#include<iostream.h> 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); }
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”;}
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.
Example for void function
void recurs( argumentList){ statemts1; if (test) recurs (arguments); statements2; }
More Example#include<iostream.h> 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;}
long factorial ( int n){ if (n <= 1) return 1; else return (n*factorial(n-1));}
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.
void print_reverse(int n ){ cout<<“enter a number : zero for
quit”<<endl; cin>>n; if( n ==0) return; print_reverse(n); cout<<n; }
Write a program to print out all the string of size n of 0, 1 . ( n is an input);
include <iostream.h> include<stdlib.h> 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);}
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); }
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.
Inline function
When the time execution of a function is short it’s better to declare it as an inline function
#include<iostream.h> inline double square(double x){ return x*x; }
int main(){ double a,b; double c=13.0; a= square(5.0); b=square(4.5+7.5); cout<<a<< “ “<<b; return 0; }
Call by Value & Call by Referencevoid 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; }
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;}
int main(){ int num1=4, int num2=9; swap1(num1,num2); cout<<num1<<“ “<<num2; swap2( &num1, &num2); cout<<num1<<“ “<<num2;}
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
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.
void print ( char * str, int length); void print( double d, int length); void print( long l, int d); void print (long v, int length);