You are on page 1of 13

Expt. No. 2: Sorting an array of integers using Bubble Sort method.

Aim: Write a program in C++ that first initializes an array of given 10 real numbers. The
program must sort numbers in ascending order using Bubble Sort method. It should print
the given list of numbers as well as the sorted list

#include<iostream.h>

int main() {

// Declare variables

int n, i, arr[50], j, temp;

// Prompt user for the size of the array

cout << "Enter the Size (max. 50): ";

cin >> n;

// Prompt user to enter 'n' numbers into the array

cout << "Enter " << n << " Numbers: ";

for (i = 0; i < n; i++)

cin >> arr[i];

// Display the original list of numbers

cout << "\nOriginal list of numbers:\n";

for (i = 0; i < n; i++) {

cout << arr[i] << " ";

// Sorting the array using Bubble Sort

cout << "\n\nSorting the Array using Bubble Sort Technique..\n";

for (i = 0; i < (n - 1); i++) {

for (j = 0; j < (n - i - 1); j++) {

// Swap elements if they are in the wrong order

if (arr[j] > arr[j + 1]) {

temp = arr[j];
arr[j] = arr[j + 1];

arr[j + 1] = temp;

// Display that the array has been sorted successfully

cout << "\nArray Sorted Successfully!\n";

// Display the new sorted array

cout << "\nThe New Array is: \n";

for (i = 0; i < n; i++)

cout << arr[i] << " ";

cout << endl;

// Return 0 to indicate successful completion of the program

return 0;

}Output
Expt. No. 3: Searching a number in an array using Binary Search technique.
Aim:Write a program in C++ that first initializes an array of given 10 sorted real numbers.
The program must verify whether a given element belongs to this array or not, using
Binary Search technique. The element (to be searched) is to be entered at the time of
execution. If the number is found, the program should print its position in the array
otherwise it should print, “The number is not found”.

#include <iostream.h>
#include <conio.h>
void main()
{
int n,i,beg,end,mid,flag;
n=10;flag=0;
double a[10],x;
clrscr();
cout<<"Enter sorted array:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter the no. that is to be searched:";
cin>>x;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(x==a[mid])
{
flag=1;
break;
}
if(x<a[mid])
end=mid-1;
else
beg=mid+1;
}
if(flag==1)
cout<<"\nThe no. is found at location: "<<mid+1;
else
cout<<"\nThe no. is not found";
getch();
}
1

Expt. No. 5: Reversing a string.


Aim: Write a program in C++ to input the given string (including spaces) and reverse it
using function which locates the end of the string and swaps the first character with the
last character, the second character with second last character and so on.
#include<iostream.h>

#include<string.h>

#include<conio.h> // for clrscr()

// Function to swap characters in a string to reverse it

void swap(int length, char s[]);

// Main function

int main() {

int len;

char str[100];

// Input: Get a string from the user

clrscr(); // Clear the screen

cout << "Enter the string:";

cin.getline(str, 100);

// Calculate the length of the string

len = strlen(str);

// Reverse the string using the swap function

swap(len, str);

// Output: Display the reversed string

cout << "\nReverse of the given string is:" << str;


getch(); // Wait for a key press

return 0; // Indicates successful completion of the program

// Function to swap characters in a string to reverse it

void swap(int length, char s[]) {

char temp;

int l = length - 1;

// Swap characters from the beginning and end of the string towards the center

for (int i = 0; i < length / 2; i++) {

temp = s[i];

s[i] = s[l];

s[l] = temp;

l--;

Output
Expt. No. 10: Implementing inheritance.
Aim: Write a program in C++ to implement the following class hierarchy. Class Student to
obtain Roll no., Class Test to obtain marks in two different subjects, Class Sports to obtain
weightage in sports, and class Result to calculate total marks. The program must print the
roll no., individual marks obtained in two subjects, sports and total marks.

#include<iostream.h>

#include<conio.h>

class student {

private:

int rollno;

public:

void setno(int a) {
rollno = a;

void putno() {

cout << "\n roll no:" << rollno;

};

class test : public student {

protected:

float sub1, sub2;

public:

void setmarks(float x, float y) {

sub1 = x;

sub2 = y;

void putmarks() {

cout << "\n marks in sub 1:" << sub1;

cout << "\n marks in sub 2:" << sub2;

};

class sports {

protected:

float score;
public:

void setscore(float s) {

score = s;

void putscore() {

cout << "\n sports score weightage:" << score;

};

class result : public test, public sports {

private:

float total;

public:

void display() {

total = sub1 + sub2 + score;

putno();

putmarks();

putscore();

cout << "\n total marks:" << total << endl;

};

void main() {

int rn;

float s1, s2, sc;

result r;
clrscr(); // Clear the screen

cout << "\n Enter students info";

cout << "\n roll no:";

cin >> rn;

r.setno(rn);

cout << "\n enter marks 1:";

cin >> s1;

cout << "\n enter marks 2:";

cin >> s2;

r.setmarks(s1, s2);

cout << "\n weightage in sports: ";

cin >> sc;

r.setscore(sc);

cout << "\n The students details are as follows :";

r.display();

getch(); // Wait for a key press}

Output
Expt. No. 15: Working with tables and images in HTML.

Aim: Create a simple HTML page on any of the following topics: College Profile, Computer
Manufacturer, or Software Development CompanyThe page must consist of a scrolling
marquee displaying an appropriate message. The page must include table with at least five
rows and 3 columns having merged cells at least at one place. The page must also display
an Image such that when the same is viewed through a browser and the mouse is placed
(hovered) on the image, an appropriate text message should be displayed. The image itself
should also act as a hyperlink to another page

<!DOCTYPE html>

<html>

<head>

<title>FRIENDS.jr.collage of science</title>

</head>

<body bgcolor="pink">

<center>

<h1>Friends Jr. College of Science</h1>

<a href="features1.html">

<img src="logo.png" alt="Friends College" height="100" width="120">

</a>

</center>

<marquee><h3>Updated on 1.10.2012</h3></marquee>

<table align="center" border="1" bordercolor="black">

<tr>

<th>Department</th>

<th colspan="2">Staff</th>

</tr>
<tr>

<td>Physics</td>

<td colspan="2">Mr. Harshada</td>

</tr>

<tr>

<td>Biology</td>

<td colspan="2">Caroline Dency</td>

</tr>

<tr>

<td>Chemistry</td>

<td colspan="2">Regina</td>

</tr>

<tr>

<td>Computer Science & IT</td>

<td colspan="2">Mrs. Anita Jeba</td>

</tr>

</table>

</body>

</html>

You might also like