Aim:
Write a C program that reads in characters from the input stream and store it in ascending /
descending order using Data Pointers.

Ascending Order

Algorithm:
Step1. Initialize the input array in data memory, with its corresponding memory locations as
0x40.
Step 2. Initialize the output array in data memory, with its corresponding memory locations
as 0x53.
Step 3. Initialize a temporary variable for swapping purpose.
Step 4. Copy all the elements of input array to output array, using the array pointer concept.
Step 5. Take the first element in array and compare with the other elements in array, and if
and only if, it is the smallest number, using temporary variable swap the smallest
variable to the first position.
Step 6. Now Repeat 5 with second number and with rest of the array, until last elements is
reached.
Step 7. Stop the execution.

Program for Ascending order

#include <reg51.h>
unsigned char data input_array[5] _at_ 0x40;
unsigned char data *input_ptr=&input_array;
unsigned char data output_array[5] _at_ 0x53;
void main()
{
unsigned char i,j;
unsigned char a=0;
//Pointer for coping the data to output array
for(i=0;i<5;i++,input_ptr++)
{
output_array[i]=*input_ptr;
}
//Ascending Code
for (i = 0; i < 5; ++i)
{
for (j = i + 1; j < 5; ++j)
{
if (output_array[i] > output_array[j])
{
a = output_array[i];
output_array[i] = output_array[j];
output_array[j] = a;
}
}
}
}

Descending Order

Algorithm:
Step1. Initialize the input array in data memory, with its corresponding memory locations as
0x40.
Step 2. Initialize the output array in data memory, with its corresponding memory locations
as 0x53.
Step 3. Initialize a temporary variable for swapping purpose.
Step 4. Copy all the elements of input array to output array, using the array pointer concept.
Step 5. Take the first element in array and compare with the other elements in array, and if
and only if, it is the largest number, using temporary variable swap the largest
variable to the first position
Step 6. Now Repeat 5 with second number and with rest of the array, until last elements is
reached
Step 7. Stop the execution.
Program for Descending order
#include <reg51.h>
unsigned char data input_array[5] _at_ 0x40;
unsigned char data *input_ptr=&input_array;
unsigned char data output_array[5] _at_ 0x53;
void main()
{
unsigned char i,j;
unsigned char a=0;
//Pointer for coping the data to output array
for(i=0;i<5;i++,input_ptr++)
{
output_array[i]=*input_ptr;
}
//Ascending Code
for (i = 0; i < 5; ++i)
{
for (j = i + 1; j < 5; ++j)
{
if (output_array[i] < output_array[j])
{
a = output_array[i];
output_array[i] = output_array[j];
output_array[j] = a;
}
}
}
}31