Sunday 15 December 2013

C Program to Accept an Array & Swap Elements using Pointers

#include < stdio.h>
void swap34(float *ptr1, float *ptr2);

void main()
{
float x[10];
int i, n;

printf("How many Elements...\n" );
scanf("%d" , &n);
printf("Enter Elements one by one\n" );
for (i = 0; i < n; i++)
{
scanf("%f" , x + i);
}
/* Function call:Interchanging 3rd element by 4th */
swap34(x + 2, x + 3);
printf("\nResultant Array...\n" );
for (i = 0; i < n; i++)
{
printf("X[%d] = %f\n" , i, x[i]);
}
}
/* Function to swap the 3rd element with the 4th element in the array */
void swap34(float *ptr1, float *ptr2 )
{
float temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}

Output

 How many Elements...
4
Enter Elements one by one
23
67
45
15

Resultant Array...
X [ 0 ] = 23.000000
X [ 1 ] = 67.000000
X [ 2 ] = 15.000000
X [ 3 ] = 45.000000

For More Details Please Visit Ictjobs.info

No comments: