[go: up one dir, main page]

0% found this document useful (0 votes)
38 views19 pages

Week7 - Lab1 and Lab2

The document discusses programming using user defined functions and strings in C++. It provides examples of function prototypes, definitions, calls, and categories of functions. Specifically, it discusses: 1. The basics of functions in C++, including definition, declaration, calling, advantages, and types of functions. 2. Details on user defined functions, including prototypes, parameters, return types, and function bodies. 3. Four examples of functions for adding two numbers with different categories: with/without arguments and return types.

Uploaded by

roastiespvt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views19 pages

Week7 - Lab1 and Lab2

The document discusses programming using user defined functions and strings in C++. It provides examples of function prototypes, definitions, calls, and categories of functions. Specifically, it discusses: 1. The basics of functions in C++, including definition, declaration, calling, advantages, and types of functions. 2. Details on user defined functions, including prototypes, parameters, return types, and function bodies. 3. Four examples of functions for adding two numbers with different categories: with/without arguments and return types.

Uploaded by

roastiespvt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering / School of


Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Experiment: 7
PART A

(PART A: TO BE REFERRED BY STUDENTS)

Aim: Programming using user defined functions & strings (character array)

Learning Outcomes: The learner would be able to


1. Understand the syntax of function declaration, definition and call.
2. Solve problems using user defined functions
3. write program using recursion
4. Use string handling functions
5. Implement programs using inline functions and macros

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

User Define Functions: -

- User can define their functions.


- Basic Parts/Components/Elements of functions: -
1. Function Prototype Or Function Declaration.
2. Function Calling or invocation
3. Function Definition.
Function Prototype or Declaration.

- Like variables, functions are also declared called function prototype.


- Function should be declared before they are used.
- We can declare function inside main() or above the main().
- Function declaration or prototype is used to tell the compiler about following:
- Return type of function.
- Number of parameters or arguments.
- Type of parameters.
- Name of the function.
- Syntax:-

return_type (datatype arg1, datatype arg2, ……..datatype argn);


- In the above Syntax, we can also eliminate the names of arguments or skip function
declarations in C++.
- Example:-
- int add(int a, int b );
- int add(int, int );
Function Calling.
- Function is called when a semi-comma follows a function name.
- Whenever a function is called, actual parameters of calling functions are copied into
formal parameters of called function(if any).
- The number of actual and formal parameters should be the same(if any).
- The names of actual and formal parameters may be the same or different(if any).
- We can call functions using(also called Parameter Passing Techniques ) call by value,
call by reference & call by address.

- 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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Function Definition.

- User define functions definition consists of the following two parts.


- Function Header.
o it contains “return type‟, “function name,‟ & “argument list.‟
o A semi-colon does not terminate the function header.
- Function Body
o The function body should be enclosed in curly braces „{„ & „}‟.
o It contains executable statements along with return value, if any.
o These executable statements perform well-defined tasks.
- Syntax:-
return_type function_name ( argument_list){
//function body.
}
- Example:-
int add(int a, int b){
return a+b;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Parameter Passing Techniques

Call by Value vs call by reference vs call by address


Call By Value Call By Reference Call By Address
Normal/Ordinary Variables are used as a Reference Variables are used as a parameter Pointer variables are used as a parameter
parameter
Formal parameter can’t update the value of actual Formal parameter update the value of actual Formal parameter update the value of actual
parameter (refer line no 4 of output) parameter (refer line no 4 of output) parameter (refer line no 4 of output)
#include <iostream> #include <iostream> #include <iostream>
using namespace std; using namespace std; using namespace std;
void swap(int a,int b){ void swap(int &a,int &b){ void swap(int *a,int *b){
cout<<"In swap() before swapping a="<<a<<" b="<<b<<endl; cout<<"In swap() before swapping a="<<a<<" b="<<b<<endl; cout<<"In swap() before swapping a="<<*a<<" b="<<*b<<endl;
int c=a; int c=a; int c=*a;
a=b; a=b; *a=*b;
b=c; b=c; *b=c;
cout<<"In swap() after swapping a="<<a<<" b="<<b<<endl; cout<<"In swap() after swapping a="<<a<<" b="<<b<<endl; cout<<"In swap() after swapping a="<<*a<<" b="<<*b<<endl;
} } }
int main() { int main() { int main() {
int a=10,b=20; int a=10,b=20; int a=10,b=20;
cout<<"In main() before swap() call a="<<a<<" b="<<b<<endl; cout<<"In main() before swap() call a="<<a<<" b="<<b<<endl; cout<<"In main() before swap() call a="<<a<<" b="<<b<<endl;
swap(a,b); swap(a,b); swap(&a,&b); //call by address
cout<<"In main() after swap() call a="<<a<<" b="<<b<<endl; cout<<"In main() after swap() call a="<<a<<" b="<<b<<endl; cout<<"In main() after swap() call a="<<a<<" b="<<b<<endl;
return 0; return 0; return 0;
} } }
Output:- Output:-
In main() before swap() call a=10 b=20 Output:- In main() before swap() call a=10 b=20
In swap() before swapping a=10 b=20 In main() before swap() call a=10 b=20 In swap() before swapping a=10 b=20
In swap() after swapping a=20 b=10 In swap() before swapping a=10 b=20 In swap() after swapping a=20 b=10
In main() after swap() call a=10 b=20 In swap() after swapping a=20 b=10 In main() after swap() call a=20 b=10
In main() after swap() call a=20 b=10
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Passing Arrays to functions

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Macro Function Inline Function

- Macros starts with # - Inline function is defined with inline keyword


Example:
o File Inclusion
 #include<iostream>
o Macros as constants
 #define PI 3.147
o Macros with arguments (Macro Functions)
 #define max(a,b) a>b?a:b
- Macros functions are expanded as inline at pre-processing by pre- - Inline functions are expanded at compile time by compiler.
processor (before compliler)
- Speedup the execution of the program - Compiler check the execution time & expanded it if required.
- Example:- - Example:-
#include <iostream> Macro body #include <iostream>
#define add(a,b) (a+b) using namespace std;
Macro Function Inline function
using namespace std;
inline void add(int a,int b){
int main(){ int c=a+b;
int a=20,b=30; Macro Call cout<<"Add is "<<c;
}
int c = add(a,b);
int main(){ Function call
cout<<"Add is "<<c; int a=20,b=30;
}
add(a,b);
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

f= fact(n); cout <<"Sum=" <<s; return fact(n) + sum(n - 1);


cout << n << "! = " <<f; return 0; }
return 0; } int main() {
} int n,s;
cout << "Enter a positive integer: ";
cin >> n;
s= sum(n);
cout <<"Sum is"<<s;
return 0;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Strings (Using Character Arrays)


- String is an array of characters
- Strings terminated with ‘\0’
- Declaration of String…
Syntax:-
char string_name[size];

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

Write a program to initialize and display sting…

#include<iostream>

using namespace std;

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Write a program to read string from user and display it…

#include<iostream>

using namespace std;

int main(){

char s[15];

cout<< “Enter your name”;

cin>>s;

cout<<"Welcome"<<s;

Write a program for Traversing/Processing a String Using Loop…

#include<iostream>

#include<cstring>

using namespace std;

int main(){

char s[15]="Programming";

cout<<"String is \n";

for (int i=0;s[i]!='\0';i++)

cout<<s[i];

}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

…String Handling Functions…


- There are various string handling functions available in cstring header file
string length - strlen(s1)

string reverse - strrev(s1)

string lower - strlwr(s1)

string upper - strupr(s1)

string copy - strcpy(s1,s2) & strncpy(s1,s2,n)

string concatenation – strcat(s1,s2) & strncat(s1,s2,n)

string comparison – strcmp(s1,s2) & strncmp(s1,s2,n)

string character - strchr(s1,ch)

string reverse character - strrchr(s1,ch)

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;
}

//WAP to Find length of a string without using string handling function…


#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s[15]="Programming";
int l=0;
for (int i=0;s[i]!='\0';i++)
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

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

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

cout<<"After Copying String is "<<s1;


}

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";
}

WAP to demonstrate strchr() function.


#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="Dhananjay",ch='a';
char *s2=strchr(s1,ch);
cout<<s2;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

WAP to demonstrate strrchr() function


#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="Dhananjay",ch='a';
char *s2=strrchr(s1,ch);
cout<<s2;
}

WAP to find reverse of a string using string function.


#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s1[10]="madaM";
strrev(s1);
cout<<s1;
}

WAP to demonstrate working of strlwr()


#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s[10]="SHALVI";
strlwr(s);
cout<<s;
return 0;
}
WAP to demonstrate working of strlupr()
#include<iostream>
#include<cstring>
using namespace std;
int main(){
char s[10]="Shalvi";
strupr(s);
cout<<s;
return 0;
}
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

Tasks:

Sr. Problem Statement I/O Test Flow Program- with Trace


Cases chart color codes Table
No.
1 Write a function that takes one integer parameter as a year, and displays its leap year or not.
2 Write a function that takes two integer parameters x & y, and returns the result XY. (Don’t use pow( ))
3 Implement a program using user defined function to return largest of three floating-point numbers.
4 WAP using user defined function to calculate and return factorial of a given integer.
5 Write a menu driven program to compute sum of digits of a number, to find reverse of a number, to count
number of digits by writing three different functions with parameters and return type.
6 Write user defined function “Search_Element” to search element is present in 1D array or not. Search function
accepts array, size of array and key to search as parameters.
7 Write a program to print Fibonacci series up to n using recursion.
8 Write one program to perform following operations on strings
a) To find length of a string
b) To compare two string for equality
c) To Copy one string to other
d) To concatenate two string
e) To find reverse of a String
9 WAP to copy one string to another string without using string handling function and display copied string.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering / School of
Technology Management & Engineering

B. Tech/MBA Tech Workbook Academic Year- 2023-24


Year:-First Subject:- Programming for Problem Solving Semester: - First

You might also like