Programming Exercise Looping Structure P-3

Programming Exercise Looping Structure P-2

IF Statement Programming Exercise Part-1

Program 14:
Write a program that initializes data into one-dimensional array. The data must be in ascending order. It inputs a value from the user and searches this value in the array using binary search method.

#include<iostream.h>
#include<conio.h>
main()
{
int abc[10] = {1,3,9,15,78,87,95,103,124,352};
int loc, num, i, M, S = 0, E = 9;
loc = 0;
clrscr();
cout<<"Enter any value ? ";
cin>>num;
while(S<=E)
{
M = (S + E)/2; 
if(num == abc[M])
{
loc = M+1;
break;
}
else if(num < abc[M])
E = M - 1;
else
S = M + 1;
}
if(loc == 0)
cout<<"Value not found "<<endl;
else
cout<<"Value found at position = "<<loc<<endl;
getch();
}

Program 15:
Write a program that inserts a new value at a specified location into one-dimensional array of N elements.

#include<iostream.h>
#include<conio.h>
main()
{
int p, val, n, i;
int arr[10] = {55,41,12,78,15,69,45,47,36};
clrscr();
cout<<"Values of array before insertion\n";
for(i=0; i<=8; i++)
cout<<arr[i]<<" ";
cout<<endl;
while(1)
{
cout<<"\nEnter position to insert value ? ";
cin>>p;
n=9;
if(p > n || p < 0)
{
cout<<"Invalid position\r";
getch();
clreol();
}
else
break;
}
while(n >= p-1)
{
arr[n+1] = arr[n];
n--;
}
cout<<"Enter value to insert ? ";
cin>>val;
arr[p-1] = val;
cout<<"\nValues of array after insertion\n";
for(i = 0; i<=9; i++)
cout<<arr[i]<<" ";
getch();
}

Program 16:
Write a program that deletes a value from a specified location of one-dimensional array of N elements.

#include<iostream.h>
#include<conio.h>
main()
{
int p, n, i;
int arr[10] = {55,41,12,78,15,69,45,47,36,78};
clrscr();
cout<<"Values of array before deletion\n";
for(i = 0; i<=9; i++)
cout<<arr[i]<<" ";
cout<<endl;
while(1)
{
cout<<"\nEnter position to delete value?";
cin>>p;
n = 9;
if(p > n || p < 0)
{
cout<<"Invalid position\r";
getch();
clreol();
}
else
break;
}
while(p<=n)
{
arr[p-1] = arr[p];
p++;
}
arr[n] = NULL;
cout<<"\nValues of array after deletion\n";
for(i = 0; i<=9; i++)
cout<<arr[i]<<" ";
cout<<endl;
getch();
}

Program 17:                                                                                     Write a program that sorts one-dimensional array’s values into descending order using bubble sort method.
// Array sorting using bubble sort method

#include<iostream.h>
#include<conio.h>
main()
{
int array[10] = {55,41,12,78,1,69,45,4,36,5};
int u, i, temp;
clrscr();
cout<<"Values of array before sorting\n";
for(i = 0; i<=9; i++)
cout<<array[i]<<" ";
cout<<endl;

// array sorting
u = 9;
while(u>=1)
{
i = 0;
while(i<u)
{
if(array[i] < array[i+1])
{
temp = array[i];
array[i] = array[i+1];
array[i+1] = temp;
}
i++;
}
u--;
}
cout<<"\nValues of array after sorting\n";
for(i = 0; i<=9; i++)
cout<<array[i]<<" ";
getch();
}

Program 18:
Write a program that sorts one-dimensional array’s values into ascending order using selection sort method.
// Array sorting using selection sort method

#include<iostream.h>
#include<conio.h>
main()
{
int array[10] = {55,41,12,78,1,69,45,4,36,5};
int u, i, temp, min, pos;
clrscr();
cout<<"Values of array before sorting\n";
for(i = 0; i<=9; i++)
cout<<array[i]<<" ";
cout<<endl;

// array sorting 
u=0;
while(u<9)
{
min = array[u];
pos = u;
i = u;
while(i<=9)
{
if(min > array[i])
{
min = array[i];
pos = i;
}
i++;
}

// swap values 
temp = array[pos];
array[pos] = array[u];
array[u] = temp;
u++;
}
cout<<"\nValues after sorting\n";
for(i = 0; i<=9; i++)
cout<<array[i]<<" ";
getch();
}

Program 19:
Write a program that sorts one-dimensional array of 5 elements in ascending order using insertion sort method.

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

void main(void)
{
int abc[4], val, u, i;
clrscr(); 
cout<<"Enter 5 values "<<endl;
for(i = 0; i<=4; i++)
cin>>abc[i];
for(u = 0; u<=4; u++)
{
val = abc[u];
for(i = u; i >=0 && val<abc[i-1]; i--)
abc[i] = abc[i-1];
abc[i] = val;
}
cout<<"Sorted array "<<endl;
for(i = 0; i<=4; i++)
cout<<abc[i]<<endl;
getch();
}

Program 20:
Write a program that inputs data into a table and displays on the screen in tabular form.

#include<iostream.h>
#include<conio.h>
main()
{
int table[3][4];
int r, c;
clrscr();
r = 0;
while(r<=2)
{
for(c = 0; c<=3; c++)
{
cout<<"Enter value for row "<<r<<" and column "<<c<<" ? ";
cin>>table[r][c];
}
r++;
}

cout<<"\nValues in tabular form" <<endl;
for(r = 0; r<=2; r++)
{
for(c = 0; c<=3; c++)
cout<<table[r][c]<<"\t";
cout<<endl;
}
getch();
}

 

Leave a Reply