C Language | Arrays 23

Question:7
C Program to take input for “n” elements using an array. Pass the entire array to a function, Further print total number of 0 (zero) elements in the array.
Sol:

#include<stdio.h>
void show(int a[],int n)
{
	int i,z=0;
	for(i=0;i<n;i++)
	{
		printf("%d\n",a[i]);
		if(a[i]==0)
		z++;
	}
	printf("Total zero elements = %d\n",z);
}
int main()
{
	int a[50],i,n;
	printf("Enter total elements ");
	scanf("%d",&n);
	/* input  */
	for(i=0;i<n;i++)
	{
		printf("Enter element ");
		scanf("%d",&a[i]);
	}
	/* to display elements */
	show(a,n);
	return(0);
}
/* Output */

Enter total elements 6
Enter element 23
Enter element 0
Enter element 52
Enter element 0
Enter element 65
Enter element 0
23
0
52
0
65
0
Total zero elements = 3

Question:8
C Program to take input for “n” elements using an array. Pass the entire array to a function, Further print the following:
* total number of +ve elements in the array.
* Sum of all +ve elements in the array.
* Average of all +ve elements in the array.
Sol:

#include<stdio.h>
void show(int a[],int n)
{
	int i,p=0,s=0;
	float av;
	for(i=0;i<n;i++)
	{
		printf("%d\n",a[i]);
		if(a[i]>0)
		{
			p++;
			s=s+a[i];
		}
	}
	av=(float)s/p;
	printf("Total +ve elements = %d\n",p);
	printf("Sum of all +ve elements %d\n",s);
	printf("Average of all +ve elements %f\n",av);
}
int main()
{
	int a[50],i,n;
	printf("Enter total elements ");
	scanf("%d",&n);
	/* input  */
	for(i=0;i<n;i++)
	{
		printf("Enter element ");
		scanf("%d",&a[i]);
	}
	/* to display elements */
	show(a,n);
	return(0);
}
/* Output */

Enter total elements 5
Enter element 36
Enter element -96
Enter element -45
Enter element 2
Enter element 5
36
-96
-45
2
5
Total +ve elements = 3
Sum of all +ve elements 43
Average of all +ve elements 14.333333

Question:9
C Program to take input for “n” elements using an array. Pass the entire array to a function, Further print the following:
* total number of -ve elements in the array.
* Sum of all -ve elements in the array.
* Average of all -ve elements in the array.
Sol:

#include<stdio.h>
void show(int a[],int n)
{
	int i,ne=0,s=0;
	float av;
	for(i=0;i<n;i++)
	{
		printf("%d\n",a[i]);
		if(a[i]<0)
		{
			ne++;
			s=s+a[i];
		}
	}
	av=(float)s/ne;
	printf("Total -ve elements = %d\n",ne);
	printf("Sum of all -ve elements %d\n",s);
	printf("Average of all -ve elements %f\n",av);
}
int main()
{
	int a[50],i,n;
	printf("Enter total elements ");
	scanf("%d",&n);
	/* input  */
	for(i=0;i<n;i++)
	{
		printf("Enter element ");
		scanf("%d",&a[i]);
	}
	/* to display elements */
	show(a,n);
	return(0);
}
/* Output */

Enter total elements 5
Enter element 63
Enter element -9
Enter element -75
Enter element 36
Enter element -8
63
-9
-75
36
-8
Total -ve elements = 3
Sum of all -ve elements -92
Average of all -ve elements -30.666666

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects