Q1

Q1. What is meant by Fibonacci series/sequence?
Fibonacci series is of number in which each number is the sum of previous two numbers.

e.g. 1, 1, 2, 3, 5, 8, 13, 21, 34……………………………..n.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

Write a program to generate Fibonacci sequence.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int n,i=0,f,s,nextterm; //where n is the number of terms entered by user to be printed.

//f is the firstterm and s is the second term.

cout<<“enter the number of terms to be printed:”<<endl;
cin>>n;
f=0;
s=1;
cout<<s;
while(i<n) //a condition is used in while loop that states that the loop will continue until
the value of i is less than n
{
nextterm=f+s;
f=s;
cout<<” “<<nextterm;
s=nextterm;
i++;
}
system(“pause”);
return 0;
}
OUTPUT:

Q2. What is meant by selection sort?
Selection sort is a process which arrange the elements of array into proper position it works as it find the minimum values in the array and swap it with the value placed at first position and then again do the same process excluding the first value.

Write a program to sort out an array of 10 integers using selection sort method.

SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int arr10,i,j,minimum,swap; //miniumum is initiliaze for the minimum value and swap is
to swap the values.

for(i=0;i<10;i++) //for loop is used to take 10 values from user.

{
cout<<“enter the elements in array:”<<endl;
cin>>arri;
}
cout<<“values before sorting the array are :”<<endl;
for(i=0;i<10;i++) //this for loop is used to cout the values entered.

{
cout<<” “<<arri;
}
for(i=0;i<9;i++) //this loop will run 9 times
{
minimum=i;
for(j=i+1;j<10;j++)
if(arrj<arrminimum)
minimum=j;
if(minimum!=i) //! this operator is called not equal to operator.

{
swap=arri; //this all section is used for swapping the values
arri=arrminimum;
arrminimum=swap;
}
}
cout<<”
“”now array after sorting”<<endl;
for(i=0;i<10;i++)
{
cout<<” “<<arri;
}
system(“pause”);
return 0;
}
OUTPUT:

Q3. What is meant by searching array?
Searching an array means to search a desired value. The searching array works as it visit the first element of array and compare it value with the required value. If the value is not found it moves to the next value and repeat the same process until the value is found.

Write a program to search an array using binary search for 7 user input values.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int n, i, arr50, search, f, l, m; //where f is first element:l is the last element:m is
the middle element.

cout<<“Enter total number of elements :”;
cin>>n;
cout<<“Enter “<<n<<” number :”;
for (i=0; i<n; i++)
{
cin>>arri;
}
cout<<“Enter a number to find :”;
cin>>search;
f = 0;
l = n-1;
m = (f+l)/2;
while (f <= l)
{
if(arrm < search) //a condition which will check that if middle arr is less than search number first element will have value of
middle +1.

{
f = m+ 1;
}
else if(arrm == search)
{
cout<<search<<” found at location “<<m+1<<”
“;
break;
}
else{
l = m – 1;
}
m = (f + l)/2;
}
if(f > l)
{
cout<<“Not found! “<<search<<” is not present in the list.”;}
system(“pause”);
return 0;
}
OUTPUT:

x

Hi!
I'm Mila

Would you like to get a custom essay? How about receiving a customized one?

Check it out