RSS

Category Archives: Pointers and Arrays

PROGRAM TO CHECK THE TYPE OF SORTING IN AN ARRAY (2ND LOGIC)

//THIS PROGRAM CHECKS WHETHER AN ARRAY IS SORTED OR NOT. IF SORTED THEN IT CHECKS THE TYPE OF SORT - ASCENDING OR DESCENDING

#include <iostream>

using namespace std;

char checksort(int arr[], int size);

int main()
{
 int size;
 cout<<"\nEnter the size of the array \n";
 cin>>size;

 int *arr = new int[size];

 cout<<"\nInput the array\n";

  for(int i = 0; i<size; i++)
  {
   cin>>arr[i];
  }

char s = checksort(arr,size);

if(s == 'A')
 cout<<"\nAscending...\n";
else if(s == 'D')
 cout<<"\nDescending...\n";
else
 cout<<"\nNot ordered...\n";

return 0;
}

 


char checksort(int arr[], int size)
 {
 char sort = 'N';
 int flag = 0;
//check ascending
 for(int i = 0; i<size-1; i++)
 {
 if(arr[i]>arr[i+1])
 {
 flag = 1;
 break;
 }
 }

if(flag == 0)
 {
 return ('A');
 }

//check descending

flag = 0;

for(int j = 0; j<size-1; j++)
 {
 if(arr[j]<arr[j+1])
 {
 flag = 1;
 break;
 }
 }

if(flag == 0)
 {
 return ('D');
 }

return 'N';
 }

 

INSERTION SORT: DESCENDING ORDER (BETTER LOGIC)

// THIS PROGRAM SORTS AN ARRAY IN DESCENDING ORDER USING INSERTION SORT

#include <iostream>

using namespace std;

void insertionsort(int arr[], int size);

int main()
{
 int arr[100], size;
 cout<<"Enter the size of the array \n";
 cin>>size;

 cout<<"\nInput the array \n";

 for(int i = 1;i<=size; i++)
 {
  cin>>arr[i];
 }

 arr[0] = 32767;

 insertionsort(arr,size);

 cout<<"\nThe sorted array is \n";

 for(int i = 1; i<=size; i++)
 {
  cout<<arr[i]<<' ';
 }

return 0;
}

void insertionsort(int arr[], int size)
{
 for(int j = 2; j<=size; j++)
 {
  int key = arr[j];
  int k = j-1;

  while(key>arr[k])
  {
   arr[k+1] = arr[k];
   k--;
  }

  arr[k+1] = key;
 }
}
 

COUNTING SORT: ASCENDING ORDER

//PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING COUNTING SORT

#include <iostream>
#include <cstring>

using namespace std;

void countingsort(int arr[], int size, int);

int main()
{
	int size, upper;
	
	cout<<"\nEnter the size of the array \n";
	cin>>size;
	cout<<"\nEnter the upper limit\n";
	cin>>upper;
	
	int *arr = new int[size+10];
	
	cout<<"\nInput the array \n";
	
		for(int i = 0; i<size; i++)
		{
			cin>>arr[i];
		}
		
	countingsort(arr,size, upper);
	
	cout<<"\nThe sorted array is \n";
	
		for(int i = 0; i<size; i++)
		{
			cout<<arr[i]<<' ';
		}
		
	return 0;
}

void countingsort(int arr[], int size,int upper)
{
	int *count = new int[upper+10];
	
	int *output = new int[size+10];
		  
	memset(count, 0, (upper+10)*sizeof(int));
		
	for(int i = 0; i<size; i++)
	{
		count[arr[i]]++;
	}
	
	for(int i = 1; i<=upper; i++)
	{
		count[i] += count[i-1];
	}
	
	for(int i = 0; i<size; i++)
	{
		output[count[arr[i]]-1] = arr[i];
		count[arr[i]]--;
	}
	
	for(int i = 0; i<size; i++)
	{
		arr[i] = output[i];
	}  
}
 

PROGRAM TO SORT AN INTEGER ARRAY IN DESCENDING ORDER USING COUNTING SORT

//THIS PROGRAM USES COUNTING SORT TO SORT AN ARRAY OF INTEGERS IN DESCENDING ORDER

#include <iostream>
#include <cstring>

using namespace std;

void countingsort(int [],int size, int upper);

int main()
{
	int size;
	
	cout<<"\nEnter the size of the array \n";
	cin>>size;
	
	int *arr = new int[size+10];
	
	int upper;
	
	cout<<"\nEnter the upper limit\n";
	cin>>upper;
	
	cout<<"\nEnter the array \n";
	
		for(int i= 0; i<size; i++)
		{
			cin>>arr[i];
		}
		
	countingsort(arr,size,upper);
	
	cout<<"\nThe sorted  array is \n";
	
	for(int i = 0; i<size; i++)
	cout<<arr[i]<<' ';
	
	return 0;
}

void countingsort(int arr[], int size, int upper)
{
	int *output = new int[size+10];
	int *count = new int[upper+10];
	
	for(int i = 0; i<=upper; i++)
	{
		count[i] = 0;
	}
	
	for(int i = 0; i<size; i++)                         
	{
	count[arr[i]]++;                                
	}                                 
									      
	for(int i = upper-1; i>=0; i--)       
	{
	count[i] = count[i] + count[i+1];
	}
	
	for(int i = 0; i<size; i++)
	{
		output[count[arr[i]] - 1] = arr[i];
		count[arr[i]]--;
	}
	
	for(int i = 0; i<size; i++)
	{
		arr[i] = output[i];
	}
}
 

THIS PROGRAM PRINTS THE LARGEST GAP BETWEEN TWO PRIME NUMBERS SMALLER THAN AN ENTERED NUMBER

// THIS PROGRAM PRINTS THE LARGEST GAP BETWEEN TWO PRIME NO.S IN A RANGE

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


void main()
{
 clrscr();
 unsigned long long range;
 cout<<"\nEnter the range of prime numbers\n";
 cin>>range;

 unsigned long long *arr = new unsigned long long [range-1];

 for(unsigned long long i = 2; i<=range;i++)
 {
  arr[i-2] = i;
 }

 for(i = 0; arr[i]<=range; i++)
 {
  if(arr[i] != 0)
  {
   for(unsigned long long j = i+1; arr[j]<=range; j++)
   {
    if(arr[j] % arr[i] == 0)
    arr[j] = 0;
   }
  }
 }

cout<<"\nThe prime numbers in this range are\n";

 for(i = 0; i<= range-2; i++)
 {
  if(arr[i] != 0)
  cout<<arr[i]<<' ';
 }

 
 int gap = 0;
 int bestgap = 0;
 unsigned long long loc = 0;

 for(i = 0; i<=range-2; i++)
 {
  gap = 0;
  for(unsigned long long j = i+1; (arr[j] == 0 && j<=range-2); j++)
  {
   gap++;
  }
  if(gap>bestgap)
  {
   bestgap = gap;
   loc = i;
  }
  j--;
  i = j;
 }

 cout<<"\n\nThe bestgap is \n"<<bestgap;
 cout<<"\n\nThe numbers comprising this gap are \n";

 loc++;

 for(i = loc; i<loc+bestgap; i++)
 cout<<i+2<<' ';

 getch();
}
 

PROGRAM TO PRINT UPPER TRIANGLE ELEMENTS OF A MATRIX(USING POINTERS)

// THIS PROGRAM PRINTS UPPER TRIANGLE ELEMENTS OF MATRIX (USING POINTERS)

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

void main()
{
clrscr();
int size;
cout<<“\nENTER THE SIZE OF THE MATRIX\n”;
cin>>size;
int **arr = new int *[size];

for(int i =0; i<size; i++)
arr[i] = new int [size];

cout<<“\nINPUT THE MATRIX \n”;

for(i =0; i<size; i++)
for(int j= 0; j<size; j++)
cin>>*(*(arr+i) + j);

cout<<“\nTHE UPPER TRIANGLE ELEMENTS OF THE MATRIX ARE \n”;

for(i = 0; i<size; i++)
{
for(j = 0; j<size; j++)
{
if(i<j)
cout<<*(*(arr + i) + j)<<‘ ‘;
else
cout<<‘ ‘<<‘ ‘;
}
cout<<‘\n’;
}

getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

PROGRAM TO SUBTRACT TWO MATRICES(USING POINTERS)

// THIS PROGRAM SUBTRACTS TWO MATRICES (USING POINTERS)

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

void main()
{
clrscr();
int size;
cout<<“\nENTER THE SIZE OF THE MATRIX \n”;
cin>>size;

int **arr1 = new int *[size];
int **arr2 = new int *[size];
int **res = new int *[size];

for(int i =0; i<size; i++)
{
arr1[i] = new int [size];
arr2[i] = new int [size];
res [i] = new int [size];
}

cout<<“\nINPUT FIRST MATRIX \n”;

for(i =0; i<size; i++)
for(int j =0; j<size;j++)
cin>>*(*(arr1 + i) + j);

cout<<“\nINPUT SECOND MATRIX \n”;

for(i =0; i<size; i++)
for(j =0; j<size; j++)
cin>>*(*(arr2 + i) + j);

cout<<“THE SUBTRACTED MATRIX IS \n”;

for(i =0; i<size; i++)
{
for(j =0; j<size; j++)
{
*(*(res + i) + j) = *(*(arr1 + i) + j) – *(*(arr2 + i) + j);
cout<<*(*(res + i) + j)<<‘ ‘;
}
cout<<‘\n’;
}

getch();
}

 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays