You are on page 1of 11

Experiment Experiment - 03-1

Number
Experiment Title To learn writing, executing and debugging C++
programs related to point.

Date of Experiment 03/02/2023


Date of Submission 10/03/2023

Objective:
 To learn writing, executing and debugging C++ programs related to
point.

Q1. Write a C++ program to calculate the GCD of two numbers using
recursive functions. Use call-by address as a method of passing parameters
to the function.

Theory:
Flow Diagram / Algorithm:

 Flow chart to program to calculate the GCD of two numbers using recursive
functions.

Compiler Details: DEV C++

Code:
#include<iostream>
using namespace std;
int gcd(int a, int b);
int main()
{
cout<<"srinjoy sahoo 2130083"<<endl;;
int a,b;
cout<<"Enter the number whose gcd is to be calculated: ";
cin>>a>>b;
cout<<"GCD of "<< a <<" and "<< b <<" is "<< gcd(a, b);
return 0;
}
int gcd(int a, int b)
{
if (a == 0 || b == 0)
return 0;
else if (a == b)
return a;
else if (a > b)
return gcd(a-b, b);
else
return gcd(a, b-a);
}
Results/Output:

 Output for program to calculate the GCD of two numbers using


recursive functions.

Remarks:
Experiment Experiment - 03-2
Number
Experiment Title Write a C++ program using pointers to search a
value from a dynamically created 1-d array.
Date of Experiment 03/02/2023
Date of Submission 10/03/2023

Q 2. Write a C++ program using pointers to search a value from a


dynamically created 1-d array.

Theory:
Flow Diagram / Algorithm:

 Flow chart to search a value from a dynamically created 1-d array.

Compiler Details: DEV C++


Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"srinjoy sahoo 2130083"<<endl;
int *ptr,m;
cout<<"Enter the number of elements: ";
cin>>m;
ptr=new int[m];
int i,n;
cout<<"Enter the elements of the array\n";
for(i=0;i<m;i++)
{
cin>>ptr[i];
}
cout<<"Enter the value to be searched :";
cin>>n;
for(i=0;i<m;i++)
{
if(ptr[i]==n)
{
cout<<"Element found at position :"<<i<<"\n";
}
}
}

Results/Output:

 Output for to search a value from a dynamically created 1-d array.


Remarks:
Experiment Experiment - 03-3
Number
Experiment Title Write a C++ program using pointers to sort a
dynamically created 1-d array
Date of Experiment 03/02/2023
Date of Submission 10/03/2023

Q3. Write a C++ program using pointers to sort a dynamically created 1-d
array.

Theory:
Flow Diagram / Algorithm:

 Flow chart to sort a dynamically created 1-d array.


Compiler Details: DEV C++

Code:
#include<iostream>
using namespace std;
int main()
{
cout<<"srinjoy sahoo 2130083"<<endl;;
int *ptr,m,j,i,t;
cout<<"Enter the number of elements: ";
cin>>m;
ptr=new int[m];
cout<<"Enter the elements of the array\n";
for(i=0;i<m;i++)
{
cin>>ptr[i];
}
for (i= 0 ; i<m ; i++)
{
for (j= 0 ; j< m - i - 1; j++)
{
if (ptr[j] > ptr[j+1])
{
t= ptr[j];
ptr[j]= ptr[j+1];
ptr[j+1]= t;
}
}
}
cout<<"Sorted element in ascending order:";
for (i= 0 ; i< m ; i++)
{
cout<<ptr[i]<<" ";
}
}
Results/Output:

 Output for to sort a dynamically created 1-d array

Remarks:

Signature of the Student

Signature of the Lab Coordinator


Srinjoy sahoo _________________________________
(Name of the Student) (Name of the Coordinator)

You might also like