BGC TRUST UNIVERSITY BANGLADESH
BGC Bidyanagar, Chandanaish, Chattogram
Department of Computer Science and Engineering
Lab Report
CA
Report No : 01
Program : B.Sc. (Hons.) in CSE
Session : Jan-June 2025
Course Code : 07-0613-CA Lab406
Course Title : Lab-1: based on 07-0613-CA401
Submitted By
Name: Arshek Ahmed Chy
Internal ID: 0192320005101043
Student ID: 230241043
4th Semester(A)
Department of CSE, BGCTUB
Submitted To
Course Teacher: MD Salah Udding Chowdhury
Designation: Assistant Professor & Chairman
Department of CSE, BGCTUB
Date of Submission: 16th June, 2025 Signature of Course Teacher
Lab 1 (i): Calculate the series Lab 1 (ii): Calculate the GPA for your last
12+32+………+n2 where n is an odd number semester
Code: Code:
// Lab 1 problem 1
#include<bits/stdc++.h> //lab 1 problem 2
using namespace std; #include<bits/stdc++.h>
int main() using namespace std;
{
int n; double GP(double n)
cin >> n; {
int sum = 0; if(n>=80.00)
for(int i = 1; i <= n; i+=2){ return 4.00;
sum += pow(i,2); else if(n >=75.00)
} return 3.75;
cout << sum; else if(n>=70.00)
return 3.50;
return 0; else if(n>=65.00)
} return 3.25;
else if(n>=60.00)
Input: return 3.00;
else if(n>=55.00)
return 2.75;
else if(n>=50.00)
return 2.50;
else if(n>=45.00)
Output: return 2.25;
else if(n>=40.00)
return 2.00;
else
return 0.00;
}
int main()
{
int n;
cin >>n;
string subjects[n];
double gpas[n], total_gpa = 0.00;
double credits[n], total_credits = 0.00;
cout << "Enter the names of subject, Lab 1(iii): Declare an array of n elements
marks, credits:\n";; and also calculate the sum of odd number
for(int i = 0; i < n; i++){ of these elements.
double marks;
cin >> subjects[i] >> marks >> Code:
credits[i];
gpas[i] = GP(marks); //lab 1 problem 3
total_gpa+= credits[i] * gpas[i]; #include<bits/stdc++.h>
total_credits+=credits[i]; using namespace std;
}
int main()
cout << "The GPA of individual subjects: {
\n"; int n;
for(int i = 0; i < n; i++){ cin >> n;
cout << subjects[i] << "\t" << gpas[i] int arr[n], sum = 0;
<< endl; for(int i = 0; i < n; i++){
} cin >> arr[i];
double cgpa = total_gpa/total_credits; if(arr[i] % 2 != 0){
sum+=arr[i];
cout << endl << fixed << setprecision(2); }
cout << "CGPA = " << cgpa; }
return 0; cout << sum;
}
return 0;
Input: }
Input:
Output:
Output:
Lab 2(i) : Calculate the series 1+2+……+n
using recursion. Lab 2(ii) : Calculate the series
m2+(m+1)2+…….+n2 using going-up
Code: recursion.
//lab 2 problem 1 Code:
#include<bits/stdc++.h>
using namespace std; //lab 2 problem 2
#include<bits/stdc++.h>
long long sum(int n){ using namespace std;
if(n == 1)
return 1; int GU(int m, int n)
else {
return sum(n-1)+n; if(m==n)
} return m*m;
int main() else
{ return m*m + GU(m+1, n);
int n; }
cin >> n; int main()
int res = sum(n); {
cout << res; int a, b;
cin >> a >> b;
return 0; int res = GU(a, b);
} cout << res << endl;
return 0;
Input: }
Input:
Output:
Output:
Lab 2(iv) : Calculate the series
Lab 2(iii) : Calculate the series m2+(m+1)2+…….+n2 using splitting-halves
m2+(m+1)2+…….+n2 using going-down recursion.
recursion.
Code:
Code:
//lab 2 problem 4
//lab 2 problem 3 #include<bits/stdc++.h>
#include<bits/stdc++.h> using namespace std;
using namespace std; int sh(int m, int n)
{
int Gd(int m, int n) int d = (m + n)/2;
{ if(m==n)
if(m==n) return m*m;
return m*m; else
return sh(m, d) + sh(d+1, n);
else }
return n*n+Gd(m, n-1); int main()
} {
int main() int a, b;
{ cin >> a >> b;
int a, b; int res = sh(a,b);
cin >> a >> b; cout << res << endl;
int res = Gd(a, b); return 0;
cout << res << endl; }
return 0;
} Input:
Input:
Output:
Output:
Lab 3(i) : Write a program to find the Lab 3(ii) : Write a program to find the
Fibonacci series for nth position using Fibonacci series for nth position using
recursion. iteration.
Code: Code:
//lab 3 problem 1 //lab 3 problem 2
#include<bits/stdc++.h> #include<bits/stdc++.h>
using namespace std; using namespace std;
int fib(int n) int main()
{ {
if(n==1) int n;
return 1; cin >> n;
else if (n==0) int a =0, b = 1, c;
return 0; for(int i = 0; i < n; i++)
else {
return fib(n-1) + fib(n-2); if(i == 0)
} cout << i << " ";
int main() else if(i == 1)
{ cout << i << " ";
int n; else {
cin >> n; c = a + b;
for(int i = 0; i <= n; i++){ cout << c << " ";
cout << fib(i) << " "; a = b;
} b = c;
return 0; }
} }
return 0;
Input: }
Input:
Output: Output:
Lab 4(i) : Find the factorial of n using Lab 4(ii) : Find the factorial of n using
recursion. iteration.
Code: Code:
//lab 4 problem 1 //lab 4 problem 2
#include<bits/stdc++.h> #include<bits/stdc++.h>
using namespace std; using namespace std;
int fact(int n) int main()
{ {
if(n==0) int n, fact = 1;
return 1; cin >> n;
else if(n==1) for(int i = 1; i <= n; i++)
return 1; {
else fact*=i;
return (n * fact(n-1)); }
} if(n == 0)
int main() cout << 0;
{ else
int n; cout << fact << endl;
cin >> n;
long long res = fact(n); return 0;
if(n == 0) }
cout << 0;
else Input:
cout << fact << endl;
return 0;
}
Input: Output:
Output:
Lab 6 : Apply the tower of Hanoi algorithm Lab 7 : Apply the pizza cutting algorithm to
for showing the total moves and also calculate the number of slices for n-cut.
calculate the total number of moves.
Code:
Code:
//lab 6 //lab 7
#include<bits/stdc++.h> #include<bits/stdc++.h>
using namespace std; using namespace std;
void th(int n, char a, char b, char c)
{ int pc(int n)
if(n > 0) {
{ if(n==0)return 1;
th(n-1, a, c , b);
cout << a << " " << c << endl; return pc(n-1) + n;
th(n-1, b, a ,c); }
} int main()
} {
int main() int a;
{ cin >> a;
int n; int res = pc(a);
cin >> n; cout << res << endl;
cout << (1 << n) - 1 <<endl; return 0;
th(n, 'a', 'b', 'c'); }
return 0;
} Input:
Input:
Output:
Output:
for(int i = 0; i < n; i++) {
Lab 8 : Apply quick sort algorithm for cout << arr[i] << " ";
sorting an array of n elements. }
cout << endl;
Code:
return 0;
//lab 8
}
#include <bits/stdc++.h>
using namespace std;
Input:
int divideArray(vector<int> &arr, int low, int
high) {
int pivot = arr[low];
int i = low + 1;
Output :
int j = high;
while(i <= j) {
while(i <= high && arr[i] <= pivot) i++;
while(arr[j] > pivot) j--;
if(i < j) {
swap(arr[i], arr[j]); Lab 9: Apply merge sort algorithm for
} sorting an array of n elements.
}
Code:
swap(arr[low], arr[j]);
return j; //lab 9
} #include <bits/stdc++.h>
using namespace std;
void sortArray(vector<int> &arr, int low, int
high) { long long numbers[10005], helper[10005];
if(low < high) { void mergeTwoParts(int left, int right) {
int pivotIndex = divideArray(arr, low, int mid = (left + right) / 2;
high); int i = left;
int j = mid + 1;
sortArray(arr, low, pivotIndex - 1); int k = left;
sortArray(arr, pivotIndex + 1, high); while(i <= mid && j <= right) {
if(numbers[i] < numbers[j]) {
}
helper[k] = numbers[i];
} i++;
} else {
int main() { helper[k] = numbers[j];
int n; j++;
cin >> n; }
k++;
vector<int> arr(n); }
for(int i = 0; i < n; i++) { while(i <= mid) {
cin >> arr[i]; helper[k] = numbers[i];
} i++;
sortArray(arr, 0, n - 1); k++;
}
while(j <= right) { Lab 10 : Apply selection sort algorithm for
helper[k] = numbers[j];
sorting an array of n elements.
j++;
k++;
} Code:
for(int i = left; i <= right; i++) {
numbers[i] = helper[i]; //lab 10
} #include <bits/stdc++.h>
}
using namespace std;
void mergeSortAll(int left, int right) {
if(left == right) return; void sortArray(vector<int> &arr) {
int n = arr.size();
int mid = (left + right) / 2;
for (int i = 0; i < n - 1; i++) {
mergeSortAll(left, mid);
int smallestIndex = i;
mergeSortAll(mid + 1, right);
for (int j = i + 1; j < n; j++) {
mergeTwoParts(left, right); if (arr[j] < arr[smallestIndex]) {
} smallestIndex = j;
}
int main() { }
int n;
if (smallestIndex != i) {
cin >> n;
swap(arr[i], arr[smallestIndex]);
for(int i = 0; i < n; i++) { }
cin >> numbers[i]; }
} }
mergeSortAll(0, n - 1);
int main() {
for(int i = 0; i < n; i++) { int n;
cout << numbers[i] << " "; cin >> n;
}
cout << endl; vector<int> arr(n);
for (int i = 0; i < n; i++) {
return 0;
cin >> arr[i];
}
Input: }
sortArray(arr); // Call the sorting
function
Output:
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Input: Input:
Output :
Output:
Lab 11 : Apply insertion sort algorithm for
sorting an array of n elements.
Code:
//lab 11
#include <bits/stdc++.h>
using namespace std;
void sortArray(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int current = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > current) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sortArray(arr);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}