Week7 - Lab1 and Lab2
Week7 - Lab1 and Lab2
Experiment: 7
PART A
Aim: Programming using user defined functions & strings (character array)
Theory:
Functions
- A function is a subprogram that performs a well-defined task.
- Every C++ program is a collection of functions.
- There should be at least one function called main( ).
- Every program execution starts with main( ) & main( ) is only function known by the
Operating System, it is used as system call. Its default return type is int.
- C++ supports two types of functions
o Built-in Library Functions
C++ provides these functions.
Examples are, sqrt(), pow(), sqrt(), strlen() etc.
o User Define Functions
Programmer can define their functions.
- Advantages of functions:
- Program maintenance & debugging is easy
- Reusability of code.
- Reduces program development time.
- Work distribution is possible.
- Easy to understand & easy to write.
- Reduces coding size.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
- Syntax:-
function_name(parameter_list);
- Example:-
1. add(a,b);
2. add(&a, &b);
- Example 1 shows call by value & 2 is call by address.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
Function Definition.
Categories of Functions
WAP to perform addition of two WAP to perform addition of two numbers, using user defined function
numbers, without using
function 1. Function without Argument 2. Function without Argument 3. Function with Argument & 4. Function with Argument &
& Without Return Type & with Return Type without Return Type with Return Type
#include <iostream> #include <iostream> #include <iostream> #include <iostream> #include <iostream>
using namespace std; using namespace std; using namespace std; using namespace std; using namespace std;
int main() { void add(){ int add(){ void add(int a,int b){ int add(int a,int b){
int a,b; int a,b; int a,b; int c; int c;
cout<<"Enter two number"<<endl; cout<<"Enter two number"<<endl; cout<<"Enter two number"<<endl; c=a+b; c=a+b;
cin>>a>>b; cin>>a>>b; cin>>a>>b; cout<<"Add is "<<c; return c;
int c=a+b; int c=a+b; int c=a+b; } }
cout<<"Add is "<<c; cout<<"Add is "<<c; return c;
return 0; } } int main() { int main() {
} int a,b; int a,b;
int main() { int main() { cout<<"Enter two number"<<endl; cout<<"Enter two number"<<endl;
add(); int d = add(); cin>>a>>b; cin>>a>>b;
return 0; cout<<"Add is "<<d; add(a,b); int c=add(a,b);
} return 0; return 0; cout<<"Add is "<<c;
} } return 0;
}
Enter two number Enter two number Enter two number Enter two number Enter two number
35 35 35 35 35
Add is 8 Add is 8 Add is 8 Add is 8 Add is 8
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
Finding Smallest element in 1D Array…using function Finding Smallest element in 2D Array (matrix)…using function
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int minelement(int a[],int n){ int minelement(int a[][3],int m,int n){
int m=a[0]; int min=a[0][0];
for(int i=0;i<n;i++){ for(int i=0;i<m;i++){
if(m>a[i]){ for(int j=0;j<n;j++)
m=a[i]; if(min>a[i][j]){
} min=a[i][j];
} }
return m; }
} return m;
int main(){ }
int a[] ={10,9,5,3,23,34},n=6,m; int main(){
m=minelement(a,n); int a[][3]={10,9,1,30,23,34,12,35,67},n=3,m=3,min;
cout<<"Smallest element is"<<m; min=minelement(a,m,n);
} cout<<"Smallest element is "<<min;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
Recursion
- Recursive function or recursion is a function that contains a call to itself.
- Works similar to divide and conquer.
- Recursion must contain one exit condition that can be satisfied, otherwise the recursive function
- will call itself repeatedly until the runtime stack overflows.
- Example: -
#include<stdio.h>
main( ){
cout<<“I will stop\n”;
main( );
}
// Program to find factorial of a number using // Program to find sum of a series using recursion // Program to find sum of following series using
recursion #include<iostream> function (Recursive function call)
#include<iostream> using namespace std; 1!+2!+3!+4!+5!+……+N!
using namespace std; int sum(int n) { #include<iostream>
int fact(int n) { if(n == 1) using namespace std;
if(n == 1) return 1; int fact(int n) {
return 1; if(n == 1)
return n + sum(n - 1); return 1;
return n * fact(n - 1); } return n * fact(n - 1);
} int main() { }
int main() { int n,s; int sum(int n) {
int n,f; cout << "Enter a positive integer: "; if(n == 1)
cout << "Enter a positive integer: "; cin >> n; return 1;
cin >> n; s=sum(n);
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
Draw flowchart and write algorithm for printing the factorial of n using subroutine(function), where n is given as input.
Algorithm :
1. Start
2. Display “Enter the value for n
3. Read n
4. fact = Factorial(n)
5. Display fact
6. Stop
Subroutine Factorial(n)
1. Start
2. f=1
3. If(n==1) Then
Go to step 07
4. f = f *n
5. n = n-1
6. Goto Step 03
7. return f
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
example:-
char month[15];
char studentname[20];
char s1[10],s2[10];
- Initialization of String…
Syntax:-
char string_name[size] = {characters in single inverted comma,
separated with comma};
or
char string_name[size] = “string”;
Example:-
char subject[10] = {‘c’,‘p’,‘p’, ‘\0’ };
char name[10] = “cpp”;
char name[ ] = “cpp”;
- Reading String…
o There are various ways to read the stings, we can read it using cin statement
- Displaying String…
o There are various ways to display stings, we can use cout statement to display
#include<iostream>
int main(){
char s[15]="Programming";
cout<<"String is "<<s;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
#include<iostream>
int main(){
char s[15];
cin>>s;
cout<<"Welcome"<<s;
#include<iostream>
#include<cstring>
int main(){
char s[15]="Programming";
cout<<"String is \n";
cout<<s[i];
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
String Length…
- strlen(str_name);
WAP to find length of a string using string handling function.
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="C++",s2[15]="Programming";
int l1,l2;
l1=strlen(s1);
l2=strlen(s2);
cout<<l1<<" "<<l2;
}
l++;
cout<<l;
}
String Concatenation…
- strncat(s1,s2 )
WAP to concatenate two strings using string handling function
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="Kit",s2[10]="Kat";
strcat(s1,s2);
cout<<" String is " <<s1;
}
String Copy…
- strncpy(s1,s2)
WAP to copy on string to another using string handling function.
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="CPP",s2[10]="Python";
strcpy(s1,s2);
cout<<"After Copying String is "<<s1;
}
- strncpy(s1,s2,n)
WAP to copy n characters in one string to another using string handling function.
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="",s2[10]="CPP";
strncpy(s1,s2,1);
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering
String Compare…
- strcmp(s1,s2)
WAP to compare two strings using string handling function.
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="CPP",s2[10]="CPP2";
int c = strcmp(s1,s2);
if(c==0)
cout<<"Equal";
else
cout<<"Not Equal";
}
- strncmp(s1,s2,n)
WAP to compare two strings upto n characters using string function.
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="C++",s2[10]="CPP2";
int c = strncmp(s1,s2,1);
if(c==0)
cout<<"Equal";
else
cout<<"Not Equal";
}
Tasks: