Friday, December 19, 2014

Inputing Elements into c pgm



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;
getch();
}
 
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