Wednesday, October 29, 2014

C Array Pointers



Calculate Sum of all Elements of an Array using Pointers as Arguments

 
#include 
#include 
void main()
{
    static int array[5] = { 200, 400, 600, 800, 1000 };
    int sum;
    int addnum(int *ptr);
    sum = addnum(array);
    printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr)
{
    int index, total = 0;
    for (index = 0; index < 5; index++)
    {
        total += *(ptr + index);
    }
    return(total);
   getch();
}
 
Output
            Sum of all array elements =  3000
 

Accept an Array & Swap Elements using Pointers

 
#include 
#include 
void swap34(float *ptr1, float *ptr2);
void main()
{
    float x[10];
    int i, n;
    clrscr();
    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);
    }
    swap34(x + 2, x + 3);
    printf("\nResultant Array...\n");
    for (i = 0; i < n; i++)
    {
        printf("X[%d] = %f\n", i, x[i]);
    }
}
void swap34(float *ptr1, float *ptr2 )
{
    float temp;
    temp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = temp;
getch();
}
 
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

Read an Array and Search for an Element

           #include 
#include  
void main()
{
    int array[20];
    int i, low, mid, high, key, size;
    clrscr();
    printf("Enter the size of an array\n");
    scanf("%d", &size);
    printf("Enter the array elements\n");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("Enter the key\n");
    scanf("%d", &key);
    
    low = 0;
    high = (size - 1);
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (key == array[mid])
        {
            printf("Successful search\n");
            return;
        }
        if (key < array[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }
    printf("unsuccessful search\n");
    getch();
}
 
Output
 
Enter the size of an array
4
Enter the array elements
90
560
300
390
Enter the key
90
Successful search
 
Enter the size of an array
4
Enter the array elements
100
500
580
470
Enter the key
300
Unsuccessful search

Merge and Sort Elements of 2 different arrays

 
#include 
#include 
void Merge(int * , int , int , int );
 
void MergeSort(int *array, int left, int right)
{
    int middle = (left+right)/2;
        if(left<right)
    {
        MergeSort(array, left, middle);
        MergeSort(array, middle + 1, right);
        Merge(array, left, middle, right);
    }
}
void Merge(int *array, int left, int middle, int right)
{
        int tmp[right - left + 1];
    int pos = 0, leftposition = left, rightposition = middle + 1;
    while (leftposition <= middle && rightposition <= right)
    {
        if (array[leftposition] < array[rightposition])
        {
            tmp[pos++] = array[leftposition++];
        }
        else
        {
            tmp[pos++] = array[rightposition++];
        }
    }
    while (leftposition <= middle)
        tmp[pos++] = array[leftposition++];
    while (rightposition <= right)
        tmp[pos++] = array[rightposition++];
    int i;
    
    for (i = 0; i < pos; i++)
    {
        array[i + left] = tmp[i];
    }
    return;
}
int main()
{
    int size;
    printf("\n enter the size of an array");
    scanf("%d", &size);
    int array[size];
    int i, j, k;
    printf("\n enter the array elements");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &array[i]);
    }
        MergeSort(array, 0, size - 1);
    for (i = 0; i < size; i++)
    {
        printf("%d ", array[i]);
    }
    printf("\n");
    return 0;
    getch();
}
 
Output
 
Enter the size of an array10
Enter the array elements-12
10
45
32
49
-58
69
38
98
34
-58 -12 10 32 34 38 45 49 69 98
 

No comments:

Post a Comment