Write functions C Programming that return the following valu

Write functions (C Programming) that return the following values. ( Assume that a and n are parameters, where a is an array of int values and n is the length of the array). (a) the largest element in a. (b) the average of all elements in a. (c) the number of positive elements in a.

Solution

// C code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double getlargest(int array[], int size)
{
int i;
int largest = array[0];
for (i = 0; i < size; i++)
{
if(array[i] > largest)
largest = array[i];
}

return largest;
}

double getaverage(int array[], int size)
{
int i;
double sum = 0.0;
for (i = 0; i < size; i++)
{
sum = sum + array[i];
}

return sum/size;
}


double getpositive(int array[], int size)
{
int i;
int positive = 0;
for (i = 0; i < size; i++)
{
if(array[i] > 0)
positive = positive +1;
}

return positive;
}

int main()
{
int i;
int array[] = {-4,1,5,-2,3,7,8};
int size = sizeof(array)/sizeof(array[0]);
int largest = getlargest(array,size);
double average = getaverage(array,size);
int positive = getpositive(array,size);
printf(\"array: \ \");
for (i = 0; i < size; ++i)
{
printf(\"%d \",array[i]);
}
printf(\"\ \");
printf(\"Largest: %d\ \",largest);
printf(\"Average: %0.2lf\ \",average);
printf(\"positive count: %d\ \",positive);

printf(\"\ \");
return 0;
}


/*
output:

array:
-4 1 5 -2 3 7 8

Largest: 8
Average: 2.57
positive count: 5

*/

Write functions (C Programming) that return the following values. ( Assume that a and n are parameters, where a is an array of int values and n is the length of
Write functions (C Programming) that return the following values. ( Assume that a and n are parameters, where a is an array of int values and n is the length of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site