Slip 23 - B) Accept n integers in an array. Copy only the non-zero elements to another array (allocated using dynamic memory allocation). Calculate the sum and average of non-zero elements.

Solution:



#include<stdio.h>
#include<stdlib.h>
void main(){
        int a[30], i,n, sum = 0;
    int count=0;
    int *ptr;
    float avg;
        printf("\n Enter the total number of elements you want to enter : ");
    scanf("%d",&n);
                printf("\n Enter element in aaray");
        for(i = 0;i<n;i++){
        scanf("%d",&a[i]);       }
    for(i = 0;i<n;i++){
            if(a[i]!=0){
                                                  count++;   }
    }
    ptr = (int *)malloc(count * sizeof(int));
    for(i = 0;i<n;i++){
            if(a[i]!=0){
        *(ptr+i)=a[i];
                sum=sum+*(ptr+i);        }
                }
    avg=sum/count;
    printf("sum is %d \n",sum);
        printf("avg is %f\n",avg);
        getch();   
  }

Post a Comment

0 Comments