Tuesday, October 28, 2014

C Binary array



Accept Sorted Array and do Search using Binary Search
 
#include 
#include 
void main()
{
    int array[10];
    int i, j, num, temp, keynum;
    int low, mid, high;
    clrscr();
    printf("Enter the value of num \n");
    scanf("%d", &num);
    printf("Enter the elements one by one \n");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("Input array elements \n");
    for (i = 0; i < num; i++)
    {
        printf("%d\n", array[i]);
    }
    
    for (i = 0; i < num; i++)
    {
        for (j = 0; j < (num - i - 1); j++)
        {
            if (array[j] > array[j + 1])
            {
                temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }
    printf("Sorted array is...\n");
    for (i = 0; i < num; i++)
    {
        printf("%d\n", array[i]);
    }
    printf("Enter the element to be searched \n");
    scanf("%d", &keynum);
    low = 1;
    high = num;
    do
    {
        mid = (low + high) / 2;
        if (keynum < array[mid])
            high = mid - 1;
        else if (keynum > array[mid])
            low = mid + 1;
    } while (keynum != array[mid] && low <= high);
    if (keynum == array[mid])
    {
        printf("Search successful \n");
    }
    else
    {
        printf("Search failed \n");
    }
}
 
 
Output
 
Enter the value of num
5
Enter the elements one by one
23
90
56
15
58
Input array elements
23
90
56
15
58
Sorted array is...
15
23
56
58
90
Enter the element to be searched
58
Search successful
 
Enter the value of num
4
Enter the elements one by one
1
98
65
45
Input array elements
1
98
65
45
Sorted array is...
1
45
65
98
Enter the element to be searched
6
Search failed

Find the Biggest Number in an Array of Numbers using Recursion

#include 
#include 
int large(int[], int, int);
int main()
{
    int size;
    int largest;
    int list[20];
    int i;
    clrscr();
    printf("Enter size of the list:");
    scanf("%d", &size);
    printf("Printing the list:\n");
    for (i = 0; i < size ; i++)
    {
        list[i] = rand() % size;
        printf("%d\t", list[i]);
    }
    if (size == 0)
    {
        printf("Empty list\n");
    }
    else
    {
        largest = list[0];
        largest = large(list, size - 1, largest);
        printf("\nThe largest number in the list is: %d\n", largest);
    }
}
int large(int list[], int size, int largest)
{
    if (size == 1)
        return largest;
 
    if (size > -1)
    {
        if (list[size] > largest)
        {
            largest = list[size];
        }
        return(largest = large(list, size - 1, largest));
    }
    else
    {
        return largest;
    }
    getch();
}

 
 
Output
 
Enter size of the list:8
Printing the list:
7          6          1          3          1          7          2          4
The largest number in the list is: 7

Compute the Sum of two One Dimensional Arrays using Malloc

#include 
#include 
#include 
#include 
            void main()
{
    int i, n;
    int *a, *b, *c;
    clrscr();
    printf("How many Elements in each array...\n");
    scanf("%d", &n);
    a = (int *)malloc(n * sizeof(int));
    b = (int *)malloc(n * sizeof(int));
    c = (int *)malloc(n * sizeof(int));
    printf("Enter Elements of First List\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", a + i);
    }
    printf("Enter Elements of Second List\n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", b + i);
    }
    for (i = 0; i < n; i++)
    {
        *(c + i) = *(a + i) + *(b + i);
    }
    printf("Resultant List is\n");
    for (i = 0; i < n; i++)
    {
        printf("%d\n", *(c + i));
    }
    getch();
}

Output
 
How many Elements in each array...
5
Enter Elements of First List
23
45
67
12
90
Enter Elements of Second List
87
56
90
45
10
Resultant List is
110
101
157
57
100

No comments:

Post a Comment