This Blog contains the information of computer programming and internet. All the questions are related to our course subjects of BSCS. Hopefully, this blog will evaluate your problems related to your study and will be a great platform for learning basic computer skills and techniques...and your comments will tell us how this blog serving you...!!
Thursday, 18 October 2012
Saturday, 13 October 2012
Friday, 5 October 2012
Monday, 1 October 2012
Bubble Sort
#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;
}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}
void sort()
{
for(int i = 0; i<n-1; i++)
for(int j = 0; j<n-i-1;j++)
if(a[j+1]<a[j])
{
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
arr.sort();
cout<<"arrays values after sorting"<<endl;
arr.display();
}
Binary Search
#include<iostream>
using namespace std;
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for (int i = 0; i<size; i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ?"<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"Enter value:"<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i = 0; i<n; i++)
cout<<a[i];
cout<<endl;
}
void search()
{
bool found = false;
int low = 0;
int high = n-1;
int key;
cout<<"enter key:"<<endl;
cin>>key;
while(low <= high && !found)
{
int mid = (low + high)/2;
if (key == a[mid])
found = true;
else if(key<a[mid])
high = mid - 1;
else
low = mid + 1;
}
if(found == true)
cout<<"Found"<<endl;
else
cout<<"not found"<<endl;
}
};
void main()
{
array arr1, arr2;
arr1.read();
cout<<"the entered array is : "<<endl;
arr1.display();
arr1.search();
}
Linear Searching the Array's element
#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;
}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}
void search()
{
bool Found = false;
int key;
cout<<"enter key: "<<endl;
cin>>key;
for(int i= 0; i<n && !Found; i++)
if (key == a[i])
if(Found)
{
bool Found = true;
}
if(Found)
{
cout<<"found"<<endl;
}
else
{
cout<<"not found"<<endl;
}
}
};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
arr.search();
}
Linear Searching the Array's element
#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;
}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}
void search()
{
bool Found = false;
int key;
cout<<"enter key: "<<endl;
cin>>key;
for(int i= 0; i<n && !Found; i++)
if (key == a[i])
if(Found)
{
bool Found = true;
}
if(Found)
{
cout<<"found"<<endl;
}
else
{
cout<<"not found"<<endl;
}
}
};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
arr.search();
}
Merging Two Arrays
#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values: "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void merge(Array a1, Array a2)
{
int i=0,j=0;
/*while (i< a1.n && j< a2.n )
{
if (a1.a[i] < a2.a[j])
{
a[n] = a1.a[i];
i++;
}
else
{
a[n] = a2.a[j];
j++;
}
n++;
}*/
while (i < a1.n)
{
a[n] = a1.a[i];
i++; n++;
}
while (j < a2.n)
{
a[n] = a2.a[j];
j++; n++;
}
}
};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
cout<<"for 2nd array ";
a2.read();
a2.display();
// itm.sort();
// itm.display();
// a2.sort();
// a2.display();
a3.merge(itm,a2);
a3.display();
// itm.del();
// itm.insert();
}
Array Reading by For Loop
#include<iostream.h>
const int size = 10;
class array
{
private:
int a[size];
int n;
public:
array()
{
for ( int i=0; i<size; i++ )
{
a[i];
n = 0;
}
}
void read()
{
int num;
cout<<"how many values you want to insert ?? "<<endl;
cin>>num;
for ( int i = 0; i<num; i++)
{
cout<<"Enter value: "<<endl;
cin>>a[i];
}
n = num;
}
void display ()
{
for (int i = 0; i<n; i++ )
cout<<a[i]<<endl;
}
};
void main()
{
array arr;
arr.read();
cout<<"The entered values are: "<<endl;
arr.display();
}
Deletion and Insertion at any index of Array
#include <iostream.h>
const int SIZE = 10;
class Array
{
private:
int a[SIZE];
int n;
public:
Array()
{
for (int i=0;i<SIZE;i++)
a[i] = 0;
n = 0;
}
void read()
{
int num;
cout<<"how many values you want to enter ? "<<endl;
cin>>num;
for(int i = 0; i<num; i++)
{
cout<<"enter values "<<endl;
cin>>a[i];
}
n = num;
}
void display()
{
for (int i=0;i<n;i++)
cout << a[i];
cout << endl;
}
void del()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=index;i<n-1;i++)
a[i] = a[i+1];
n--;
display();
}
void insert()
{
int index;
cout << "Enter index";
cin >> index;
for (int i=n;i>index;i--)
a[i] = a[i-1];
cout << "Enter Value";
cin >> a[i];
n++;
display();
}
};
void main()
{
Array itm, a2, a3;
itm.read();
itm.display();
a2.read();
a2.display();
itm.del();
itm.insert();
}
Subscribe to:
Posts (Atom)