0% found this document useful (0 votes)
79 views

C ++ Programming

The C++ code examples demonstrate various array operations - linear search, binary search, finding the largest/smallest element, inserting/deleting an element, and sorting arrays using selection sort, bubble sort, and insertion sort. When compiled and executed, each program will accept user input, perform the relevant array operation, and output the results.

Uploaded by

Swadesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

C ++ Programming

The C++ code examples demonstrate various array operations - linear search, binary search, finding the largest/smallest element, inserting/deleting an element, and sorting arrays using selection sort, bubble sort, and insertion sort. When compiled and executed, each program will accept user input, perform the relevant array operation, and output the results.

Uploaded by

Swadesh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

/* C++ Program - Linear Search */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

/* C++ Program - Binary Search */

#include<iostream.h>
#include<conio.h>

1
void main()
{
clrscr();
int n, i, arr[50], search, first, last, middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location "<<middle+1<<"\n";
break;
}
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not present in the list.";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result.
Above C++ Programming Example Output (Element found):

2
Above C++ Programming Example Output (Element Not found):

/* C++ Program - Find Largest Element in Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int large, arr[50], size, i;
cout<<"Enter Array Size (max 50) : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Searching for largest number ...\n\n";
large=arr[0];
for(i=0; i<size; i++)
{
if(large<arr[i])
{
large=arr[i];
}
}
cout<<"Largest Number = "<<large;
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

3
/* C++ Program - Find Smallest Element in Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int small, arr[50], size, i;
cout<<"Enter Array Size (max 50) : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Searching for smallest element ...\n\n";
small=arr[0];
for(i=0; i<size; i++)
{
if(small>arr[i])
{
small=arr[i];
}
}
cout<<"Smallest Element = "<<small;
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

/* C++ Program - Insert Element in Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, insert, i, pos;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{

4
cin>>arr[i];
}
cout<<"Enter element to be insert : ";
cin>>insert;
cout<<"At which position (Enter index number) ? ";
cin>>pos;
// now create a space at the required position
for(i=size; i>pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
cout<<"Element inserted successfully..!!\n";
cout<<"Now the new array is : \n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

/* C++ Program - Delete Element from Array */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50], size, i, del, count=0;
cout<<"Enter array size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be delete : ";
cin>>del;
for(i=0; i<size; i++)
{
if(arr[i]==del)

5
{
for(int j=i; j<(size-1); j++)
{
arr[j]=arr[j+1];
}
count++;
break;
}
}
if(count==0)
{
cout<<"Element not found..!!";
}
else
{
cout<<"Element deleted successfully..!!\n";
cout<<"Now the new array is :\n";
for(i=0; i<(size-1); i++)
{
cout<<arr[i]<<" ";
}
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ Programming Code for Selection Sort


Following C++ program ask to the user to enter array size and array element, then it will sort the
array is ascending order and display the sorted array:

/* C++ Program - Selection Sort */

#include<iostream.h>
#include<conio.h>
void main()
{

6
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

/* C++ Program - Bubble Sort */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{

7
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!\n";
cout<<"Sorted list in ascending order :\n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

/* C++ Program - Insertion Sort */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort ... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;

8
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result:

/* C++ Program - Compare Two String */

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
clrscr();
char str1[100], str2[100];
cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not equal";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following result.
Above C++ Programming Example Output (for equal strings):

9
Above C++ Programming Example Output (for unequal strings):

10

You might also like