2. (a) Write a function that calculates the geometric mean of a vector x = (x1,
ID: 3885872 • Letter: 2
Question
2. (a) Write a function that calculates the geometric mean of a vector x = (x1, . . . , xn) and returns an error message if any element of x is negative.
(b) Apply your function to each column of the dataset given below. Some of the columns have “data entry errors” and contain negative numbers. Create a labeled output vector that gives the geometric mean for valid data and NA for columns with invalid data.
(Hint use try()).
set.seed(123) data <- matrix(rnorm(10000, mean=3), ncol=25, dimnames=list(NULL, paste("X", 1:25, sep=".")))
(c) Write a function which takes a numeric vector x, and returns a named list containing the mean, median and variance of the values in x.
(d) Write a function which goes through every entry in a list, cheks whether it is a character vector (is.character()), and if so prints it (print() or cat()).
Explanation / Answer
#include<stdio.h>
#include<math.h>
//Function to calculate the product of all the elements from the array and return it.
double ArrayProduct(double *vect, int size)
{
//Product is initialized to 1
double productValue = 1.0;
//Loop variable
int counter;
//Loops till one less than size
for(counter = 0; counter < size; counter++)
{
//Calculates the product
productValue *= vect[counter];
}//End of loop
//Returns the product
return productValue;
}//End of function
//Function to calculate the geometric mean of all the elements from the array and return it.
double GeometricMean(double* array, int size)
{
//Calculates the geometric mean
double result = pow(ArrayProduct(array,size), (1.0/(double)(size)));
//Returns the result
return result;
}//End of function
//Main function
int main()
{
//Declares and assigns the values to the array
double vect[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
//Calls the function and displays geometric mean
printf("Geometric Mean = %lf", GeometricMean(vect, 7));
}//End of main
Sample Run:
Geometric Mean = 3.380015
2 c)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function to calculate mean, median and variance
void MeanMedianVariance(float vec[], int size, float *mean, float *median, float *var)
{
//Loop variable and temporary variable
int counter, colCounter, temp;
//Calculates the mean
//Loops till one less than size of vec array
for (counter = 0; counter < size; counter++)
*mean = *mean + vec[counter];
//Divides the sum with size of array
*mean = *mean / size;
//Calculates the variance
//Loops till one less than the size of vec array
for (counter = 0; counter < size; counter++)
*var = *var + pow((vec[counter] - *mean) , 2);
*var = *var / size;
//Sort the array contents to find the median value
for (counter = 0; counter < size - 1; counter++)
{
//Loops till one less than the size of vec array
for (colCounter = counter; colCounter < size; colCounter++)
{
//Checks if the counter index position value of array is greater than the colCounter index position of the array then swap
if (vec[counter] > vec[colCounter])
{
//Swapping process
temp = vec[counter];
vec[counter] = vec[colCounter];
vec[colCounter] = temp;
}//End of if
}//End of inner loop
}//End of outer loop
//Calculate the median value
//Checks for even size
if ((size + 1) % 2 == 0)
{
//Calculates median
*median = vec[((size + 1) / 2) - 1];
}//End of if
//Odd size
else
{
//Calculates median
*median = (vec[((size + 1) / 2) - 1] + vec[((size + 2) / 2) - 1]) / 2;
}//End of else
}//End of function
//Main function
int main()
{
//Declares local variable
//*vect: Pointer to store numbers entered by the user
//mean: To store the mean value
//median: To store median value
//variance: To store the variance value
float *vect, mean = 0, median, sd, variance;
//To store size
int size;
//Loop variable
int counter;
//Accepts the size of the array
printf(" Enter how many numbers you want? ");
scanf("%d", &size);
//Allocates memory to the pointer
vect = (float *)malloc(sizeof (float) * size);
//Accepts data from the user for the array
printf("Enter the numbers: ");
//Loops till size and accepts data
for (counter = 0; counter < size; counter++)
scanf("%f", &vect[counter]);
//Call the function to calculate mean, median and variance
MeanMedianVariance(vect, size, &mean, &median, &variance);
//Displays the variance value
printf("Variance: %f ", variance);
//Displays the mean value
printf("Mean : %f ", mean);
//Displays the median value
printf("Median: %f ", median);
return 0;
}//End of main
Sample Run:
Enter how many numbers you want? 8
Enter the numbers:
120
250
310
110
540
300
600
220
Variance: 28098.437500
Mean : 306.250000
Median: 275.000000
2 d)
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
//Function to check array contents if it contains character then print it
void printCharacter(char arr[], int size)
{
//Loop variable
int counter;
//Loops till one less than the size
for(counter = 0; counter < size; counter++)
{
//Checks for lowercase and uppercase character
if((arr[counter] >= 'a' && arr[counter] <= 'z') || (arr[counter] >= 'A' && arr[counter] <= 'Z'))
//print the character
printf("%c ", arr[counter]);
}//End of for loop
}//End of function
//Main function
int main()
{
//Declares a character pointer
char *vect;
//To store size
int size;
//Accepts the size of the array
printf(" Enter size of array: ");
scanf("%d", &size);
//Allocates memory to the pointer
vect = (char *)malloc(sizeof (char) * size);
//Accepts data from the user for the array
printf("Enter the data: ");
scanf("%s", vect);
//Calls the method to display the character
printCharacter(vect, size);
}//End of main
Sample Run:
Enter size of array: 10
Enter the data:
this3is20
t h i s i s
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.