You are on page 1of 5

Course Title: CP

Assignment no.3

Student Name: Abdul Sammad Khan.


Enrolment No:01-135201-004.
Class and Section: BS(IT)-1B.
Submitted to: Sir Umar Khatakh.
Date:06thApril,2020.

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD
Q1. What is meant by Fibonacci series/sequence? Write a program to generate
Fibonacci sequence.

Ans: Fibonacci series:


The Fibonacci sequence is a set of numbers that starts with a one or a
zero, followed by a one, and proceeds based on the rule that each number called a Fibonacci number is
equal to the sum of the preceding two numbers.

Example:

F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ….

In some texts, it is customary to use n = 1. In that case, the first two terms are defined as 1 and 1 by
default, and therefore: F (1) = 1, 1, 2, 3, 5, 8, 13, 21, 34…….

Program of a Fibonacci series:

Program:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n1 = 0, n2 = 1, sum, number;
cout << "Enter the number of elements: ";
cin >> number;
cout << n1 << " " << n2 << " "; //printing 0 and 1
for (int i = 2; i < number; ++i) //loop starts from 2 because 0 and 1 are already printed
{
sum = n1 + n2;
cout << sum << " ";
n1 = n2;
n2 = sum;
}
_getch();
return 0;
}

Result/Output:
Q2. What is meant by selection sort using an array? Write a program to sort out an
array of 20 integers. Such that you need to user input the array values and sort
them in both ascending and descending order using the selection sort mechanism .

Ans: Selection sort:


Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and
the unsorted part at the right end. Initially, the sorted part is empty, and the unsorted part is the entire
list. The smallest element is selected from the unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This process continues moving unsorted array
boundary by one element to the right.

Program of sorting an array:

program in ascending order:


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a, b, c, num[20]; //declaring array and variables.
cout << "Enter number:" << endl;
for (int a = 0; a <5; a++)
{
cin >> num[a];
}
for (int a = 0; a <5; a++)
{
for (b = 0;b<5; b++) //using for loop
{
if (num[a] < num[b])
{
c = num[a];
num[a] =num[b]; //replacing values
num[b] = c; //sorting values
}
}
}
cout << "Ascending order:" << endl; //displaying message
for (int a = 0; a <5; a++)
{
cout << " "<<num[a]; //displaying data
}
_getch();
return 0; //returning zero to the system
}

Result/Output:

program in descending order:


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a, b, c, num[20]; //declaring array and variables.
cout << "Enter number:" << endl;
for (int a = 0; a <5; a++)
{
cin >> num[a];
}
for (int a = 0; a <5; a++)
{
for (b = 0;b<5; b++) //using for loop
{
if (num[a] > num[b])
{
c = num[a];
num[a] =num[b]; //replacing values
num[b] = c; //sorting values
}
}
}
cout << "descending order:" << endl; //displaying message
for (int a = 0; a <5; a++)
{
cout << " "<<num[a]; //displaying data
}
_getch();
return 0; //returning zero to the system
}

Result/Output:

You might also like