C Language: Pointers 10

Question:7
C Program to take input for “N” elements using an array, pass the entire array to a function, display the array elements and check and print total odd elements in the array.
Sol:

#include <stdio.h> 
void show(int *p,int n)
{
     int i,od=0;
     for(i=0;i<n;i++)
     {
          printf("%d\n",*p);
          if(*p % 2==1)// if(*p % 2!=0)
          od=od+1;
          
          p++;
     }
     printf("Total odd elements = %d\n",od);
}
int main()
{
    int a[20],i,n;
    printf("Enter total elements ");
    scanf("%d",&n);
    /* input  */
    for(i=0;i<n;i++)
    {
         printf("Enter %d element ",i);
         scanf("%d",&a[i]);
     }
     /* function calling */
     show(a,n);
     /* or */
     /* show(&a[0],n); */
     
    return(0);
}
/* Output */
Enter total elements 5
Enter 0 element 24
Enter 1 element 25
Enter 2 element 63
Enter 3 element 66
Enter 4 element 21
24
25
63
66
21
Total odd elements = 3

Question:8
C Program to take input for “N” elements using an array, pass the entire array to a function, display the largest array elements.
Sol:

#include <stdio.h> 
void show(int *p,int n)
{
     int i,m;
     m=*p;
     for(i=0;i<n;i++)
     {
          printf("%d\n",*p);
          if(*p>m)
          m=*p;
          
          p++;
     }
     printf("Max element = %d\n",m);
}
int main()
{
    int a[20],i,n;
    printf("Enter total elements ");
    scanf("%d",&n);
    /* input  */
    for(i=0;i<n;i++)
    {
         printf("Enter %d element ",i);
         scanf("%d",&a[i]);
     }
     /* function calling */
     show(a,n);
     /* or */
     /* show(&a[0],n); */
     
    return(0);
}
/* Output */
Enter total elements 5
Enter 0 element 24
Enter 1 element 63
Enter 2 element 245
Enter 3 element 21
Enter 4 element 5
24
63
245
21
5
Max element = 245

Question:9
C Program to take input for “N” elements using an array, pass the entire array to a function, display the lowest array elements.
Sol:

#include <stdio.h> 
void show(int *p,int n)
{
     int i,m;
     m=*p;
     for(i=0;i<n;i++)
     {
          printf("%d\n",*p);
          if(*p<m)
          m=*p;
          
          p++;
     }
     printf("Min element = %d\n",m);
}
int main()
{
    int a[20],i,n;
    printf("Enter total elements ");
    scanf("%d",&n);
    /* input  */
    for(i=0;i<n;i++)
    {
         printf("Enter %d element ",i);
         scanf("%d",&a[i]);
     }
     /* function calling */
     show(a,n);
     /* or */
     /* show(&a[0],n); */
     
    return(0);
}
/* Output */
Enter total elements 5
Enter 0 element 24
Enter 1 element 6
Enter 2 element 3
Enter 3 element 32
Enter 4 element 89
24
6
3
32
89
Min element = 3