[go: up one dir, main page]

0% found this document useful (0 votes)
9 views7 pages

Dsa 1

Uploaded by

imranzbrbd
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)
9 views7 pages

Dsa 1

Uploaded by

imranzbrbd
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/ 7

Heaven’s light is our guide

Rajshahi University of Engineering and Technology

Department of Computer Science & Engineering

Lab Report

Course Title: Data Structure Sessional


Course Code: CSE 1202
Module No: 01
Module Name: Flowchart & Algorithm
Date of experiment: 18/05/2025
Date of submission: 25/05/2025

Submitted by Submitted to

Name: Md. Imran Hasan Emrana Kabir Hashi


Roll: 2303112 Assistant Professor
Section: B Computer Science & Engineering
Objective:

1. To write a program to check whether a given number is a prime number.


2. To input student details, sort them based on CGPA, and identify students above or
below a user-defined threshold.
3. To calculate each student’s CGPA from the GPA of 8 semesters and display the
results along with student information.

Prime Number Check:

Algorithm:
Step 1: Start the program.
Step 2: Input a number n from the user.
Step 3: Initialize a boolean flag check = true.
Step 4: Loop i from 2 to √n:

• If n is divisible by i (i.e., n % i == 0), then:

o Set check = false

o Break the loop


Step 5: After the loop, check the flag:

• If check == true, print "Prime".

• Else, print "Not Prime".


Step 6: End the program.

Code:
#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
bool isPrime = true;

if (n <= 1) {
isPrime = false;
} else {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime)
cout << "Prime" << endl;
else
cout << "Not Prime" << endl;

return 0;
}

Output:

Discussion:
This program checks whether a given number is prime by testing divisibility up to its square
root. If no divisor is found, the number is identified as prime; otherwise, it is not prime. The
approach is efficient and confirms correctness with simple logic.

Complexity Analysis:
Time Complexity = O(n)
Space Complexity = O(n)(O(n) for nodes + O(h) for recursion stack)

Problem 2: CGPA & Grade Order

Algorithm:
Step 1: Start the program.
Step 2: Input the number of students t.
Step 3: For each student, input name, roll, course, and CGPA.
Step 4: Store all student records in an array of objects.
Step 5: Sort the array of students in descending order of CGPA using nested
loops.
Step 6: Display the sorted student list.
Step 7: Input a CGPA threshold value.
Step 8: Display students with CGPA ≥ threshold.
Step 9: Display students with CGPA < threshold.
Step 10: End the program.

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // for swap
using namespace std;

class stu {
public:
string name;
int roll;
string course;
float cgpa;
};

int main() {
int t;
cout << "How Many Students: ";
cin >> t;
vector<stu> cse(t);

for (int i = 0; i < t; i++) {


cout << "\nEnter details for student " << i + 1 << ":\n";
cout << "Enter name: ";
cin >> cse[i].name; // use getline if multi-word needed
cout << "Enter roll: ";
cin >> cse[i].roll;
cout << "Enter course: ";
cin >> cse[i].course;
cout << "Enter CGPA: ";
cin >> cse[i].cgpa;
}

// Sorting in descending order of CGPA


for (int j = 0; j < t - 1; j++) {
for (int i = j + 1; i < t; i++) {
if (cse[i].cgpa > cse[j].cgpa) {
swap(cse[i], cse[j]);
}
}
}
cout << "\nSorted Student List:\n";
for (int i = 0; i < t; i++) {
cout << cse[i].name << " "
<< cse[i].roll << " "
<< cse[i].course << " "
<< cse[i].cgpa << endl;
}

float target;
cout << "\nEnter a CGPA to compare: ";
cin >> target;

cout << "\nStudents with CGPA >= " << target << ":\n";
for (int i = 0; i < t; i++) {
if (cse[i].cgpa >= target) {
cout << "Name: " << cse[i].name << endl;
cout << "Roll: " << cse[i].roll << endl;
cout << "CGPA: " << cse[i].cgpa << endl;
}
}

cout << "\nStudents with CGPA < " << target << ":\n";
for (int i = 0; i < t; i++) {
if (cse[i].cgpa < target) {
cout << "Name: " << cse[i].name << endl;
cout << "Roll: " << cse[i].roll << endl;
cout << "CGPA: " << cse[i].cgpa << endl;
}
}

return 0;
}
Output:

Discussion:
This program stores and manages student information using a class in C++. It sorts students by
CGPA in descending order and then compares their CGPA against a user-defined threshold.
This helps to identify high-performing and low-performing students efficiently. The use of
classes provides structured data handling, and sorting ensures the proper arrangement of
student results.

Complexity Analysis:
Time Complexity:
• Input of t students: O(t)
• Sorting using nested loops (selection sort–like method): O(t²)
• Displaying sorted list and comparisons with threshold: O(t)
Space Complexity:
• Storage of t student records requires O(t) space.
• No significant extra space is used beyond this.

You might also like