Tuesday, October 28, 2014

C Array Pro



Print the Number of Odd & Even Numbers in an Array

#include 
#include 
void main()
{
    int array[100], i, num;
 
    printf("Enter the size of an array \n");
    scanf("%d", &num);
    printf("Enter the elements of the array \n");
    for (i = 0; i < num; i++)
    {
        scanf("%d", &array[i]);
    }
    printf("Even numbers in the array are - ");
    for (i = 0; i < num; i++)
    {
        if (array[i] % 2 == 0)
        {
            printf("%d \t", array[i]);
        }
    }
    printf("\n Odd numbers in the array are -");
    for (i = 0; i < num; i++)
    {
        if (array[i] % 2 != 0)
        {
            printf("%d \t", array[i]);
        }
    }
    getch();
}
 
 
Output
 
Enter the size of an array
6
Enter the elements of the array
12
19
45
69
98
23
Even numbers in the array are - 12     98
 Odd numbers in the array are - 19     45     69     23

Print all the Repeated Numbers with Frequency in an Array

#include 
#include 
#include 
 
void duplicate(int array[], int num)
{
    int *count = (int *)calloc(sizeof(int), (num - 2));
    int i;
 
    printf("Duplicate elements present in the given array are ");
    for (i = 0; i < num; i++)
    {
        if (count[array[i]] == 1)
            printf(" %d ", array[i]);
        else
            count[array[i]]++;
    }
}
 
int main()
{
    int array[] = {5, 10, 10, 2, 1, 4, 2};
    int array_freq = sizeof(array) / sizeof(array[0]);
    duplicate(array, array_freq);
    getchar();
    return 0;
    getch();
}
 
Output
 
Duplicate elements present in the given array are  10  2

Find the Largest Number in an Array

#include 
#include 
int main()
{
    int array[50], size, i, largest;
    clrscr();
    printf("\n Enter the size of the array: ");
    scanf("%d", &size);
    printf("\n Enter %d elements of  the array: ", size);
    for (i = 0; i < size; i++)
        scanf("%d", &array[i]);
    largest = array[0];
    for (i = 1; i < size; i++)
    {
        if (largest < array[i])
            largest = array[i];
    }
    printf("\n largest element present in the given array is : %d", largest);
    return 0;
}
 
Output
 
Enter the size of the array: 5
Enter 5 elements of the array: 12
56
34
78
100
Largest element present in the given array is: 100
 

Find the Number of Elements in an Array

 
#include 
#include 
#include 
#include 
int main()
{
    int array[] = {15, 50, 34, 20, 10, 79, 100};
    int n; 
    n = sizeof(array);
    printf("Size of the given array is %d\n", n/sizeof(int));
    return 0;
    getch();
}
 
 
Output
            
            Size of the given array is 7

Check Array bounds while Inputing Elements into the Array

#include 
#include   
int main(void)
{
int array[5], b, c;
clrscr();
for (b = 0; b < 10 && (scanf("%d", &c)); b++)
array[b] = c;
for (b = 0; b < 15; b++)
printf("%d ", array[b]);
return 0;
getch();
}

Output
12
23
56
12
14
19
23
12 23 56 12 14 23 6 134513824 0 -1081194040 11672807 1 -1081193996 -1081193988 -1216161720

No comments:

Post a Comment