0% found this document useful (0 votes)
32 views2 pages

Cho Program - 1-2

The document describes a school exam results system where a teacher can input and search for student marks using linear and binary search techniques. It includes a C++ program that allows the teacher to enter sorted marks, search for a specific mark, and display its position. The program features dynamic memory allocation for storing marks and ensures that the input marks are in ascending order.

Uploaded by

inder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Cho Program - 1-2

The document describes a school exam results system where a teacher can input and search for student marks using linear and binary search techniques. It includes a C++ program that allows the teacher to enter sorted marks, search for a specific mark, and display its position. The program features dynamic memory allocation for storing marks and ensures that the input marks are in ascending order.

Uploaded by

inder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

LAB 1-2

(ii) In a school exam results system, a teacher has recorded the marks of 10
students in a subject, arranged in ascending order to simplify the search process.
The system allows the teacher to input the sorted list of marks, followed by a
specific mark to look for. The program then identifies and displays the position of
the entered mark in the list using two different search techniques that vary in
their approach and efficiency.

#include <iostream>
using namespace std;

class ExamResults {
private:
int* marks; // Dynamically allocated array for marks
int size; // Number of elements entered by the user

public:
// Constructor to allocate memory
ExamResults(int n) {
size = n;
marks = new int[size];
}

// Destructor to free memory


~ExamResults() {
delete[] marks;
}

// Function to input sorted marks


void inputMarks() {
cout << "Enter " << size << " marks in ascending order:\n";
for (int i = 0; i < size; i++) {
cout << "Mark " << i + 1 << ": ";
cin >> marks[i];
if (i > 0 && marks[i] < marks[i - 1]) {
cout << "Marks not in ascending order. Please re-enter.\n";
i--; // Re-enter current mark
}
}
}

// Linear Search
int linearSearch(int key) {
for (int i = 0; i < size; i++) {
if (marks[i] == key)
return i;
}
return -1;
}

// Binary Search
int binarySearch(int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (marks[mid] == key)
return mid;
else if (marks[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

// Display Result (using char* instead of string)


void displayPosition(const char* method, int key, int position) {
cout << "\n" << method << " Search Result:\n";
if (position != -1)
cout << "Mark " << key << " found at index: " << position << endl;
else
cout << "Mark " << key << " not found in the list.\n";
}
};

int main() {
int n;
cout << "Enter number of students (marks to be entered): ";
cin >> n;

ExamResults er(n);

[Link]();

int key;
cout << "\nEnter a mark to search for: ";
cin >> key;

int posLinear = [Link](key);


int posBinary = [Link](key);

[Link]("Linear", key, posLinear);


[Link]("Binary", key, posBinary);

return 0;
}

You might also like